1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace ACSEO \Bundle \TypesenseBundle \Tests \Transformer ;
6+
7+ use ACSEO \TypesenseBundle \Client \CollectionClient ;
8+ use ACSEO \TypesenseBundle \Client \TypesenseClient ;
9+ use ACSEO \TypesenseBundle \Tests \Functional \Entity \Book ;
10+ use ACSEO \TypesenseBundle \Tests \Functional \Entity \Author ;
11+ use ACSEO \TypesenseBundle \Transformer \DoctrineToTypesenseTransformer ;
12+ use Symfony \Component \PropertyAccess \PropertyAccess ;
13+ use PHPUnit \Framework \TestCase ;
14+
15+ class DoctrineToTypesenseTransformerTest extends TestCase
16+ {
17+
18+ public function testConvert ()
19+ {
20+ $ collectionDefinitions = $ this ->getCollectionDefinitions (Book::class);
21+ $ propertyAccessor = PropertyAccess::createPropertyAccessor ();
22+ $ transformer = new DoctrineToTypesenseTransformer ($ collectionDefinitions , $ propertyAccessor );
23+
24+ $ book = new Book (1 , 'test ' , new Author ('Nicolas Potier ' , 'France ' ), new \Datetime ('02/10/1984 ' ));
25+ self ::assertEquals (
26+ [
27+ "id " => "1 " ,
28+ "sortable_id " => 1 ,
29+ "title " => "test " ,
30+ "author " => "Nicolas Potier " ,
31+ "author_country " => "France " ,
32+ "published_at " => 445219200
33+ ],
34+ $ transformer ->convert ($ book )
35+ );
36+
37+ $ book = new Book (1 , 'test ' , new Author ('Nicolas Potier ' , 'France ' ), new \DateTimeImmutable ('02/10/1984 ' ));
38+
39+ self ::assertEquals (
40+ [
41+ "id " => "1 " ,
42+ "sortable_id " => 1 ,
43+ "title " => "test " ,
44+ "author " => "Nicolas Potier " ,
45+ "author_country " => "France " ,
46+ "published_at " => 445219200
47+ ],
48+ $ transformer ->convert ($ book )
49+ );
50+ }
51+
52+ public function testCastValueDatetime ()
53+ {
54+ $ collectionDefinitions = $ this ->getCollectionDefinitions (Book::class);
55+ $ propertyAccessor = PropertyAccess::createPropertyAccessor ();
56+ $ transformer = new DoctrineToTypesenseTransformer ($ collectionDefinitions , $ propertyAccessor );
57+ //Datetime
58+ $ value = $ transformer ->castValue (Book::class, 'published_at ' , new \Datetime ('02/10/1984 ' ));
59+ self ::assertEquals (445219200 , $ value );
60+ //DatetimeImmutable
61+ $ value = $ transformer ->castValue (Book::class, 'published_at ' , new \DatetimeImmutable ('02/10/1984 ' ));
62+ self ::assertEquals (445219200 , $ value );
63+ }
64+
65+ public function testCastValueObject ()
66+ {
67+ $ collectionDefinitions = $ this ->getCollectionDefinitions (Book::class);
68+ $ propertyAccessor = PropertyAccess::createPropertyAccessor ();
69+ $ transformer = new DoctrineToTypesenseTransformer ($ collectionDefinitions , $ propertyAccessor );
70+
71+ // Conversion OK
72+ $ author = new Author ('Nicolas Potier ' , 'France ' );
73+ $ value = $ transformer ->castValue (Book::class, 'author ' , $ author );
74+ self ::assertEquals ($ author ->__toString (), $ value );
75+
76+ // Conversion KO
77+ $ this ->expectExceptionMessage ('Call to undefined method ArrayObject::__toString() ' );
78+ $ value = $ transformer ->castValue (Book::class, 'author ' , new \ArrayObject ());
79+ }
80+
81+ private function getCollectionDefinitions ($ entityClass )
82+ {
83+ return [
84+ 'books ' => [
85+ 'typesense_name ' => 'books ' ,
86+ 'entity ' => $ entityClass ,
87+ 'name ' => 'books ' ,
88+ 'fields ' => [
89+ 'id ' => [
90+ 'name ' => 'id ' ,
91+ 'type ' => 'primary ' ,
92+ 'entity_attribute ' => 'id ' ,
93+ ],
94+ 'sortable_id ' => [
95+ 'entity_attribute ' => 'id ' ,
96+ 'name ' => 'sortable_id ' ,
97+ 'type ' => 'int32 ' ,
98+ ],
99+ 'title ' => [
100+ 'name ' => 'title ' ,
101+ 'type ' => 'string ' ,
102+ 'entity_attribute ' => 'title ' ,
103+ ],
104+ 'author ' => [
105+ 'name ' => 'author ' ,
106+ 'type ' => 'object ' ,
107+ 'entity_attribute ' => 'author ' ,
108+ ],
109+ 'michel ' => [
110+ 'name ' => 'author_country ' ,
111+ 'type ' => 'string ' ,
112+ 'entity_attribute ' => 'author.country ' ,
113+ ],
114+ 'publishedAt ' => [
115+ 'name ' => 'published_at ' ,
116+ 'type ' => 'datetime ' ,
117+ 'optional ' => true ,
118+ 'entity_attribute ' => 'publishedAt ' ,
119+ ],
120+ ],
121+ 'default_sorting_field ' => 'sortable_id ' ,
122+ ],
123+ ];
124+ }
125+ }
0 commit comments