| Conditions | 1 |
| Paths | 1 |
| Total Lines | 86 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 94 | public function testQueryCacheWithQuery() |
||
| 95 | { |
||
| 96 | $db = $this->getConnectionWithData(); |
||
| 97 | |||
| 98 | $db->queryCacheEnable(true); |
||
| 99 | $query = $this->getQuery($db)->select(['name'])->from('customer'); |
||
| 100 | $update = $db->createCommand( |
||
| 101 | <<<SQL |
||
| 102 | UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id |
||
| 103 | SQL, |
||
| 104 | ); |
||
| 105 | |||
| 106 | $this->assertSame('user1', $query->where(['id' => 1])->scalar(), 'Asserting initial value'); |
||
| 107 | |||
| 108 | /* No cache */ |
||
| 109 | $update->bindValues([':id' => 1, ':name' => 'user11'])->execute(); |
||
| 110 | |||
| 111 | $this->assertSame( |
||
| 112 | 'user11', |
||
| 113 | $query->where(['id' => 1])->scalar(), |
||
| 114 | 'Query reflects DB changes when caching is disabled' |
||
| 115 | ); |
||
| 116 | |||
| 117 | /* Connection cache */ |
||
| 118 | $db->cache(function (ConnectionPDOInterface $db) use ($query, $update) { |
||
| 119 | $this->assertSame('user2', $query->where(['id' => 2])->scalar(), 'Asserting initial value for user #2'); |
||
| 120 | |||
| 121 | $update->bindValues([':id' => 2, ':name' => 'user22'])->execute(); |
||
| 122 | |||
| 123 | $this->assertSame( |
||
| 124 | 'user2', |
||
| 125 | $query->where(['id' => 2])->scalar(), |
||
| 126 | 'Query does NOT reflect DB changes when wrapped in connection caching' |
||
| 127 | ); |
||
| 128 | |||
| 129 | $db->noCache(function () use ($query) { |
||
| 130 | $this->assertSame( |
||
| 131 | 'user22', |
||
| 132 | $query->where(['id' => 2])->scalar(), |
||
| 133 | 'Query reflects DB changes when wrapped in connection caching and noCache simultaneously' |
||
| 134 | ); |
||
| 135 | }); |
||
| 136 | |||
| 137 | $this->assertSame( |
||
| 138 | 'user2', |
||
| 139 | $query->where(['id' => 2])->scalar(), |
||
| 140 | 'Cache does not get changes after getting newer data from DB in noCache block.' |
||
| 141 | ); |
||
| 142 | }, 10); |
||
| 143 | |||
| 144 | $db->queryCacheEnable(false); |
||
| 145 | |||
| 146 | $db->cache(function () use ($query, $update) { |
||
| 147 | $this->assertSame( |
||
| 148 | 'user22', |
||
| 149 | $query->where(['id' => 2])->scalar(), |
||
| 150 | 'When cache is disabled for the whole connection, Query inside cache block does not get cached' |
||
| 151 | ); |
||
| 152 | |||
| 153 | $update->bindValues([':id' => 2, ':name' => 'user2'])->execute(); |
||
| 154 | |||
| 155 | $this->assertSame('user2', $query->where(['id' => 2])->scalar()); |
||
| 156 | }, 10); |
||
| 157 | |||
| 158 | $db->queryCacheEnable(true); |
||
| 159 | $query->cache(); |
||
| 160 | |||
| 161 | $this->assertSame('user11', $query->where(['id' => 1])->scalar()); |
||
| 162 | |||
| 163 | $update->bindValues([':id' => 1, ':name' => 'user1'])->execute(); |
||
| 164 | |||
| 165 | $this->assertSame( |
||
| 166 | 'user11', |
||
| 167 | $query->where(['id' => 1])->scalar(), |
||
| 168 | 'When both Connection and Query have cache enabled, we get cached value' |
||
| 169 | ); |
||
| 170 | $this->assertSame( |
||
| 171 | 'user1', |
||
| 172 | $query->noCache()->where(['id' => 1])->scalar(), |
||
| 173 | 'When Query has disabled cache, we get actual data' |
||
| 174 | ); |
||
| 175 | |||
| 176 | $db->cache(function () use ($query) { |
||
| 177 | $this->assertSame('user1', $query->noCache()->where(['id' => 1])->scalar()); |
||
| 178 | $this->assertSame('user11', $query->cache()->where(['id' => 1])->scalar()); |
||
| 179 | }, 10); |
||
| 180 | } |
||
| 182 |