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\Database\Mvc\Model;
15
16use Codeception\Example;
17use DatabaseTester;
18use Phalcon\Mvc\Model;
19use Phalcon\Mvc\Model\Exception;
20use Phalcon\Mvc\Model\Row;
21use Phalcon\Test\Fixtures\Migrations\InvoicesMigration;
22use Phalcon\Test\Fixtures\Migrations\StringPrimaryMigration;
23use Phalcon\Test\Fixtures\Traits\DiTrait;
24use Phalcon\Test\Models\Invoices;
25use Phalcon\Test\Models\InvoicesExtended;
26use Phalcon\Test\Models\InvoicesMap;
27use Phalcon\Test\Models\ModelWithStringPrimary;
28
29use function uniqid;
30
31/**
32 * Class FindFirstCest
33 */
34class FindFirstCest
35{
36    use DiTrait;
37
38    public function _before(DatabaseTester $I)
39    {
40        $this->setNewFactoryDefault();
41        $this->setDatabase($I);
42
43        /** @var PDO $connection */
44        $connection = $I->getConnection();
45        (new InvoicesMigration($connection));
46    }
47
48    /**
49     * Tests Phalcon\Mvc\Model :: findFirst()
50     *
51     * @author Phalcon Team <team@phalcon.io>
52     * @since  2020-02-01
53     *
54     * @group  mysql
55     * @group  pgsql
56     * @group  sqlite
57     */
58    public function mvcModelFindFirst(DatabaseTester $I)
59    {
60        $I->wantToTest('Mvc\Model - findFirst()');
61
62        $title = uniqid('inv-');
63        /** @var PDO $connection */
64        $connection = $I->getConnection();
65        $migration  = new InvoicesMigration($connection);
66        $migration->insert(4, null, 0, $title);
67
68        $invoice = Invoices::findFirst();
69
70        $I->assertInstanceOf(
71            Invoices::class,
72            $invoice
73        );
74
75        $I->assertEquals(
76            4,
77            $invoice->inv_id
78        );
79
80        $invoice = Invoices::findFirst(null);
81
82        $I->assertInstanceOf(
83            Invoices::class,
84            $invoice
85        );
86
87        $I->assertEquals(
88            4,
89            $invoice->inv_id
90        );
91    }
92
93    /**
94     * Tests Phalcon\Mvc\Model :: findFirst()
95     *
96     * @author Phalcon Team <team@phalcon.io>
97     * @since  2020-02-01
98     *
99     * @group  mysql
100     * @group  pgsql
101     * @group  sqlite
102     */
103    public function mvcModelFindFirstColumnMap(DatabaseTester $I)
104    {
105        $I->wantToTest('Mvc\Model - findFirst() - with column map');
106
107        $title = uniqid('inv-');
108        /** @var PDO $connection */
109        $connection = $I->getConnection();
110        $migration  = new InvoicesMigration($connection);
111        $migration->insert(4, null, 0, $title);
112
113        $invoice = InvoicesMap::findFirst();
114
115        $I->assertInstanceOf(
116            InvoicesMap::class,
117            $invoice
118        );
119
120        $I->assertEquals(
121            4,
122            $invoice->id
123        );
124
125        $I->assertEquals(
126            $title,
127            $invoice->title
128        );
129
130        $invoice = InvoicesMap::findFirst(null);
131
132        $I->assertInstanceOf(
133            InvoicesMap::class,
134            $invoice
135        );
136
137        $I->assertEquals(
138            4,
139            $invoice->id
140        );
141
142        $I->assertEquals(
143            $title,
144            $invoice->title
145        );
146    }
147
148    /**
149     * Tests Phalcon\Mvc\Model :: findFirst() - not found
150     *
151     * @author Phalcon Team <team@phalcon.io>
152     * @since  2020-02-01
153     *
154     * @group  mysql
155     * @group  pgsql
156     * @group  sqlite
157     */
158    public function mvcModelFindFirstNotFound(DatabaseTester $I)
159    {
160        $I->wantToTest('Mvc\Model - findFirst() - not found');
161
162        $robot = Invoices::findFirst(
163            [
164                'conditions' => 'inv_id < 0',
165            ]
166        );
167
168        $I->assertNull($robot);
169    }
170
171    /**
172     * Tests Phalcon\Mvc\Model :: findFirstBy() - not found
173     *
174     * @author Phalcon Team <team@phalcon.io>
175     * @since  2020-02-01
176     *
177     * @group  mysql
178     * @group  pgsql
179     * @group  sqlite
180     */
181    public function mvcModelFindFirstByNotFound(DatabaseTester $I)
182    {
183        $I->wantToTest('Mvc\Model - findFirstBy() - not found');
184
185        $I->assertNull(
186            Invoices::findFirstByInvTitle('unknown')
187        );
188    }
189
190    /**
191     * Tests Phalcon\Mvc\Model :: findFirst() - extended
192     *
193     * @author Phalcon Team <team@phalcon.io>
194     * @since  2020-02-01
195     *
196     * @group  mysql
197     * @group  pgsql
198     * @group  sqlite
199     */
200    public function mvcModelFindFirstExtended(DatabaseTester $I)
201    {
202        $I->wantToTest('Mvc\Model - findFirst() - extended');
203
204        $title = uniqid('inv-');
205        /** @var PDO $connection */
206        $connection = $I->getConnection();
207        $migration  = new InvoicesMigration($connection);
208        $migration->insert(4, null, 0, $title);
209
210        $invoice = InvoicesExtended::findFirst(4);
211
212        $I->assertInstanceOf(
213            InvoicesExtended::class,
214            $invoice
215        );
216
217        $I->assertEquals(4, $invoice->inv_id);
218
219        $invoice = InvoicesExtended::findFirst(0);
220        $I->assertNull($invoice);
221    }
222
223    /**
224     * Tests Phalcon\Mvc\Model :: findFirst() - exception
225     *
226     * @author Phalcon Team <team@phalcon.io>
227     * @since  2020-02-01
228     *
229     * @group  mysql
230     * @group  pgsql
231     * @group  sqlite
232     */
233    public function mvcModelFindFirstException(DatabaseTester $I)
234    {
235        $I->wantToTest('Mvc\Model - findFirst() - exception');
236
237        $I->expectThrowable(
238            new Exception(
239                'Parameters passed must be of type array, string, numeric or null'
240            ),
241            function () {
242                Invoices::findFirst(false);
243            }
244        );
245    }
246
247    /**
248     * Tests Phalcon\Mvc\Model :: findFirst() - option 'column'
249     *
250     * @author Phalcon Team <team@phalcon.io>
251     * @since  2020-11-22
252     *
253     * @group  mysql
254     * @group  pgsql
255     * @group  sqlite
256     *
257     * @param DatabaseTester $I
258     */
259    public function mvcModelFindFirstColumn(DatabaseTester $I): void
260    {
261        $I->wantToTest('Mvc\Model - findFirst() - column option');
262
263        /** @var PDO $connection */
264        $connection = $I->getConnection();
265        $migration  = new InvoicesMigration($connection);
266        $migration->insert(4);
267
268        $invoice = Invoices::findFirst([
269            'columns' => 'inv_id',
270        ]);
271
272        $I->assertInstanceOf(
273            Row::class,
274            $invoice
275        );
276
277        $I->assertEquals(
278            4,
279            $invoice->inv_id
280        );
281
282        $I->assertEquals(
283            ['inv_id' => 4],
284            $invoice->toArray()
285        );
286    }
287
288    /**
289     * Tests Phalcon\Mvc\Model :: findFirst() - exception
290     *
291     * @dataProvider findFirstProvider
292     *
293     * @param DatabaseTester $I
294     * @param Example        $example
295     *
296     * @author       Phalcon Team <team@phalcon.io>
297     * @since        2020-01-27
298     *
299     * @group        mysql
300     * @group        pgsql
301     * @group        sqlite
302     */
303    public function mvcModelFindFirstStringPrimaryKey(DatabaseTester $I, Example $example)
304    {
305        $I->wantToTest('Mvc\Model - findFirst() - string primary key');
306
307        $connection = $I->getConnection();
308        $migration  = new StringPrimaryMigration($connection);
309        $migration->insert(
310            '5741bfd7-6870-40b7-adf6-cbacb515b9a9',
311            1
312        );
313        $migration->insert(
314            '1c53079c-249e-0c63-af8d-52413bfa2a2b',
315            2
316        );
317
318        $model = ModelWithStringPrimary::findFirst($example['params']);
319
320        $I->assertSame($example['found'], $model instanceof Model);
321    }
322
323    /**
324     * @return array
325     */
326    protected function findFirstProvider(): array
327    {
328        return [
329            [
330                'params' => [
331                    'uuid = ?0',
332                    'bind' => ['5741bfd7-6870-40b7-adf6-cbacb515b9a9'],
333                ],
334                'found'  => true,
335            ],
336            [
337                'params' => [
338                    'uuid = ?0',
339                    'bind' => ['1c53079c-249e-0c63-af8d-52413bfa2a2b'],
340                ],
341                'found'  => true,
342            ],
343            [
344                'params' => [
345                    'uuid = ?0',
346                    'bind' => ['1c53079c-249e-0c63-af8d-52413bfa2a2c'],
347                ],
348                'found'  => false,
349            ],
350            [
351                'params' => '134',
352                'found'  => false,
353            ],
354            [
355                'params' => "uuid = '134'",
356                'found'  => false,
357            ],
358        ];
359    }
360}
361