1
+ <?php
2
+
3
+ namespace Simply \Database ;
4
+
5
+ use Simply \Database \Connection \MySqlConnection ;
6
+ use Simply \Database \Test \MockPdoStatement ;
7
+ use Simply \Database \Test \TestCase \UnitTestCase ;
8
+ use Simply \Database \Test \TestHouseModel ;
9
+
10
+ /**
11
+ * QueryTest.
12
+ * @author Riikka Kalliomäki <[email protected] >
13
+ * @copyright Copyright (c) 2018 Riikka Kalliomäki
14
+ * @license http://opensource.org/licenses/mit-license.php MIT License
15
+ */
16
+ class QueryTest extends UnitTestCase
17
+ {
18
+ public function testGenerateFromInvalidSchema (): void
19
+ {
20
+ $ query = $ this ->getQuery ('' , []);
21
+
22
+ $ query = $ query ->withSchema ($ this ->getPersonSchema (), 'person ' );
23
+ $ query = $ query ->withoutSchemas ();
24
+
25
+ $ this ->expectException (\InvalidArgumentException::class);
26
+ $ query ->fetchModels ('person ' );
27
+ }
28
+
29
+ public function testGenerateFromOneSchema (): void
30
+ {
31
+ $ data = [['h_id ' => 1 , 'h_street ' => 'Street Name ' ]];
32
+ $ query = $ this ->getQuery ('SELECT {h.fields} FROM {h.table} ' , $ data )
33
+ ->withSchema ($ this ->getPersonSchema ()->getRelationship ('home ' )->getReferencedSchema (), 'h ' );
34
+
35
+ /** @var TestHouseModel[] $houses */
36
+ $ houses = $ query ->fetchModels ();
37
+
38
+ $ this ->assertCount (1 , $ houses );
39
+ $ this ->assertSame ('Street Name ' , $ houses [0 ]->getStreet ());
40
+ }
41
+
42
+ public function testFetchCallback (): void
43
+ {
44
+ $ data = [['age ' => '5 ' ], ['age ' => '3 ' ]];
45
+
46
+ $ ages = $ this ->getQuery ('SELECT age FROM {table} ' , $ data )
47
+ ->withSchema ($ this ->getPersonSchema ())
48
+ ->fetchCallback (function (array $ row ): int {
49
+ return $ row ['age ' ];
50
+ });
51
+
52
+ $ this ->assertSame ([5 , 3 ], $ ages );
53
+ }
54
+
55
+ public function testNoParametersPassed (): void
56
+ {
57
+ $ schema = $ this ->getPersonSchema ();
58
+
59
+ $ connection = $ this ->getMockBuilder (MySqlConnection::class)
60
+ ->disableOriginalConstructor ()
61
+ ->setMethods (['query ' ])
62
+ ->getMock ();
63
+ $ connection ->expects ($ this ->once ())
64
+ ->method ('query ' )
65
+ ->with ("SELECT age FROM ` {$ schema ->getTable ()}` " , [])
66
+ ->willReturn ($ this ->getMockStatement ([]));
67
+
68
+ $ query = new Query ($ connection , 'SELECT age FROM {table} ' );
69
+ $ query = $ query ->withSchema ($ schema );
70
+ $ query = $ query ->withParameters (['name ' => 'value ' ]);
71
+ $ query = $ query ->withoutParameters ();
72
+
73
+ $ this ->assertCount (0 , $ query ->fetchRows ());
74
+ }
75
+
76
+ private function getQuery (string $ sql , array $ rows ): Query
77
+ {
78
+ $ connection = $ this ->getMockBuilder (MySqlConnection::class)
79
+ ->disableOriginalConstructor ()
80
+ ->setMethods (['query ' ])
81
+ ->getMock ();
82
+ $ connection ->expects ($ this ->atMost (1 ))->method ('query ' )->willReturn ($ this ->getMockStatement ($ rows ));
83
+
84
+ return new Query ($ connection , $ sql );
85
+ }
86
+
87
+ public function getMockStatement (array $ rows ): \PDOStatement
88
+ {
89
+ $ statement = $ this ->getMockBuilder (MockPdoStatement::class)
90
+ ->disableOriginalConstructor ()
91
+ ->getMock ();
92
+
93
+ $ statement ->expects ($ this ->atMost (1 ))->method ('getIterator ' )->willReturn (new \ArrayIterator ($ rows ));
94
+
95
+ return $ statement ;
96
+ }
97
+ }
0 commit comments