Skip to content

Commit

Permalink
Fixed return type hint for Resultset::getFirst
Browse files Browse the repository at this point in the history
See: #15027
  • Loading branch information
sergeyklay authored and niden committed May 16, 2020
1 parent 18afd6e commit f435b74
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- Fixed return type hint for `Phalcon\Db\Column::getSize` and `Phalcon\Db\ColumnInterface::getSize` to reflect original behavior [#15019](https://github.com/phalcon/cphalcon/issues/15019)
- Fixed return type hint for `Phalcon\Db\Column::getAfterPosition` and `Phalcon\Db\ColumnInterface::getAfterPosition` to reflect original behavior [#15021](https://github.com/phalcon/cphalcon/issues/15021)
- Fixed return type hint for `Phalcon\Mvc\Model\Manager::executeQuery` and `Phalcon\Mvc\Model\Manager::ManagerInterface` to reflect original behavior [#15024](https://github.com/phalcon/cphalcon/issues/15024)
- Fixed return type hint for `Phalcon\Mvc\Model\Resultset::getFirst` and `Phalcon\Mvc\Model\ResultsetInterface::getFirst` to reflect original behavior [#15027](https://github.com/phalcon/cphalcon/issues/15027)

# [4.0.5](https://github.com/phalcon/cphalcon/releases/tag/v4.0.5) (2020-03-07)
## Added
Expand Down
38 changes: 30 additions & 8 deletions ext/phalcon/mvc/model/resultset.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions ext/phalcon/mvc/model/resultset.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions ext/phalcon/mvc/model/resultsetinterface.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions ext/phalcon/mvc/model/resultsetinterface.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 25 additions & 3 deletions phalcon/Mvc/Model/Resultset.zep
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
* For the full copyright and license information, please view the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Phalcon\Mvc\Model;
Expand Down Expand Up @@ -307,8 +307,30 @@ abstract class Resultset

/**
* Get first row in the resultset
*
* ```php
* $model = new Robots();
* $manager = $model->getModelsManager();
*
* // \Robots
* $manager->createQuery('SELECT * FROM Robots')
* ->execute()
* ->getFirst();
*
* // \Phalcon\Mvc\Model\Row
* $manager->createQuery('SELECT r.id FROM Robots AS r')
* ->execute()
* ->getFirst();
*
* // NULL
* $manager->createQuery('SELECT r.id FROM Robots AS r WHERE r.name = "NON-EXISTENT"')
* ->execute()
* ->getFirst();
* ```
*
* @return ModelInterface|Row|null
*/
public function getFirst() -> <ModelInterface> | null
public function getFirst() -> var | null
{
if this->count == 0 {
return null;
Expand Down
8 changes: 5 additions & 3 deletions phalcon/Mvc/Model/ResultsetInterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
* For the full copyright and license information, please view the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Phalcon\Mvc\Model;
Expand Down Expand Up @@ -49,8 +49,10 @@ interface ResultsetInterface

/**
* Get first row in the resultset
*
* @return ModelInterface|Row|null
*/
public function getFirst() -> <ModelInterface> | null;
public function getFirst() -> var | null;

/**
* Returns the current hydration mode
Expand Down
90 changes: 90 additions & 0 deletions tests/database/Mvc/Model/Resultset/GetFirstCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the
* LICENSE.txt file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Database\Mvc\Model\Resultset;

use DatabaseTester;
use Phalcon\Mvc\Model\ManagerInterface;
use Phalcon\Mvc\Model\Row;
use Phalcon\Storage\Exception;
use Phalcon\Test\Fixtures\Migrations\InvoicesMigration;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use Phalcon\Test\Fixtures\Traits\RecordsTrait;
use Phalcon\Test\Models\Invoices;

class GetFirstCest
{
use DiTrait;
use RecordsTrait;

/**
* @var InvoicesMigration
*/
private $invoiceMigration;

/**
* Executed before each test
*
* @param DatabaseTester $I
* @return void
*/
public function _before(DatabaseTester $I): void
{
try {
$this->setNewFactoryDefault();
} catch (Exception $e) {
$I->fail($e->getMessage());
}

$this->setDatabase($I);

$this->invoiceMigration = new InvoicesMigration($I->getConnection());
}

/**
* Tests Mvc\Model\Resultset :: getFirst() - Issue 15027
*
* @param DatabaseTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2020-05-06
* @issue 15027
*
* @group mysql
* @group pgsql
*/
public function mvcModelResultsetGetFirst(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model\Resultset :: getFirst() - #15027');

/**
* @todo The following tests are skipped for sqlite because we will get
* a General Error 5 database is locked error.
*/
$invId = ('sqlite' === $I->getDriver()) ? 'null' : 'default';

$this->insertDataInvoices($this->invoiceMigration, 7, $invId, 2, 'ccc');

/** @var ManagerInterface $manager */
$manager = $this->getService('modelsManager');

$sql = sprintf('SELECT i.inv_id FROM [%s] AS i', Invoices::class);
$I->assertInstanceOf(Row::class, $manager->createQuery($sql)->execute()->getFirst());

$sql = sprintf('SELECT * FROM [%s] AS i', Invoices::class);
$I->assertInstanceOf(Invoices::class, $manager->createQuery($sql)->execute()->getFirst());

$sql = sprintf('SELECT i.inv_id FROM [%s] AS i WHERE inv_total = -42', Invoices::class);
$I->assertNull($manager->createQuery($sql)->execute()->getFirst());
}
}

0 comments on commit f435b74

Please sign in to comment.