1
1
<?php
2
2
namespace JeremyHarris \LazyLoad \Test \TestCase \ORM ;
3
3
4
+ use Cake \Datasource \ConnectionManager ;
4
5
use Cake \Datasource \EntityInterface ;
6
+ use Cake \Log \Log ;
5
7
use Cake \ORM \Entity ;
6
8
use Cake \ORM \Locator \LocatorAwareTrait ;
7
9
use Cake \TestSuite \TestCase ;
17
19
*/
18
20
class LazyLoadEntityTraitTest extends TestCase
19
21
{
20
-
21
22
use LocatorAwareTrait;
22
23
23
24
/**
@@ -43,6 +44,8 @@ public function setUp(): void
43
44
{
44
45
parent ::setUp ();
45
46
47
+ $ this ->connection = ConnectionManager::get ('test ' );
48
+
46
49
$ this ->Articles = $ this ->getTableLocator ()->get ('Articles ' );
47
50
$ this ->Articles ->setEntityClass (LazyLoadableEntity::class);
48
51
$ this ->Articles ->belongsTo ('Authors ' );
@@ -97,6 +100,35 @@ public function testMissingPrimaryKey()
97
100
$ this ->assertNull ($ comment ->author );
98
101
}
99
102
103
+ /**
104
+ * tests that no db queries are performed if there's no association
105
+ *
106
+ * @return void
107
+ */
108
+ public function testNoDbQueriesOnEmptyAssociation ()
109
+ {
110
+ Log::setConfig ('queries ' , ['className ' => 'Array ' ]);
111
+ $ log = Log::engine ('queries ' );
112
+
113
+ $ this ->connection ->enableQueryLogging ();
114
+
115
+ $ article = $ this ->Articles ->newEntity ([
116
+ 'title ' => 'Article with no author ' ,
117
+ 'body ' => 'Article content ' ,
118
+ ]);
119
+
120
+ // Force a database query
121
+ $ this ->getTableLocator ()->get ('Authors ' )->getSchema ();
122
+
123
+ $ initialQueries = count ($ log ->read ());
124
+ $ this ->assertFalse (isset ($ article ->author ));
125
+ $ finalQueries = count ($ log ->read ());
126
+
127
+ $ this ->connection ->disableQueryLogging ();
128
+
129
+ $ this ->assertEquals ($ initialQueries , $ finalQueries );
130
+ }
131
+
100
132
/**
101
133
* tests that we can override _repository to prevent errors from being thrown
102
134
* in cases where we're creating an entity without a table. this happens in
@@ -166,7 +198,7 @@ public function testUnsetProperty()
166
198
->will ($ this ->returnValue ($ this ->Comments ));
167
199
168
200
$ this ->assertInstanceOf (EntityInterface::class, $ comment ->author );
169
- $ comment ->unsetProperty ('author ' );
201
+ $ comment ->unset ('author ' );
170
202
$ this ->assertNull ($ comment ->author );
171
203
172
204
// test re-setting a previously un-set prop
@@ -225,7 +257,7 @@ public function testUnsetEagerLoadedProperty()
225
257
->first ();
226
258
227
259
$ this ->assertInstanceOf (EntityInterface::class, $ comment ->author );
228
- $ comment ->unsetProperty ('author ' );
260
+ $ comment ->unset ('author ' );
229
261
$ this ->assertNull ($ comment ->author );
230
262
}
231
263
0 commit comments