Skip to content

Commit

Permalink
Pass the locator to the constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Sep 19, 2017
1 parent 8ee1cfc commit cb4d38d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/ORM/AssociationCollection.php
Expand Up @@ -17,6 +17,7 @@
use ArrayIterator;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\Locator\LocatorInterface;
use InvalidArgumentException;
use IteratorAggregate;

Expand All @@ -39,6 +40,21 @@ class AssociationCollection implements IteratorAggregate
*/
protected $_items = [];

/**
* Constructor.
*
* Sets the default table locator for associations.
* If no locator is provided, the global one will be used.
*
* @param \Cake\ORM\Locator\LocatorInterface|null $tableLocator Table locator instance.
*/
public function __construct(LocatorInterface $tableLocator = null)
{
if ($tableLocator !== null) {
$this->_tableLocator = $tableLocator;
}
}

/**
* Add an association to the collection
*
Expand Down
3 changes: 1 addition & 2 deletions src/ORM/Locator/TableLocator.php
Expand Up @@ -213,8 +213,7 @@ public function get($alias, array $options = [])
$options['connection'] = ConnectionManager::get($connectionName);
}
if (empty($options['associations'])) {
$associations = new AssociationCollection();
$associations->setTableLocator($this);
$associations = new AssociationCollection($this);
$options['associations'] = $associations;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/ORM/AssociationCollectionTest.php
Expand Up @@ -19,6 +19,7 @@
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Entity;
use Cake\ORM\Locator\LocatorInterface;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
Expand All @@ -42,6 +43,20 @@ public function setUp()
$this->associations = new AssociationCollection();
}

/**
* Test the constructor.
*
* @return void
*/
public function testConstructor()
{
$this->assertSame(TableRegistry::getTableLocator(), $this->associations->getTableLocator());

$tableLocator = $this->createMock(LocatorInterface::class);
$associations = new AssociationCollection($tableLocator);
$this->assertSame($tableLocator, $associations->getTableLocator());
}

/**
* Test the simple add/has and get methods.
*
Expand Down

0 comments on commit cb4d38d

Please sign in to comment.