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:
parent
f0f93912ee
commit
a8480d5e1b
1 changed files with 17 additions and 8 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue