5d. Refactor unit tests

As we need the `Time` object to be passed as an argument to the article
wrapper in every test method, let’s refactor and create a private method
that is responsible for creating the wrapper, and any arguments that it
needs.
This commit is contained in:
Oliver Davies 2020-03-19 22:11:22 +00:00
parent f0f93912ee
commit a8480d5e1b

View file

@ -2,6 +2,7 @@
namespace Drupal\Tests\my_module\Unit\Wrapper; namespace Drupal\Tests\my_module\Unit\Wrapper;
use Drupal\Component\Datetime\Time;
use Drupal\Component\Datetime\TimeInterface; use Drupal\Component\Datetime\TimeInterface;
use Drupal\my_module\Wrapper\ArticleWrapper; use Drupal\my_module\Wrapper\ArticleWrapper;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
@ -9,14 +10,21 @@ use Drupal\Tests\UnitTestCase;
class ArticleWrapperTest extends UnitTestCase { class ArticleWrapperTest extends UnitTestCase {
private $time;
protected function setUp() {
parent::setUp();
$this->time = $this->createMock(Time::class);
}
/** @test */ /** @test */
public function it_can_return_the_article() { public function it_can_return_the_article() {
$article = $this->createMock(NodeInterface::class); $article = $this->createMock(NodeInterface::class);
$article->method('id')->willReturn(5); $article->method('id')->willReturn(5);
$article->method('bundle')->willReturn('article'); $article->method('bundle')->willReturn('article');
$time = $this->createMock(TimeInterface::class); $articleWrapper = $this->createArticleWrapper($article);
$articleWrapper = new ArticleWrapper($time, $article);
$this->assertInstanceOf(NodeInterface::class, $articleWrapper->getOriginal()); $this->assertInstanceOf(NodeInterface::class, $articleWrapper->getOriginal());
$this->assertSame(5, $articleWrapper->getOriginal()->id()); $this->assertSame(5, $articleWrapper->getOriginal()->id());
@ -30,8 +38,7 @@ class ArticleWrapperTest extends UnitTestCase {
$page = $this->createMock(NodeInterface::class); $page = $this->createMock(NodeInterface::class);
$page->method('bundle')->willReturn('page'); $page->method('bundle')->willReturn('page');
$time = $this->createMock(TimeInterface::class); $articleWrapper = $this->createArticleWrapper($page);
new ArticleWrapper($time, $page);
} }
/** /**
@ -42,9 +49,7 @@ class ArticleWrapperTest extends UnitTestCase {
string $offset, string $offset,
bool $expected bool $expected
) { ) {
$time = $this->createMock(TimeInterface::class); $this->time->method('getRequestTime')->willReturn(
$time->method('getRequestTime')->willReturn(
(new \DateTime())->getTimestamp() (new \DateTime())->getTimestamp()
); );
@ -55,7 +60,7 @@ class ArticleWrapperTest extends UnitTestCase {
(new \DateTime())->modify($offset)->getTimestamp() (new \DateTime())->modify($offset)->getTimestamp()
); );
$articleWrapper = new ArticleWrapper($time, $article); $articleWrapper = $this->createArticleWrapper($article);
$this->assertSame($expected, $articleWrapper->isPublishable()); $this->assertSame($expected, $articleWrapper->isPublishable());
} }
@ -69,5 +74,9 @@ class ArticleWrapperTest extends UnitTestCase {
]; ];
} }
private function createArticleWrapper(NodeInterface $article): ArticleWrapper {
return new ArticleWrapper($this->time, $article);
}
} }