Skip to content

Commit

Permalink
Add AssociationCollection::load method.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Sep 19, 2017
1 parent 535f99f commit 369bc32
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/ORM/AssociationCollection.php
Expand Up @@ -56,6 +56,30 @@ public function add($alias, Association $association)
return $this->_items[strtolower($alias)] = $association;
}

/**
* Creates and adds the Association object to this collection.
*
* @param string $className The name of association class.
* @param string $associated The alias for the target table.
* @param array $options List of options to configure the association definition.
* @return \Cake\ORM\Association
* @throws InvalidArgumentException
*/
public function load($className, $associated, array $options = [])
{
$options += [
'tableLocator' => $this->getTableLocator()
];

$association = new $className($associated, $options);
if (!$association instanceof Association) {
$message = sprintf('The association must extend `%s` class, `%s` given.', Association::class, get_class($association));
throw new InvalidArgumentException($message);
}

return $this->add($association->getName(), $association);
}

/**
* Fetch an attached association by name.
*
Expand Down
42 changes: 42 additions & 0 deletions tests/TestCase/ORM/AssociationCollectionTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Entity;
use Cake\ORM\Locator\LocatorInterface;
use Cake\TestSuite\TestCase;

/**
Expand Down Expand Up @@ -70,6 +71,47 @@ public function testAddHasRemoveAndGet()
$this->assertNull($this->associations->get('Users'));
}

/**
* Test the load method.
*
* @return void
*/
public function testLoad()
{
$this->associations->load(BelongsTo::class, 'Users');
$this->assertTrue($this->associations->has('Users'));
$this->assertInstanceOf(BelongsTo::class, $this->associations->get('Users'));
$this->assertSame($this->associations->getTableLocator(), $this->associations->get('Users')->getTableLocator());
}

/**
* Test the load method with custom locator.
*
* @return void
*/
public function testLoadCustomLocator()
{
$locator = $this->createMock(LocatorInterface::class);
$this->associations->load(BelongsTo::class, 'Users', [
'tableLocator' => $locator
]);
$this->assertTrue($this->associations->has('Users'));
$this->assertInstanceOf(BelongsTo::class, $this->associations->get('Users'));
$this->assertSame($locator, $this->associations->get('Users')->getTableLocator());
}

/**
* Test load invalid class.
*
* @return void
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The association must extend `Cake\ORM\Association` class, `stdClass` given.
*/
public function testLoadInvalid()
{
$this->associations->load('stdClass', 'Users');
}

/**
* Test removeAll method
*
Expand Down

0 comments on commit 369bc32

Please sign in to comment.