@@ -290,6 +290,97 @@ public function testUpdateRollback()
290290 $ this ->assertEquals ($ original , $ updated );
291291 }
292292
293+ public function testPrefixAndRemoveTableName ()
294+ {
295+ $ method = new \ReflectionMethod ('PicoMapper\Mapping ' , 'prefixTableNameTo ' );
296+ $ method ->setAccessible (true );
297+
298+ // Test with string input
299+ $ input = 'field ' ;
300+ $ expectedOutput = 'customers.field ' ;
301+ $ actualOutput = $ method ->invoke ($ this ->getMapping (), $ input );
302+ $ this ->assertEquals ($ expectedOutput , $ actualOutput );
303+
304+ // Test with array input
305+ $ input = ['field1 ' , 'field2 ' ];
306+ $ expectedOutput = ['customers.field1 ' , 'customers.field2 ' ];
307+ $ actualOutput = $ method ->invoke ($ this ->getMapping (), $ input );
308+ $ this ->assertEquals ($ expectedOutput , $ actualOutput );
309+
310+ // Test with nested array input
311+ $ input = ['field1 ' , ['nestedField1 ' , 'nestedField2 ' ]];
312+ $ expectedOutput = ['customers.field1 ' , ['customers.nestedField1 ' , 'customers.nestedField2 ' ]];
313+ $ actualOutput = $ method ->invoke ($ this ->getMapping (), $ input );
314+ $ this ->assertEquals ($ expectedOutput , $ actualOutput );
315+
316+ // Test multi-pass safety
317+ $ input = ['field1 ' , 'field2 ' ];
318+ $ expectedOutput = ['customers.field1 ' , 'customers.field2 ' ];
319+ $ actualOutput = $ method ->invoke ($ this ->getMapping (), $ method ->invoke ($ this ->getMapping (), $ input ));
320+ $ this ->assertEquals ($ expectedOutput , $ actualOutput );
321+
322+ // Don't change input if other table already appended
323+ $ input = 'table2.field ' ;
324+ $ expectedOutput = 'table2.field ' ;
325+ $ actualOutput = $ method ->invoke ($ this ->getMapping (), $ input );
326+ $ this ->assertEquals ($ expectedOutput , $ actualOutput );
327+ }
328+
329+ public function testFindOneWithDirectJoin ()
330+ {
331+ $ customer = $ this ->getMapping ()
332+ ->join ('orders ' , 'customer_id ' , 'id ' )
333+ ->eq ('customers.id ' , 2 )
334+ ->findOne ();
335+
336+ $ this ->assertEquals ('Jane Doe ' , $ customer ['name ' ]);
337+ $ this ->assertCount (1 , $ customer ['orders ' ]);
338+ $ this ->assertCount (2 , $ customer ['orders ' ][0 ]['items ' ]);
339+
340+ $ this ->assertEquals ('2018-01-02 ' , $ customer ['orders ' ][0 ]['date_created ' ]);
341+ $ this ->assertEquals ('Bread ' , $ customer ['orders ' ][0 ]['items ' ][0 ]['description ' ]);
342+ $ this ->assertEquals (120 , $ customer ['orders ' ][0 ]['items ' ][0 ]['amount ' ]);
343+ $ this ->assertEquals ('Yogurt ' , $ customer ['orders ' ][0 ]['items ' ][1 ]['description ' ]);
344+ $ this ->assertEquals (400 , $ customer ['orders ' ][0 ]['items ' ][1 ]['amount ' ]);
345+ }
346+
347+ public function testFindOneWithDirectLeft ()
348+ {
349+ $ customer = $ this ->getMapping ()
350+ ->left ('orders ' , 'o ' , 'customer_id ' , 'customers ' , 'id ' )
351+ ->eq ('customers.id ' , 2 )
352+ ->findOne ();
353+
354+ $ this ->assertEquals ('Jane Doe ' , $ customer ['name ' ]);
355+ $ this ->assertCount (1 , $ customer ['orders ' ]);
356+ $ this ->assertCount (2 , $ customer ['orders ' ][0 ]['items ' ]);
357+
358+ $ this ->assertEquals ('2018-01-02 ' , $ customer ['orders ' ][0 ]['date_created ' ]);
359+ $ this ->assertEquals ('Bread ' , $ customer ['orders ' ][0 ]['items ' ][0 ]['description ' ]);
360+ $ this ->assertEquals (120 , $ customer ['orders ' ][0 ]['items ' ][0 ]['amount ' ]);
361+ $ this ->assertEquals ('Yogurt ' , $ customer ['orders ' ][0 ]['items ' ][1 ]['description ' ]);
362+ $ this ->assertEquals (400 , $ customer ['orders ' ][0 ]['items ' ][1 ]['amount ' ]);
363+ }
364+
365+ public function testFindOneWithDirectAndSecondaryLeft ()
366+ {
367+ $ customer = $ this ->getMapping ()
368+ ->left ('orders ' , 'o ' , 'customer_id ' , 'customers ' , 'id ' )
369+ ->left ('items ' , 'i ' , 'order_id ' , 'o ' , 'id ' )
370+ ->eq ('customers.id ' , 2 )
371+ ->findOne ();
372+
373+ $ this ->assertEquals ('Jane Doe ' , $ customer ['name ' ]);
374+ $ this ->assertCount (1 , $ customer ['orders ' ]);
375+ $ this ->assertCount (2 , $ customer ['orders ' ][0 ]['items ' ]);
376+
377+ $ this ->assertEquals ('2018-01-02 ' , $ customer ['orders ' ][0 ]['date_created ' ]);
378+ $ this ->assertEquals ('Bread ' , $ customer ['orders ' ][0 ]['items ' ][0 ]['description ' ]);
379+ $ this ->assertEquals (120 , $ customer ['orders ' ][0 ]['items ' ][0 ]['amount ' ]);
380+ $ this ->assertEquals ('Yogurt ' , $ customer ['orders ' ][0 ]['items ' ][1 ]['description ' ]);
381+ $ this ->assertEquals (400 , $ customer ['orders ' ][0 ]['items ' ][1 ]['amount ' ]);
382+ }
383+
293384 /**
294385 * Returns a new mapping for testing.
295386 *
@@ -316,7 +407,8 @@ public function getMapping()
316407
317408 $ customer = (new Definition ('customers ' ))
318409 ->withColumns ('id ' , 'name ' )
319- ->withMany ($ order , 'orders ' , 'customer_id ' );
410+ ->withMany ($ order , 'orders ' , 'customer_id ' )
411+ ->withDeletionTimestamp ('date_deleted ' );
320412
321413 return new Mapping ($ this ->db , $ customer , [], [
322414 'inserted ' => [$ this ->hook ],
@@ -346,7 +438,8 @@ public function getReadOnlyMapping()
346438
347439 $ customer = (new Definition ('customers ' ))
348440 ->withColumns ('id ' , 'name ' )
349- ->withMany ($ order , 'orders ' , 'customer_id ' );
441+ ->withMany ($ order , 'orders ' , 'customer_id ' )
442+ ->withDeletionTimestamp ('date_deleted ' );
350443
351444 return new Mapping ($ this ->db , $ customer );
352445 }
0 commit comments