1<?php
2
3/**
4 * This file is part of the Phalcon Framework.
5 *
6 * (c) Phalcon Team <team@phalcon.io>
7 *
8 * For the full copyright and license information, please view the LICENSE.txt
9 * file that was distributed with this source code.
10 */
11
12declare(strict_types=1);
13
14namespace Phalcon\Test\Integration\Mvc\Model\Query;
15
16use Codeception\Example;
17use IntegrationTester;
18use Phalcon\Cache;
19use Phalcon\Cache\AdapterFactory;
20use Phalcon\Storage\SerializerFactory;
21use Phalcon\Test\Fixtures\Traits\DiTrait;
22use Phalcon\Test\Models\Robots;
23
24/**
25 * Class CacheCest
26 */
27class CacheCest
28{
29    use DiTrait;
30
31    public function _before(IntegrationTester $I)
32    {
33        $this->setNewFactoryDefault();
34        $this->setDiMysql();
35    }
36
37    public function _after(IntegrationTester $I)
38    {
39        $this->container['db']->close();
40    }
41
42    /**
43     * Tests Phalcon\Mvc\Model\Query :: cache()
44     *
45     * @dataProvider getValidSerializers
46     *
47     * @author       Phalcon Team <team@phalcon.io>
48     * @since        2019-09-27
49     */
50    public function mvcModelQueryCache(IntegrationTester $I, Example $serializer)
51    {
52        $I->wantToTest('Mvc\Model\Query - cache() - ' . $serializer[0]);
53
54        $di = $this->getDi();
55
56        // services: modelsCache
57        $serializerFactory = new SerializerFactory();
58        $adapterFactory    = new AdapterFactory($serializerFactory);
59
60        $options = [
61            'defaultSerializer' => $serializer[0],
62            'lifetime'          => 30,
63            'storageDir'        => cacheDir('mvcModelQueryCache'),
64        ];
65
66        $adapter = $adapterFactory->newInstance('stream', $options);
67        $cache   = new Cache($adapter);
68        $di->set('modelsCache', $cache);
69
70        $cacheKey         = 'uniqkey' . $serializer[0];
71        $options['cache'] = [
72            'key'      => $cacheKey,
73            'lifetime' => 50,
74        ];
75
76        $result         = Robots::find($options);
77        $numberOfRobots = $result->count();
78
79        // Create a temporary robot to test if the count is cached or fresh
80        $newrobot           = new Robots();
81        $newrobot->name     = 'Not cached robot';
82        $newrobot->type     = 'notcached';
83        $newrobot->year     = 2014;
84        $newrobot->datetime = '2015-03-05 04:16:17';
85        $newrobot->text     = 'Not cached robot';
86
87        $newrobot->create();
88
89        $result = Robots::find($options);
90        $I->assertEquals($numberOfRobots, $result->count());
91
92        // Delete the temp robot
93        Robots::findFirst("type = 'notcached'")->delete();
94
95        // Test delete robot isn't affecting cache
96        $result = Robots::find($options);
97        $I->assertNotFalse($result);
98        $I->assertEquals($numberOfRobots, $result->count());
99
100        $cache->delete($cacheKey);
101    }
102
103    /**
104     * Tests Phalcon\Mvc\Model\Query :: cache()
105     *
106     * @dataProvider getValidSerializers
107     *
108     * @author       Phalcon Team <team@phalcon.io>
109     * @since        2019-09-28
110     */
111    public function mvcModelQueryCacheCount(IntegrationTester $I, Example $serializer)
112    {
113        $I->wantToTest('Mvc\Model\Query - cache() - ' . $serializer[0]);
114
115        $di = $this->getDi();
116
117        // services: modelsCache
118        $serializerFactory = new SerializerFactory();
119        $adapterFactory    = new AdapterFactory($serializerFactory);
120
121        $options = [
122            'defaultSerializer' => $serializer[0],
123            'lifetime'          => 30,
124            'storageDir'        => cacheDir('mvcModelQueryCache'),
125        ];
126
127        $adapter = $adapterFactory->newInstance('stream', $options);
128        $cache   = new Cache($adapter);
129        $di->set('modelsCache', $cache);
130
131        $cacheKey         = 'uniqkey' . $serializer[0];
132        $options['cache'] = [
133            'key'      => $cacheKey,
134            'lifetime' => 50,
135        ];
136
137        $result1 = Robots::count($options);
138
139        // Create a temporary robot to test if the count is cached or fresh
140        $newrobot           = new Robots();
141        $newrobot->name     = 'Not cached robot';
142        $newrobot->type     = 'notcached';
143        $newrobot->year     = 2014;
144        $newrobot->datetime = '2015-03-05 04:16:17';
145        $newrobot->text     = 'Not cached robot';
146
147        $newrobot->create();
148
149        $result2 = Robots::count($options);
150        $I->assertEquals($result1, $result2);
151
152        // Delete the temp robot
153        Robots::findFirst("type = 'notcached'")->delete();
154
155        // Test delete robot isn't affecting cache
156        $result2 = Robots::count($options);
157        $I->assertNotFalse($result2);
158        $I->assertEquals($result1, $result2);
159
160        $cache->delete($cacheKey);
161    }
162
163    private function getValidSerializers(): array
164    {
165        return [
166            [
167                'Igbinary',
168            ],
169            [
170                'Msgpack',
171            ],
172            [
173                'Php',
174            ],
175        ];
176    }
177}
178