Skip to content

Commit

Permalink
Changed MetaData::getDI to throw exception
Browse files Browse the repository at this point in the history
See: #15011
  • Loading branch information
sergeyklay committed May 5, 2020
1 parent 45ee3a4 commit f62b248
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Changed return type hint for `Phalcon\Mvc\ModelInterface::sum` [#15000](https://github.com/phalcon/cphalcon/issues/15000)
- Changed return type for `Phalcon\Mvc\Model\Criteria::getLimit` so that integer, NULL or array will be returned [#15004](https://github.com/phalcon/cphalcon/issues/15004)
- Changed return type hint for `Phalcon\Mvc\Model\Manager::getCustomEventsManager` to return NULL instead of boolean FALSE if there is no special events manager [#15008](https://github.com/phalcon/cphalcon/issues/15008)
- Changed `Phalcon\Mvc\Model\MetaData::getDI` so that now it will throw a `Phalcon\Mvc\Model\Exception` if there is no `DiInterface` instance [#15011](https://github.com/phalcon/cphalcon/issues/15011)

## Fixed
- Fixed `Phalcon\Mvc\Model\Query\Builder::getPhql` to add single quote between string value on a simple condition [#14874](https://github.com/phalcon/cphalcon/issues/14874)
Expand Down
2 changes: 1 addition & 1 deletion ext/phalcon/mvc/model.zep.c

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

2 changes: 1 addition & 1 deletion ext/phalcon/mvc/model/manager.zep.c

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

82 changes: 53 additions & 29 deletions ext/phalcon/mvc/model/metadata.zep.c

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

16 changes: 13 additions & 3 deletions phalcon/Mvc/Model/MetaData.zep
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,17 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface
*/
public function getDI() -> <DiInterface>
{
return this->container;
var container;

let container = <DiInterface> this->container;

if typeof container != "object" {
throw new Exception(
Exception::containerServiceNotFound("internal services")
);
}

return container;
}

/**
Expand Down Expand Up @@ -798,7 +808,7 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface
/**
* Get the meta-data extraction strategy
*/
let container = this->container,
let container = this->getDI(),
strategy = this->getStrategy(),
modelMetadata = strategy->getMetaData(
model,
Expand Down Expand Up @@ -849,7 +859,7 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface
* Get the meta-data extraction strategy
*/
if typeof strategy != "object" {
let container = this->container,
let container = this->getDI(),
strategy = this->getStrategy();
}

Expand Down
51 changes: 44 additions & 7 deletions tests/database/Mvc/Model/MetaData/GetSetDICest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,44 @@
*
* (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.
*/

declare(strict_types=1);

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

use DatabaseTester;
use Phalcon\Mvc\Model\Exception as ExpectedException;
use Phalcon\Mvc\Model\MetaData\Memory;
use Phalcon\Storage\Exception;
use Phalcon\Test\Fixtures\Traits\DiTrait;

class GetSetDICest
{
use DiTrait;

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

/**
* Tests Phalcon\Mvc\Model\MetaData :: getDI() / setDI()
*
* @param DatabaseTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2020-02-01
*
Expand All @@ -39,11 +53,34 @@ public function mvcModelMetadataGetSetDI(DatabaseTester $I)
$I->wantToTest('Mvc\Model\MetaData - getDI() / setDI()');

$metadata = new Memory();

$I->assertNull($metadata->getDI());

$metadata->setDI($this->container);

$I->assertEquals($this->container, $metadata->getDI());
}

/**
* Tests Phalcon\Mvc\Model\MetaData :: getDI() - exception
*
* @param DatabaseTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2020-05-05
*
* @group common
*/
public function mvcModelMetadataGetDIThrowsException(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model\MetaData - getDI() - exception');

$I->expectThrowable(
new ExpectedException(
'A dependency injection container is required to access internal services'
),
function () {
(new Memory())->getDI();
}
);
}


}

0 comments on commit f62b248

Please sign in to comment.