PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/database/Mvc/Model/FindCest.php

http://github.com/phalcon/cphalcon
PHP | 379 lines | 205 code | 59 blank | 115 comment | 0 complexity | 656a08aa68a0a64e849c222a301fbad0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the Phalcon Framework.
  4. *
  5. * (c) Phalcon Team <team@phalcon.io>
  6. *
  7. * For the full copyright and license information, please view the LICENSE.txt
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Phalcon\Test\Database\Mvc\Model;
  12. use DatabaseTester;
  13. use PDO;
  14. use Phalcon\Cache;
  15. use Phalcon\Cache\AdapterFactory;
  16. use Phalcon\Mvc\Model\Exception;
  17. use Phalcon\Storage\SerializerFactory;
  18. use Phalcon\Test\Fixtures\Migrations\AbstractMigration;
  19. use Phalcon\Test\Fixtures\Migrations\CustomersMigration;
  20. use Phalcon\Test\Fixtures\Migrations\InvoicesMigration;
  21. use Phalcon\Test\Fixtures\Migrations\ObjectsMigration;
  22. use Phalcon\Test\Fixtures\Traits\DiTrait;
  23. use Phalcon\Test\Models\Customers;
  24. use Phalcon\Test\Models\Invoices;
  25. use Phalcon\Test\Models\Objects;
  26. use function getOptionsRedis;
  27. use function outputDir;
  28. use function uniqid;
  29. /**
  30. * Class FindCest
  31. */
  32. class FindCest
  33. {
  34. use DiTrait;
  35. public function _before(DatabaseTester $I)
  36. {
  37. $this->setNewFactoryDefault();
  38. $this->setDatabase($I);
  39. }
  40. /**
  41. * Tests Phalcon\Mvc\Model :: find()
  42. *
  43. * @author Phalcon Team <team@phalcon.io>
  44. * @since 2020-02-01
  45. *
  46. * @group mysql
  47. * @group pgsql
  48. * @group sqlite
  49. */
  50. public function mvcModelFind(DatabaseTester $I)
  51. {
  52. $I->wantToTest('Mvc\Model - find()');
  53. /** @var PDO $connection */
  54. $connection = $I->getConnection();
  55. $migration = new ObjectsMigration($connection);
  56. $migration->insert(1, 'random data', 1);
  57. $data = Objects::find();
  58. $I->assertEquals(1, count($data));
  59. $record = $data[0];
  60. $I->assertEquals(1, $record->obj_id);
  61. $I->assertEquals('random data', $record->obj_name);
  62. }
  63. /**
  64. * Tests Phalcon\Mvc\Model :: find()
  65. *
  66. * @author Phalcon Team <team@phalcon.io>
  67. * @since 2020-02-01
  68. *
  69. * @group mysql
  70. * @group pgsql
  71. * @group sqlite
  72. */
  73. public function mvcModelFindWithCache(DatabaseTester $I)
  74. {
  75. $I->wantToTest('Mvc\Model - find() - with cache');
  76. $file = outputDir('data-/my/-c/ac/my-cache');
  77. $I->safeDeleteFile($file);
  78. /** @var PDO $connection */
  79. $connection = $I->getConnection();
  80. $migration = new ObjectsMigration($connection);
  81. $migration->insert(1, 'random data', 1);
  82. $options = [
  83. 'defaultSerializer' => 'Json',
  84. 'storageDir' => outputDir(),
  85. 'lifetime' => 172800,
  86. 'prefix' => 'data-',
  87. ];
  88. // Models Cache setup
  89. $serializerFactory = new SerializerFactory();
  90. $adapterFactory = new AdapterFactory($serializerFactory);
  91. $adapter = $adapterFactory->newInstance('stream', $options);
  92. $cache = new Cache($adapter);
  93. $this->container->setShared('modelsCache', $cache);
  94. /**
  95. * Get the records (should cache the resultset)
  96. */
  97. $data = Objects::find(
  98. [
  99. 'cache' => [
  100. 'key' => 'my-cache',
  101. ],
  102. ]
  103. );
  104. /**
  105. * See the file created
  106. */
  107. $I->seeFileFound($file);
  108. $I->assertEquals(1, count($data));
  109. $record = $data[0];
  110. $I->assertEquals(1, $record->obj_id);
  111. $I->assertEquals('random data', $record->obj_name);
  112. /**
  113. * Get the models cache
  114. */
  115. $modelsCache = $this->container->get('modelsCache');
  116. $exists = $modelsCache->has('my-cache');
  117. $I->assertTrue($exists);
  118. /**
  119. * Get the data now from the cache
  120. */
  121. $data = $modelsCache->get('my-cache');
  122. $I->assertEquals(1, count($data));
  123. $record = $data[0];
  124. $I->assertEquals(1, $record->obj_id);
  125. $I->assertEquals('random data', $record->obj_name);
  126. }
  127. /**
  128. * Tests Phalcon\Mvc\Model :: find() - second iteration of Resultset
  129. *
  130. * @author Phalcon Team <team@phalcon.io>
  131. * @since 2020-10-17
  132. *
  133. * @see https://github.com/phalcon/cphalcon/issues/15065
  134. *
  135. * @group mysql
  136. * @group pgsql
  137. * @group sqlite
  138. */
  139. public function mvcModelFindResultsetSecondIteration(DatabaseTester $I)
  140. {
  141. $I->wantToTest('Mvc\Model - find() - second iteration of Resultset');
  142. /** @var PDO $connection */
  143. $connection = $I->getConnection();
  144. $customersMigration = new CustomersMigration($connection);
  145. $customersMigration->clear();
  146. $customersMigration->insert(1, 1, uniqid('cust-', true), uniqid('cust-', true));
  147. $customersMigration->insert(2, 0, uniqid('cust-', true), uniqid('cust-', true));
  148. $customers = Customers::find();
  149. $I->assertCount(2, $customers);
  150. /**
  151. * First iteration
  152. *
  153. * @var Customers $customer
  154. */
  155. foreach ($customers as $customer) {
  156. $I->assertNotNull(
  157. $customer->getId()
  158. );
  159. $I->assertIsNumeric(
  160. $customer->getId()
  161. );
  162. }
  163. /**
  164. * Second iteration
  165. *
  166. * @var Customers $secondCustomer
  167. */
  168. foreach ($customers as $secondCustomer) {
  169. $I->assertNotNull(
  170. $secondCustomer->getId()
  171. );
  172. $I->assertIsNumeric(
  173. $secondCustomer->getId()
  174. );
  175. }
  176. }
  177. /**
  178. * Tests Phalcon\Mvc\Model :: find() - with cache/exception
  179. *
  180. * @author Phalcon Team <team@phalcon.io>
  181. * @since 2021-05-10
  182. *
  183. * @group mysql
  184. * @group pgsql
  185. * @group sqlite
  186. */
  187. public function mvcModelFindWithCacheException(DatabaseTester $I)
  188. {
  189. $I->wantToTest('Mvc\Model - find() - with cache - exception');
  190. $I->expectThrowable(
  191. new Exception(
  192. 'Cache service must be an object implementing Psr\SimpleCache\CacheInterface'
  193. ),
  194. function () {
  195. $options = [
  196. 'storageDir' => outputDir(),
  197. 'lifetime' => 172800,
  198. 'prefix' => 'data-',
  199. ];
  200. // Models Cache setup
  201. $serializerFactory = new SerializerFactory();
  202. $adapterFactory = new AdapterFactory($serializerFactory);
  203. $adapter = $adapterFactory->newInstance('stream', $options);
  204. $this->container->setShared('modelsCache', $adapter);
  205. Objects::find(
  206. [
  207. 'cache' => [
  208. 'key' => 'my-cache',
  209. ],
  210. ]
  211. );
  212. }
  213. );
  214. }
  215. /**
  216. * Tests Phalcon\Mvc\Model :: find() - private property with Redis cache
  217. *
  218. * @author Phalcon Team <team@phalcon.io>
  219. * @since 2021-05-25
  220. * @issue 15439
  221. *
  222. * @group mysql
  223. * @group pgsql
  224. * @group sqlite
  225. */
  226. public function mvcModelFindPrivatePropertyWithRedisCache(DatabaseTester $I)
  227. {
  228. $I->wantToTest('Mvc\Model - find() - private property with Redis cache');
  229. /** @var PDO $connection */
  230. $connection = $I->getConnection();
  231. $migration = new InvoicesMigration($connection);
  232. $migration->insert(1, 1, 1, 'Test', 101);
  233. $cacheKey = uniqid('redis-');
  234. /**
  235. * Find without models cache
  236. */
  237. /** @var Invoices $original */
  238. $original = Invoices::find(
  239. [
  240. 'conditions' => 'inv_id = :inv_id:',
  241. 'bind' => [
  242. 'inv_id' => 1,
  243. ],
  244. ]
  245. );
  246. $I->assertCount(1, $original);
  247. $record = $original[0];
  248. $actual = $record->getIsActive();
  249. $I->assertTrue($actual);
  250. // Models Cache setup
  251. $serializerFactory = new SerializerFactory();
  252. $adapterFactory = new AdapterFactory($serializerFactory);
  253. $adapter = $adapterFactory->newInstance('redis', getOptionsRedis());
  254. $cache = new Cache($adapter);
  255. $this->container->setShared('modelsCache', $cache);
  256. /**
  257. * Find it - so that we can use the models cache now
  258. */
  259. /** @var Invoices $cached */
  260. $cached = Invoices::find(
  261. [
  262. 'conditions' => 'inv_id = :inv_id:',
  263. 'bind' => [
  264. 'inv_id' => 1,
  265. ],
  266. 'cache' => [
  267. 'key' => $cacheKey,
  268. 'lifetime' => 60,
  269. ],
  270. ]
  271. );
  272. $I->assertCount(1, $cached);
  273. $record = $cached[0];
  274. $actual = $record->getIsActive();
  275. $I->assertTrue($actual);
  276. /**
  277. * Delete the record just in case to ensure we get it from the cache
  278. */
  279. $result = $original->delete();
  280. $I->assertNotFalse($result);
  281. /**
  282. * Ensure we do not have anything in the db
  283. */
  284. /** @var Invoices $original */
  285. $original = Invoices::find(
  286. [
  287. 'conditions' => 'inv_id = :inv_id:',
  288. 'bind' => [
  289. 'inv_id' => 1,
  290. ],
  291. ]
  292. );
  293. $I->assertCount(0, $original);
  294. /**
  295. * Finally get it back from the cache
  296. */
  297. /** @var Invoices $cached */
  298. $cached = Invoices::find(
  299. [
  300. 'conditions' => 'inv_id = :inv_id:',
  301. 'bind' => [
  302. 'inv_id' => 1,
  303. ],
  304. 'cache' => [
  305. 'key' => $cacheKey,
  306. 'lifetime' => 60,
  307. ],
  308. ]
  309. );
  310. $I->assertCount(1, $cached);
  311. $record = $cached[0];
  312. $actual = $record->getIsActive();
  313. $I->assertTrue($actual);
  314. /**
  315. * delete the cached entry
  316. */
  317. $result = $cache->delete($cacheKey);
  318. $I->assertTrue($result);
  319. }
  320. }