33
44use Sirius \Orm \Connection ;
55use Sirius \Orm \ConnectionLocator ;
6- use Sirius \Orm \Mapper ;
7- use Sirius \Orm \MapperConfig ;
86use Sirius \Orm \Orm ;
9- use Sirius \Orm \Relation \RelationConfig ;
107
118require_once __DIR__ . '/../AbstractTestSuite.php ' ;
129
@@ -22,6 +19,11 @@ class SiriusOrmTestSuite extends AbstractTestSuite
2219 */
2320 private $ orm ;
2421
22+ /**
23+ * @var \app\Entity\Product
24+ */
25+ private $ product ;
26+
2527 function initialize ()
2628 {
2729 $ loader = require_once __DIR__ . "/vendor/autoload.php " ;
@@ -31,44 +33,21 @@ function initialize()
3133
3234 $ this ->initTables ();
3335
34- $ this ->orm ->register ('products ' , Mapper::make ($ this ->orm , MapperConfig::fromArray ([
35- MapperConfig::TABLE => 'products ' ,
36- MapperConfig::COLUMNS => ['id ' , 'name ' , 'sku ' , 'price ' , 'category_id ' ],
37- MapperConfig::RELATIONS => [
38- 'images ' => [
39- RelationConfig::FOREIGN_MAPPER => 'images ' ,
40- RelationConfig::TYPE => RelationConfig::TYPE_ONE_TO_MANY ,
41- RelationConfig::FOREIGN_KEY => 'imageable_id ' ,
42- RelationConfig::FOREIGN_GUARDS => ['imageable_type ' => 'products ' ]
43- ],
44- 'category ' => [
45- RelationConfig::FOREIGN_MAPPER => 'categories ' ,
46- RelationConfig::TYPE => RelationConfig::TYPE_MANY_TO_ONE
47- ],
48- 'tags ' => [
49- RelationConfig::FOREIGN_MAPPER => 'tags ' ,
50- RelationConfig::TYPE => RelationConfig::TYPE_MANY_TO_MANY
51- ]
52- ]
53- ])));
54-
55- $ this ->orm ->register ('categories ' , Mapper::make ($ this ->orm , MapperConfig::fromArray ([
56- MapperConfig::TABLE => 'categories ' ,
57- MapperConfig::COLUMNS => ['id ' , 'name ' ],
58-
59- ])));
60-
61- $ this ->orm ->register ('tags ' , Mapper::make ($ this ->orm , MapperConfig::fromArray ([
62- MapperConfig::TABLE => 'tags ' ,
63- MapperConfig::COLUMNS => ['id ' , 'name ' ],
64-
65- ])));
66-
67- $ this ->orm ->register ('images ' , Mapper::make ($ this ->orm , MapperConfig::fromArray ([
68- MapperConfig::TABLE => 'images ' ,
69- MapperConfig::COLUMNS => ['id ' , 'path ' , 'imageable_id ' , 'imageable_type ' ],
70-
71- ])));
36+ require_once __DIR__ . '/definitions.php ' ;
37+
38+ $ orm = $ this ->orm ;
39+ $ this ->orm ->register ('products ' , function ($ orm ) {
40+ return new app \Mapper \ProductMapper ($ orm );
41+ });
42+ $ this ->orm ->register ('categories ' , function ($ orm ) {
43+ return new app \Mapper \CategoryMapper ($ orm );
44+ });
45+ $ this ->orm ->register ('tags ' , function ($ orm ) {
46+ return new app \Mapper \TagMapper ($ orm );
47+ });
48+ $ this ->orm ->register ('images ' , function ($ orm ) {
49+ return new app \Mapper \ImageMapper ($ orm );
50+ });
7251 }
7352
7453 function clearCache ()
@@ -88,7 +67,8 @@ function commit()
8867
8968 function insert ($ i )
9069 {
91- $ product = $ this ->orm ->get ('products ' )->newEntity ([
70+ $ mapper = $ this ->orm ->get ('products ' );
71+ $ product = $ mapper ->newEntity ([
9272 'name ' => 'Product # ' . $ i ,
9373 'sku ' => 'SKU # ' . $ i ,
9474 'price ' => sqrt (1000 + $ i * 100 ),
@@ -104,72 +84,79 @@ function insert($i)
10484 ]
10585 ]);
10686
107- $ this -> orm -> save (' products ' , $ product );
87+ $ mapper -> save ($ product , [ ' category ' , ' images ' , ' tags ' ] );
10888
109- $ this ->products [] = $ product ->id ;
89+ $ this ->products [] = $ product ->getId () ;
11090
11191 return $ product ;
11292 }
11393
11494 public function test_insert ()
11595 {
96+ $ mapper = $ this ->orm ->get ('products ' );
11697 $ product = $ this ->insert (0 );
117- $ product = $ this ->orm ->find ('products ' , $ product ->id );
98+ /** @var \app\Entity\Product $product */
99+ $ product = $ mapper ->find ($ product ->getId ());
118100 $ this ->assertNotNull ($ product , 'Product not found ' );
119- $ this ->assertNotNull ($ product ->category_id , 'Category was not associated with the product ' );
120- $ this ->assertNotNull ($ product ->images [ 0 ]-> path , 'Image not present ' );
121- $ this ->assertNotNull ($ product ->tags [ 0 ]-> name , 'Tag not present ' );
101+ $ this ->assertNotNull ($ product ->getCategoryId () , 'Category was not associated with the product ' );
102+ $ this ->assertNotNull ($ product ->getImages ()-> get ( 0 )-> getPath () , 'Image not present ' );
103+ $ this ->assertNotNull ($ product ->getTags ()-> get ( 0 )-> getName () , 'Tag not present ' );
122104 }
123105
124106 function prepare_update ()
125107 {
108+ $ mapper = $ this ->orm ->get ('products ' );
126109 $ this ->product = $ this ->insert (0 );
127- $ this ->product = $ this -> orm -> find (' products ' , 1 , ['category ' , 'images ' , 'tags ' ]);
110+ $ this ->product = $ mapper -> find (1 , ['category ' , 'images ' , 'tags ' ]);
128111 }
129112
130113 function update ($ i )
131114 {
132- $ this ->product ->name = 'New product name ' . $ i ;
133- $ this ->product ->category ->name = 'New category name ' . $ i ;
134- $ this ->product ->images [0 ]->path = 'new_path_ ' . $ i . '.jpg ' ;
135- $ this ->product ->tags [0 ]->name = 'New tag name ' . $ i ;
136- $ this ->orm ->save ('products ' , $ this ->product );
115+ $ mapper = $ this ->orm ->get ('products ' );
116+ $ this ->product ->setName ('New product name ' . $ i );
117+ $ this ->product ->getCategory ()->setName ('New category name ' . $ i );
118+ $ this ->product ->getImages ()->get (0 )->setPath ('new_path_ ' . $ i . '.jpg ' );
119+ $ this ->product ->getTags ()->get (0 )->setName ('New tag name ' . $ i );
120+ $ mapper ->save ($ this ->product , ['category ' , 'images ' , 'tags ' ]);
137121 }
138122
139123 function test_update ()
140124 {
141-
142- $ this ->product ->name = 'New product name ' ;
143- $ this ->product ->category -> name = 'New category name ' ;
144- $ this ->product ->images [ 0 ]-> path = 'new_path.jpg ' ;
145- $ this ->product ->tags [ 0 ]-> set ( ' name ' , 'New tag name ' );
146- $ this -> orm -> save (' products ' , $ this ->product );
147- $ product = $ this -> orm -> find (' products ' , 1 , ['category ' , 'tags ' , 'images ' ]);
148-
149- $ this ->assertEquals ('New product name ' , $ product ->name );
150- $ this ->assertEquals ('New category name ' , $ product ->get ( ' category ' )->name );
151- $ this ->assertEquals ('new_path.jpg ' , $ product ->images [ 0 ]-> path );
125+ $ mapper = $ this -> orm -> get ( ' products ' );
126+ $ this ->product ->setName ( 'New product name ' ) ;
127+ $ this ->product ->getCategory ()-> setName ( 'New category name ' ) ;
128+ $ this ->product ->getImages ()-> get ( 0 )-> setPath ( 'new_path.jpg ' ) ;
129+ $ this ->product ->getTags ()-> get ( 0 )-> setName ( 'New tag name ' );
130+ $ mapper -> save ($ this ->product , [ ' category ' , ' images ' , ' tags ' ] );
131+ $ product = $ mapper -> find (1 , ['category ' , 'tags ' , 'images ' ]);
132+
133+ $ this ->assertEquals ('New product name ' , $ product ->getName () );
134+ $ this ->assertEquals ('New category name ' , $ product ->getCategory ( )->getName () );
135+ $ this ->assertEquals ('new_path.jpg ' , $ product ->getImages ()-> get ( 0 )-> getPath () );
152136
153137 // order not preserved for some reason
154- $ this ->assertEquals ('New tag name ' , $ product ->tags [ 1 ]-> name );
155- $ this ->assertEquals ('Tag #t2_0 ' , $ product ->tags [ 0 ]-> name );
138+ $ this ->assertEquals ('New tag name ' , $ product ->getTags ()-> get ( 1 )-> getName () );
139+ $ this ->assertEquals ('Tag #t2_0 ' , $ product ->getTags ()-> get ( 0 )-> getName () );
156140 }
157141
158142 function find ($ i )
159143 {
160- $ this ->orm ->find ('products ' , 1 );
144+ $ mapper = $ this ->orm ->get ('products ' );
145+ $ mapper ->find (1 );
161146 }
162147
163148 function test_find ()
164149 {
165- $ product = $ this ->orm ->find ('products ' , 1 );
150+ $ mapper = $ this ->orm ->get ('products ' );
151+ $ product = $ mapper ->find (1 );
166152 $ lastRun = self ::NB_TEST - 1 ;
167- $ this ->assertEquals ('New product name ' . $ lastRun , $ product ->name ); // changed by "update"
153+ $ this ->assertEquals ('New product name ' . $ lastRun , $ product ->getName () ); // changed by "update"
168154 }
169155
170156 function complexQuery ($ i )
171157 {
172- $ this ->orm ->select ('products ' )
158+ $ this ->orm ->get ('products ' )
159+ ->newQuery ()
173160 ->join ('INNER ' , 'categories ' , 'categories.id = products.category_id ' )
174161 ->where ('products.id ' , 50 , '> ' )
175162 ->where ('categories.id ' , 300 , '< ' )
@@ -178,7 +165,8 @@ function complexQuery($i)
178165
179166 function test_complexQuery ()
180167 {
181- $ this ->assertEquals (249 , $ this ->orm ->select ('products ' )
168+ $ this ->assertEquals (249 , $ this ->orm ->get ('products ' )
169+ ->newQuery ()
182170 ->join ('INNER ' , 'categories ' , 'categories.id = products.category_id ' )
183171 ->where ('products.id ' , 50 , '> ' )
184172 ->where ('categories.id ' , 300 , '< ' )
@@ -187,7 +175,8 @@ function test_complexQuery()
187175
188176 function relations ($ i )
189177 {
190- $ products = $ this ->orm ->select ('products ' )
178+ $ products = $ this ->orm ->get ('products ' )
179+ ->newQuery ()
191180 ->load ('category ' , 'tags ' , 'images ' )
192181 ->where ('price ' , 50 , '> ' )
193182 ->limit (10 )
@@ -201,10 +190,10 @@ function test_relations()
201190 {
202191 $ product = $ this ->orm ->get ('products ' )->find (1 );
203192 $ lastRun = self ::NB_TEST - 1 ;
204- $ this ->assertEquals ('New product name ' . $ lastRun , $ product ->name );
205- $ this ->assertEquals ('New category name ' . $ lastRun , $ product ->category -> name );
206- $ this ->assertEquals ('new_path_ ' . $ lastRun . '.jpg ' , $ product ->images [ 0 ]-> path );
207- $ this ->assertEquals ('New tag name ' . $ lastRun , $ product ->tags [ 1 ]-> name );
208- $ this ->assertEquals ('Tag #t2_0 ' , $ product ->tags [ 0 ]-> name );
193+ $ this ->assertEquals ('New product name ' . $ lastRun , $ product ->getName () );
194+ $ this ->assertEquals ('New category name ' . $ lastRun , $ product ->getCategory ()-> getName () );
195+ $ this ->assertEquals ('new_path_ ' . $ lastRun . '.jpg ' , $ product ->getImages ()-> get ( 0 )-> getPath () );
196+ $ this ->assertEquals ('New tag name ' . $ lastRun , $ product ->getTags ()-> get ( 1 )-> getName () );
197+ $ this ->assertEquals ('Tag #t2_0 ' , $ product ->getTags ()-> get ( 0 )-> getName () );
209198 }
210- }
199+ }
0 commit comments