From a8480d5e1b3a39e37bb875072eaf1926fa422fc6 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 19 Mar 2020 22:11:22 +0000 Subject: [PATCH] 5d. Refactor unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../src/Unit/Wrapper/ArticleWrapperTest.php | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/web/modules/custom/my_module/tests/src/Unit/Wrapper/ArticleWrapperTest.php b/web/modules/custom/my_module/tests/src/Unit/Wrapper/ArticleWrapperTest.php index 64b2b24..98e90b4 100644 --- a/web/modules/custom/my_module/tests/src/Unit/Wrapper/ArticleWrapperTest.php +++ b/web/modules/custom/my_module/tests/src/Unit/Wrapper/ArticleWrapperTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\my_module\Unit\Wrapper; +use Drupal\Component\Datetime\Time; use Drupal\Component\Datetime\TimeInterface; use Drupal\my_module\Wrapper\ArticleWrapper; use Drupal\node\NodeInterface; @@ -9,14 +10,21 @@ use Drupal\Tests\UnitTestCase; class ArticleWrapperTest extends UnitTestCase { + private $time; + + protected function setUp() { + parent::setUp(); + + $this->time = $this->createMock(Time::class); + } + /** @test */ public function it_can_return_the_article() { $article = $this->createMock(NodeInterface::class); $article->method('id')->willReturn(5); $article->method('bundle')->willReturn('article'); - $time = $this->createMock(TimeInterface::class); - $articleWrapper = new ArticleWrapper($time, $article); + $articleWrapper = $this->createArticleWrapper($article); $this->assertInstanceOf(NodeInterface::class, $articleWrapper->getOriginal()); $this->assertSame(5, $articleWrapper->getOriginal()->id()); @@ -30,8 +38,7 @@ class ArticleWrapperTest extends UnitTestCase { $page = $this->createMock(NodeInterface::class); $page->method('bundle')->willReturn('page'); - $time = $this->createMock(TimeInterface::class); - new ArticleWrapper($time, $page); + $articleWrapper = $this->createArticleWrapper($page); } /** @@ -42,9 +49,7 @@ class ArticleWrapperTest extends UnitTestCase { string $offset, bool $expected ) { - $time = $this->createMock(TimeInterface::class); - - $time->method('getRequestTime')->willReturn( + $this->time->method('getRequestTime')->willReturn( (new \DateTime())->getTimestamp() ); @@ -55,7 +60,7 @@ class ArticleWrapperTest extends UnitTestCase { (new \DateTime())->modify($offset)->getTimestamp() ); - $articleWrapper = new ArticleWrapper($time, $article); + $articleWrapper = $this->createArticleWrapper($article); $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); + } + }