Skip to content

Commit 4e50e42

Browse files
committed
Add unit test.
1 parent 4c3105c commit 4e50e42

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"require-dev": {
2323
"symfony/phpunit-bridge": "^5.0|^6.0",
2424
"phpunit/phpunit": "^9.5",
25-
"symfony/yaml": "^3.4 || ^4.4 || ^5.4 || ^6.0"
25+
"symfony/yaml": "^3.4 || ^4.4 || ^5.4 || ^6.0",
26+
"phpspec/prophecy-phpunit": "^2.0"
2627
},
2728
"autoload": {
2829
"psr-4": { "ACSEO\\TypesenseBundle\\": "src/" }
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ACSEO\Bundle\TypesenseBundle\Tests\Unit\EventListener;
6+
7+
use ACSEO\TypesenseBundle\Client\CollectionClient;
8+
use ACSEO\TypesenseBundle\Manager\DocumentManager;
9+
use ACSEO\TypesenseBundle\EventListener\TypesenseIndexer;
10+
use ACSEO\TypesenseBundle\Manager\CollectionManager;
11+
use ACSEO\TypesenseBundle\Tests\Functional\Entity\Book;
12+
use ACSEO\TypesenseBundle\Tests\Functional\Entity\Author;
13+
use ACSEO\TypesenseBundle\Transformer\DoctrineToTypesenseTransformer;
14+
use Doctrine\Persistence\Event\LifecycleEventArgs;
15+
use Doctrine\Persistence\ObjectManager;
16+
use PHPUnit\Framework\TestCase;
17+
use Prophecy\PhpUnit\ProphecyTrait;
18+
use Symfony\Component\PropertyAccess\PropertyAccess;
19+
20+
class TypesenseIndexerTest extends TestCase
21+
{
22+
use ProphecyTrait;
23+
24+
public function setUp(): void
25+
{
26+
$this->objectManager = $this->prophesize(ObjectManager::class);
27+
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
28+
}
29+
30+
private function initialize($collectionDefinitions)
31+
{
32+
$transformer = new DoctrineToTypesenseTransformer($collectionDefinitions, $this->propertyAccessor);
33+
34+
$collectionClient = $this->prophesize(CollectionClient::class);
35+
36+
$collectionManager = new CollectionManager($collectionClient->reveal(), $transformer, $collectionDefinitions);
37+
$this->documentManager = $this->prophesize(DocumentManager::class);
38+
39+
$this->eventListener = new TypesenseIndexer($collectionManager, $this->documentManager->reveal(), $transformer);
40+
}
41+
42+
/**
43+
* @dataProvider postUpdateProvider
44+
*/
45+
public function testPostUpdate($prefix)
46+
{
47+
$collectionDefinitions = $this->getCollectionDefinitions(Book::class, $prefix);
48+
49+
$this->initialize($collectionDefinitions);
50+
51+
$book = new Book(1, 'The Doors of Perception', new Author('Aldoux Huxley', 'United Kingdom'), new \DateTime('1954-01-01'));
52+
53+
$eventArgs = new LifecycleEventArgs($book, $this->objectManager->reveal());
54+
55+
$this->eventListener->postUpdate($eventArgs);
56+
$this->eventListener->postFlush();
57+
58+
$this->documentManager->delete(sprintf('%sbooks', $prefix), 1)->shouldHaveBeenCalled();
59+
$this->documentManager->index(sprintf('%sbooks', $prefix), [
60+
'id' => 1,
61+
'sortable_id' => 1,
62+
'title' => 'The Doors of Perception',
63+
'author' => 'Aldoux Huxley',
64+
'author_country' => 'United Kingdom',
65+
'published_at' => -504921600,
66+
])->shouldHaveBeenCalled();
67+
}
68+
69+
public function postUpdateProvider()
70+
{
71+
return [
72+
[ '' ],
73+
[ 'foo_' ],
74+
];
75+
}
76+
77+
private function getCollectionDefinitions($entityClass, $prefix = '')
78+
{
79+
return [
80+
'books' => [
81+
'typesense_name' => sprintf('%sbooks', $prefix),
82+
'entity' => $entityClass,
83+
'name' => 'books',
84+
'fields' => [
85+
'id' => [
86+
'name' => 'id',
87+
'type' => 'primary',
88+
'entity_attribute' => 'id',
89+
],
90+
'sortable_id' => [
91+
'entity_attribute' => 'id',
92+
'name' => 'sortable_id',
93+
'type' => 'int32',
94+
],
95+
'title' => [
96+
'name' => 'title',
97+
'type' => 'string',
98+
'entity_attribute' => 'title',
99+
],
100+
'author' => [
101+
'name' => 'author',
102+
'type' => 'object',
103+
'entity_attribute' => 'author',
104+
],
105+
'michel' => [
106+
'name' => 'author_country',
107+
'type' => 'string',
108+
'entity_attribute' => 'author.country',
109+
],
110+
'publishedAt' => [
111+
'name' => 'published_at',
112+
'type' => 'datetime',
113+
'optional' => true,
114+
'entity_attribute' => 'publishedAt',
115+
],
116+
],
117+
'default_sorting_field' => 'sortable_id',
118+
],
119+
];
120+
}
121+
}

0 commit comments

Comments
 (0)