Skip to content

Commit

Permalink
The property 'options' is always an array in Model Relations. (#13989)
Browse files Browse the repository at this point in the history
* The property 'options' is always an array in Model Relations.

* Added tests for Model Relation.
  • Loading branch information
SidRoberts authored and niden committed Apr 23, 2019
1 parent 2ce04de commit 357293f
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- `Phalcon\Mvc\View\Simple::render()` `params` property is now always an array.
- `Phalcon\Mvc\Model\CriteriaInterface::limit()` now takes `offset` as an integer. [#13977](https://github.com/phalcon/cphalcon/pull/13977)
- Changed `Phalcon\Mvc\Model\Manager::getModelSource()` to use `setModelSource()` internally instead of setting the source manually [#13987](https://github.com/phalcon/cphalcon/pull/13987)
- The property `options` is always an array in `Phalcon\Mvc\Model\Relation`. [#13989](https://github.com/phalcon/cphalcon/pull/13989)

## Fixed
- Fixed `Mvc\Collection::isInitialized()` now works as intended. [#13931](https://github.com/phalcon/cphalcon/pull/13931)
Expand Down
51 changes: 27 additions & 24 deletions phalcon/Mvc/Model/Relation.zep
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ class Relation implements RelationInterface
/**
* Phalcon\Mvc\Model\Relation constructor
*
* @param int type
* @param string|array fields
* @param string|array referencedFields
* @param array options
*/
public function __construct(int type, string! referencedModel, var fields, var referencedFields, var options = null) -> void
public function __construct(int type, string! referencedModel, var fields, var referencedFields, array options = []) -> void
{
let this->type = type,
this->referencedModel = referencedModel,
Expand All @@ -72,14 +70,15 @@ class Relation implements RelationInterface
public function getForeignKey()
{
var options, foreignKey;

let options = this->options;
if typeof options == "array" {
if fetch foreignKey, options["foreignKey"] {
if foreignKey {
return foreignKey;
}

if fetch foreignKey, options["foreignKey"] {
if foreignKey {
return foreignKey;
}
}

return false;
}

Expand Down Expand Up @@ -118,18 +117,18 @@ class Relation implements RelationInterface
public function getOption(string! name)
{
var option;
if fetch option, this->options[name] {
return option;

if !fetch option, this->options[name] {
return null;
}
return null;

return option;
}

/**
* Returns the options
*
* @return string|array
*/
public function getOptions()
public function getOptions() -> array
{
return this->options;
}
Expand All @@ -142,14 +141,15 @@ class Relation implements RelationInterface
public function getParams()
{
var options, params;

let options = this->options;
if typeof options == "array" {
if fetch params, options["params"] {
if params {
return params;
}

if fetch params, options["params"] {
if params {
return params;
}
}

return false;
}

Expand Down Expand Up @@ -199,7 +199,9 @@ class Relation implements RelationInterface
public function isThrough() -> bool
{
var type;

let type = this->type;

return type == self::HAS_ONE_THROUGH || type == self::HAS_MANY_THROUGH;
}

Expand All @@ -209,13 +211,14 @@ class Relation implements RelationInterface
public function isReusable() -> bool
{
var options, reusable;

let options = this->options;
if typeof options == "array" {
if fetch reusable, options["reusable"] {
return reusable;
}

if !fetch reusable, options["reusable"] {
return false;
}
return false;

return reusable;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions phalcon/Mvc/Model/RelationInterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ interface RelationInterface

/**
* Returns the options
*
* @return string|array
*/
public function getOptions();
public function getOptions() -> array;

/**
* Returns parameters that must be always used when the related records are obtained
Expand Down
61 changes: 56 additions & 5 deletions tests/integration/Mvc/Model/Relation/GetFieldsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Phalcon\Test\Integration\Mvc\Model\Relation;

use IntegrationTester;
use Phalcon\Mvc\Model\Relation;

/**
* Class GetFieldsCest
Expand All @@ -24,12 +25,62 @@ class GetFieldsCest
*
* @param IntegrationTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2018-11-13
* @author Sid Roberts <[email protected]>
* @since 2019-04-18
*/
public function mvcModelRelationGetFields(IntegrationTester $I)
public function mvcModelRelationGetFieldsString(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getFields()');
$I->skipTest('Need implementation');
$I->wantToTest('Mvc\Model\Relation - getFields() - string');

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
[
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
]
);

$I->assertEquals(
'id',
$relation->getFields()
);
}

/**
* Tests Phalcon\Mvc\Model\Relation :: getFields()
*
* @param IntegrationTester $I
*
* @author Sid Roberts <[email protected]>
* @since 2019-04-18
*/
public function mvcModelRelationGetFieldsArray(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getFields() - array');

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
[
'type',
'name',
],
'robots_id',
[
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
]
);

$I->assertEquals(
[
'type',
'name',
],
$relation->getFields()
);
}
}
31 changes: 28 additions & 3 deletions tests/integration/Mvc/Model/Relation/GetOptionCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Phalcon\Test\Integration\Mvc\Model\Relation;

use IntegrationTester;
use Phalcon\Mvc\Model\Relation;

/**
* Class GetOptionCest
Expand All @@ -24,12 +25,36 @@ class GetOptionCest
*
* @param IntegrationTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2018-11-13
* @author Sid Roberts <[email protected]>
* @since 2019-04-18
*/
public function mvcModelRelationGetOption(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getOption()');
$I->skipTest('Need implementation');

$options = [
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
];

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
$options
);



$I->assertEquals(
$options['reusable'],
$relation->getOption('reusable')
);

$I->assertEquals(
$options['alias'],
$relation->getOption('alias')
);
}
}
24 changes: 21 additions & 3 deletions tests/integration/Mvc/Model/Relation/GetOptionsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Phalcon\Test\Integration\Mvc\Model\Relation;

use IntegrationTester;
use Phalcon\Mvc\Model\Relation;

/**
* Class GetOptionsCest
Expand All @@ -24,12 +25,29 @@ class GetOptionsCest
*
* @param IntegrationTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2018-11-13
* @author Sid Roberts <[email protected]>
* @since 2019-04-18
*/
public function mvcModelRelationGetOptions(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getOptions()');
$I->skipTest('Need implementation');

$options = [
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
];

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
$options
);

$I->assertEquals(
$options,
$relation->getOptions()
);
}
}
60 changes: 57 additions & 3 deletions tests/integration/Mvc/Model/Relation/GetParamsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Phalcon\Test\Integration\Mvc\Model\Relation;

use IntegrationTester;
use Phalcon\Mvc\Model\Relation;

/**
* Class GetParamsCest
Expand All @@ -24,12 +25,65 @@ class GetParamsCest
*
* @param IntegrationTester $I
*
* @author Phalcon Team <[email protected]>
* @since 2018-11-13
* @author Sid Roberts <[email protected]>
* @since 2019-04-18
*/
public function mvcModelRelationGetParams(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getParams()');
$I->skipTest('Need implementation');

$options = [
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
'params' => [
'conditions' => 'robotTypeId = :type:',
'bind' => [
'type' => 4,
],
],
];

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
$options
);

$I->assertEquals(
$options['params'],
$relation->getParams()
);
}

/**
* Tests Phalcon\Mvc\Model\Relation :: getParams() when none are set
*
* @param IntegrationTester $I
*
* @author Sid Roberts <[email protected]>
* @since 2019-04-18
*/
public function mvcModelRelationGetParamsWhenNoneSet(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Relation - getParams() when none are set');

$options = [
'reusable' => true, // cache related data
'alias' => 'mechanicalParts',
];

$relation = new Relation(
Relation::HAS_MANY,
'RobotsParts',
'id',
'robots_id',
$options
);

$I->assertFalse(
$relation->getParams()
);
}
}
Loading

0 comments on commit 357293f

Please sign in to comment.