1
1
<?php
2
2
namespace Gt \Database \Test \Query ;
3
3
4
+ use DateTime ;
4
5
use Gt \Database \Connection \DefaultSettings ;
5
6
use Gt \Database \Connection \Driver ;
6
7
use Gt \Database \Query \PhpQuery ;
14
15
use PHPUnit \Framework \TestCase ;
15
16
16
17
class QueryCollectionTest extends TestCase {
17
- /** @var QueryCollection */
18
- private $ queryCollection ;
19
- /** @var MockObject|Query */
20
- private $ mockQuery ;
21
-
22
- protected function setUp ():void {
23
- /** @var MockObject|QueryFactory $mockQueryFactory */
24
- $ mockQueryFactory = $ this ->createMock (QueryFactory::class);
25
- $ this ->mockQuery = $ this ->createMock (Query::class);
26
- $ mockQueryFactory
18
+ public function testQueryCollectionQuery () {
19
+ $ queryFactory = $ this ->createMock (QueryFactory::class);
20
+ $ query = $ this ->createMock (Query::class);
21
+ $ queryFactory
27
22
->expects (static ::once ())
28
23
->method ("create " )
29
24
->with ("something " )
30
- ->willReturn ($ this -> mockQuery );
25
+ ->willReturn ($ query );
31
26
32
- $ this -> queryCollection = new QueryCollectionDirectory (
27
+ $ queryCollection = new QueryCollectionDirectory (
33
28
__DIR__ ,
34
29
new Driver (new DefaultSettings ()),
35
- $ mockQueryFactory
30
+ $ queryFactory
36
31
);
37
- }
38
32
39
- public function testQueryCollectionQuery () {
40
33
$ placeholderVars = ["nombre " => "hombre " ];
41
- $ this -> mockQuery
34
+ $ query
42
35
->expects (static ::once ())
43
36
->method ("execute " )
44
37
->with ([$ placeholderVars ]);
45
38
46
- $ resultSet = $ this -> queryCollection ->query (
39
+ $ resultSet = $ queryCollection ->query (
47
40
"something " ,
48
41
$ placeholderVars
49
42
);
@@ -55,9 +48,23 @@ public function testQueryCollectionQuery() {
55
48
}
56
49
57
50
public function testQueryCollectionQueryNoParams () {
58
- $ this ->mockQuery ->expects (static ::once ())->method ("execute " )->with ();
51
+ $ queryFactory = $ this ->createMock (QueryFactory::class);
52
+ $ query = $ this ->createMock (Query::class);
53
+ $ queryFactory
54
+ ->expects (static ::once ())
55
+ ->method ("create " )
56
+ ->with ("something " )
57
+ ->willReturn ($ query );
58
+
59
+ $ queryCollection = new QueryCollectionDirectory (
60
+ __DIR__ ,
61
+ new Driver (new DefaultSettings ()),
62
+ $ queryFactory
63
+ );
64
+
65
+ $ query ->expects (static ::once ())->method ("execute " )->with ();
59
66
60
- $ resultSet = $ this -> queryCollection ->query ("something " );
67
+ $ resultSet = $ queryCollection ->query ("something " );
61
68
62
69
static ::assertInstanceOf (
63
70
ResultSet::class,
@@ -66,28 +73,56 @@ public function testQueryCollectionQueryNoParams() {
66
73
}
67
74
68
75
public function testQueryShorthand () {
76
+ $ queryFactory = $ this ->createMock (QueryFactory::class);
77
+ $ query = $ this ->createMock (Query::class);
78
+ $ queryFactory
79
+ ->expects (static ::once ())
80
+ ->method ("create " )
81
+ ->with ("something " )
82
+ ->willReturn ($ query );
83
+
84
+ $ queryCollection = new QueryCollectionDirectory (
85
+ __DIR__ ,
86
+ new Driver (new DefaultSettings ()),
87
+ $ queryFactory
88
+ );
89
+
69
90
$ placeholderVars = ["nombre " => "hombre " ];
70
- $ this -> mockQuery
91
+ $ query
71
92
->expects (static ::once ())
72
93
->method ("execute " )
73
94
->with ([$ placeholderVars ]);
74
95
75
96
static ::assertInstanceOf (
76
97
ResultSet::class,
77
- $ this -> queryCollection ->something ($ placeholderVars )
98
+ $ queryCollection ->something ($ placeholderVars )
78
99
);
79
100
}
80
101
81
102
public function testQueryShorthandNoParams () {
82
- $ this ->mockQuery ->expects (static ::once ())->method ("execute " )->with ();
103
+ $ queryFactory = $ this ->createMock (QueryFactory::class);
104
+ $ query = $ this ->createMock (Query::class);
105
+ $ queryFactory
106
+ ->expects (static ::once ())
107
+ ->method ("create " )
108
+ ->with ("something " )
109
+ ->willReturn ($ query );
110
+
111
+ $ queryCollection = new QueryCollectionDirectory (
112
+ __DIR__ ,
113
+ new Driver (new DefaultSettings ()),
114
+ $ queryFactory
115
+ );
116
+
117
+ $ query ->expects (static ::once ())->method ("execute " )->with ();
83
118
84
119
static ::assertInstanceOf (
85
120
ResultSet::class,
86
- $ this -> queryCollection ->something ()
121
+ $ queryCollection ->something ()
87
122
);
88
123
}
89
124
90
- public function testQueryCollectionClass () {
125
+ public function testQueryCollectionClass_defaultNamespace () {
91
126
$ projectDir = implode (DIRECTORY_SEPARATOR , [
92
127
sys_get_temp_dir (),
93
128
"phpgt " ,
@@ -96,20 +131,83 @@ public function testQueryCollectionClass() {
96
131
]);
97
132
$ baseQueryDirectory = implode (DIRECTORY_SEPARATOR , [
98
133
$ projectDir ,
99
- "query " ,
134
+ "resultSet " ,
100
135
]);
101
136
$ queryCollectionClassPath = "$ baseQueryDirectory/Example.php " ;
102
137
if (!is_dir ($ baseQueryDirectory )) {
103
138
mkdir ($ baseQueryDirectory , recursive: true );
104
139
}
105
- touch ($ queryCollectionClassPath );
140
+ $ php = <<<PHP
141
+ <?php
142
+ namespace App\Query;
143
+
144
+ class Example {
145
+ public function getTimestamp():string {
146
+ return "select current_timestamp";
147
+ }
148
+ }
149
+ PHP ;
150
+
151
+ file_put_contents ($ queryCollectionClassPath , $ php );
106
152
107
153
$ sut = new QueryCollectionClass (
108
154
$ queryCollectionClassPath ,
109
155
new Driver (new DefaultSettings ()),
110
156
);
111
157
112
- $ query = $ sut ->query ("getTimestamp " );
113
- self ::assertInstanceOf (PhpQuery::class, $ query );
158
+ $ resultSet = $ sut ->query ("getTimestamp " );
159
+ self ::assertInstanceOf (ResultSet::class, $ resultSet );
160
+ $ row = $ resultSet ->fetch ();
161
+ $ actualDateTime = $ row ->getDateTime ("current_timestamp " );
162
+ $ expectedDateTime = new DateTime ();
163
+ self ::assertSame (
164
+ $ expectedDateTime ->format ("Y-m-d H:i " ),
165
+ $ actualDateTime ->format ("Y-m-d H:i " ),
166
+ );
167
+ }
168
+
169
+ public function testQueryCollectionClass_namespace () {
170
+ $ projectDir = implode (DIRECTORY_SEPARATOR , [
171
+ sys_get_temp_dir (),
172
+ "phpgt " ,
173
+ "database " ,
174
+ uniqid (),
175
+ ]);
176
+ $ baseQueryDirectory = implode (DIRECTORY_SEPARATOR , [
177
+ $ projectDir ,
178
+ "resultSet " ,
179
+ ]);
180
+ $ queryCollectionClassPath = "$ baseQueryDirectory/Example.php " ;
181
+ if (!is_dir ($ baseQueryDirectory )) {
182
+ mkdir ($ baseQueryDirectory , recursive: true );
183
+ }
184
+ $ php = <<<PHP
185
+ <?php
186
+ namespace GtTest\DatabaseExample;
187
+
188
+ class Example {
189
+ public function getTimestamp():string {
190
+ return "select current_timestamp";
191
+ }
192
+ }
193
+ PHP ;
194
+
195
+ file_put_contents ($ queryCollectionClassPath , $ php );
196
+
197
+ $ sut = new QueryCollectionClass (
198
+ $ queryCollectionClassPath ,
199
+ new Driver (new DefaultSettings ()),
200
+ );
201
+ $ sut ->setAppNamespace ("GtTest \\DatabaseExample " );
202
+
203
+ $ resultSet = $ sut ->query ("getTimestamp " );
204
+ self ::assertInstanceOf (ResultSet::class, $ resultSet );
205
+ $ row = $ resultSet ->fetch ();
206
+ $ actualDateTime = $ row ->getDateTime ("current_timestamp " );
207
+ $ expectedDateTime = new DateTime ();
208
+ self ::assertSame (
209
+ $ expectedDateTime ->format ("Y-m-d H:i " ),
210
+ $ actualDateTime ->format ("Y-m-d H:i " ),
211
+ );
114
212
}
115
213
}
0 commit comments