Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed MetaData::getDI to throw exception #15012

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 42 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 <team@phalcon.io>
*
* 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 <team@phalcon.io>
* @since 2020-02-01
*
Expand All @@ -39,11 +53,32 @@ 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 <team@phalcon.io>
* @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();
}
);
}
}