Mock loadByProperties, return mock nodes, add
...assertions
This commit is contained in:
parent
09f7f528b9
commit
bc34a96187
|
@ -2,8 +2,10 @@
|
|||
|
||||
namespace Drupal\Tests\example\Unit;
|
||||
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\example\Repository\PostNodeRepository;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
|
@ -13,14 +15,48 @@ final class PostNodeRepositoryUnitTest extends UnitTestCase {
|
|||
|
||||
/** @test */
|
||||
public function it_returns_posts(): void {
|
||||
$node1 = $this->createMock(NodeInterface::class);
|
||||
$node1->method('bundle')->willReturn('post');
|
||||
$node1->method('getCreatedTime')->willReturn(strtotime('-1 week'));
|
||||
$node1->method('label')->willReturn('Post one');
|
||||
|
||||
$node2 = $this->createMock(NodeInterface::class);
|
||||
$node2->method('bundle')->willReturn('post');
|
||||
$node2->method('getCreatedTime')->willReturn(strtotime('-8 days'));
|
||||
$node2->method('label')->willReturn('Post two');
|
||||
|
||||
$node3 = $this->createMock(NodeInterface::class);
|
||||
$node3->method('bundle')->willReturn('post');
|
||||
$node3->method('getCreatedTime')->willReturn(strtotime('yesterday'));
|
||||
$node3->method('label')->willReturn('Post three');
|
||||
|
||||
$node4 = $this->createMock(NodeInterface::class);
|
||||
$node4->method('bundle')->willReturn('page');
|
||||
$node4->method('label')->willReturn('Not a post');
|
||||
|
||||
$nodeStorage = $this->createMock(EntityStorageInterface::class);
|
||||
$nodeStorage->method('loadByProperties')->willReturn([$node1, $node2, $node3]);
|
||||
|
||||
$entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
|
||||
$entityTypeManager->method('getStorage')->with('node')->willReturn($nodeStorage);
|
||||
|
||||
$repository = new PostNodeRepository($entityTypeManager);
|
||||
|
||||
$repository->findAll();
|
||||
$posts = $repository->findAll();
|
||||
|
||||
self::assertContainsOnlyInstancesOf(NodeInterface::class, $posts);
|
||||
|
||||
$titles = array_map(
|
||||
fn (NodeInterface $node) => $node->label(),
|
||||
$posts,
|
||||
);
|
||||
|
||||
self::assertCount(3, $titles);
|
||||
self::assertSame(
|
||||
['Post two', 'Post one', 'Post three'],
|
||||
$titles,
|
||||
);
|
||||
self::assertNotContains('Not a page', $titles);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue