| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\ORM;
- use Cake\ORM\AssociationCollection;
- use Cake\ORM\Association\BelongsTo;
- use Cake\ORM\Association\BelongsToMany;
- use Cake\ORM\Entity;
- use Cake\ORM\Locator\LocatorInterface;
- use Cake\TestSuite\TestCase;
- /**
- * AssociationCollection test case.
- */
- class AssociationCollectionTest extends TestCase
- {
- /**
- * @var AssociationCollection
- */
- public $associations;
- /**
- * setup
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $this->associations = new AssociationCollection();
- }
- /**
- * Test the constructor.
- *
- * @return void
- */
- public function testConstructor()
- {
- $this->assertSame($this->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.
- *
- * @return void
- */
- public function testAddHasRemoveAndGet()
- {
- $this->assertFalse($this->associations->has('users'));
- $this->assertFalse($this->associations->has('Users'));
- $this->assertNull($this->associations->get('users'));
- $this->assertNull($this->associations->get('Users'));
- $belongsTo = new BelongsTo('');
- $this->assertSame($belongsTo, $this->associations->add('Users', $belongsTo));
- $this->assertTrue($this->associations->has('users'));
- $this->assertTrue($this->associations->has('Users'));
- $this->assertSame($belongsTo, $this->associations->get('users'));
- $this->assertSame($belongsTo, $this->associations->get('Users'));
- $this->assertNull($this->associations->remove('Users'));
- $this->assertFalse($this->associations->has('users'));
- $this->assertFalse($this->associations->has('Users'));
- $this->assertNull($this->associations->get('users'));
- $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
- *
- * @return void
- */
- public function testRemoveAll()
- {
- $this->assertEmpty($this->associations->keys());
- $belongsTo = new BelongsTo('');
- $this->assertSame($belongsTo, $this->associations->add('Users', $belongsTo));
- $belongsToMany = new BelongsToMany('');
- $this->assertSame($belongsToMany, $this->associations->add('Cart', $belongsToMany));
- $this->associations->removeAll();
- $this->assertEmpty($this->associations->keys());
- }
- /**
- * Test getting associations by property.
- *
- * @return void
- */
- public function testGetByProperty()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')
- ->setMethods(['table'])
- ->getMock();
- $table->setSchema([]);
- $belongsTo = new BelongsTo('Users', [
- 'sourceTable' => $table
- ]);
- $this->assertEquals('user', $belongsTo->getProperty());
- $this->associations->add('Users', $belongsTo);
- $this->assertNull($this->associations->get('user'));
- $this->assertSame($belongsTo, $this->associations->getByProperty('user'));
- }
- /**
- * Test associations with plugin names.
- *
- * @return void
- */
- public function testAddHasRemoveGetWithPlugin()
- {
- $this->assertFalse($this->associations->has('Photos.Photos'));
- $this->assertFalse($this->associations->has('Photos'));
- $belongsTo = new BelongsTo('');
- $this->assertSame($belongsTo, $this->associations->add('Photos.Photos', $belongsTo));
- $this->assertTrue($this->associations->has('Photos'));
- $this->assertFalse($this->associations->has('Photos.Photos'));
- }
- /**
- * Test keys()
- *
- * @return void
- */
- public function testKeys()
- {
- $belongsTo = new BelongsTo('');
- $this->associations->add('Users', $belongsTo);
- $this->associations->add('Categories', $belongsTo);
- $this->assertEquals(['users', 'categories'], $this->associations->keys());
- $this->associations->remove('Categories');
- $this->assertEquals(['users'], $this->associations->keys());
- }
- /**
- * Data provider for AssociationCollection::getByType
- */
- public function associationCollectionType()
- {
- return [
- ['BelongsTo', 'BelongsToMany'],
- ['belongsTo', 'belongsToMany'],
- ['belongsto', 'belongstomany']
- ];
- }
- /**
- * Test getting association names by getByType.
- *
- * @param string $belongsToStr
- * @param string $belongsToManyStr
- * @dataProvider associationCollectionType
- */
- public function testGetByType($belongsToStr, $belongsToManyStr)
- {
- $belongsTo = new BelongsTo('');
- $this->associations->add('Users', $belongsTo);
- $belongsToMany = new BelongsToMany('');
- $this->associations->add('Tags', $belongsToMany);
- $this->assertSame([$belongsTo], $this->associations->getByType($belongsToStr));
- $this->assertSame([$belongsToMany], $this->associations->getByType($belongsToManyStr));
- $this->assertSame([$belongsTo, $belongsToMany], $this->associations->getByType([$belongsToStr, $belongsToManyStr]));
- }
- /**
- * Type should return empty array.
- *
- * @return void
- */
- public function hasTypeReturnsEmptyArray()
- {
- foreach (['HasMany', 'hasMany', 'FooBar', 'DoesNotExist'] as $value) {
- $this->assertSame([], $this->associations->getByType($value));
- }
- }
- /**
- * test cascading deletes.
- *
- * @return void
- */
- public function testCascadeDelete()
- {
- $mockOne = $this->getMockBuilder('Cake\ORM\Association\BelongsTo')
- ->setConstructorArgs([''])
- ->getMock();
- $mockTwo = $this->getMockBuilder('Cake\ORM\Association\HasMany')
- ->setConstructorArgs([''])
- ->getMock();
- $entity = new Entity();
- $options = ['option' => 'value'];
- $this->associations->add('One', $mockOne);
- $this->associations->add('Two', $mockTwo);
- $mockOne->expects($this->once())
- ->method('cascadeDelete')
- ->with($entity, $options);
- $mockTwo->expects($this->once())
- ->method('cascadeDelete')
- ->with($entity, $options);
- $this->assertNull($this->associations->cascadeDelete($entity, $options));
- }
- /**
- * Test saving parent associations
- *
- * @return void
- */
- public function testSaveParents()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')
- ->setMethods(['table'])
- ->getMock();
- $table->setSchema([]);
- $mockOne = $this->getMockBuilder('Cake\ORM\Association\BelongsTo')
- ->setMethods(['saveAssociated'])
- ->setConstructorArgs(['Parent', [
- 'sourceTable' => $table,
- ]])
- ->getMock();
- $mockTwo = $this->getMockBuilder('Cake\ORM\Association\HasMany')
- ->setMethods(['saveAssociated'])
- ->setConstructorArgs(['Child', [
- 'sourceTable' => $table
- ]])
- ->getMock();
- $this->associations->add('Parent', $mockOne);
- $this->associations->add('Child', $mockTwo);
- $entity = new Entity();
- $entity->set('parent', ['key' => 'value']);
- $entity->set('child', ['key' => 'value']);
- $options = ['option' => 'value'];
- $mockOne->expects($this->once())
- ->method('saveAssociated')
- ->with($entity, $options)
- ->will($this->returnValue(true));
- $mockTwo->expects($this->never())
- ->method('saveAssociated');
- $result = $this->associations->saveParents(
- $table,
- $entity,
- ['Parent', 'Child'],
- $options
- );
- $this->assertTrue($result, 'Save should work.');
- }
- /**
- * Test saving filtered parent associations.
- *
- * @return void
- */
- public function testSaveParentsFiltered()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')
- ->setMethods(['table'])
- ->getMock();
- $table->setSchema([]);
- $mockOne = $this->getMockBuilder('Cake\ORM\Association\BelongsTo')
- ->setMethods(['saveAssociated'])
- ->setConstructorArgs(['Parents', [
- 'sourceTable' => $table,
- ]])
- ->getMock();
- $mockTwo = $this->getMockBuilder('Cake\ORM\Association\BelongsTo')
- ->setMethods(['saveAssociated'])
- ->setConstructorArgs(['Categories', [
- 'sourceTable' => $table
- ]])
- ->getMock();
- $this->associations->add('Parents', $mockOne);
- $this->associations->add('Categories', $mockTwo);
- $entity = new Entity();
- $entity->set('parent', ['key' => 'value']);
- $entity->set('category', ['key' => 'value']);
- $options = ['atomic' => true];
- $mockOne->expects($this->once())
- ->method('saveAssociated')
- ->with($entity, ['atomic' => true, 'associated' => ['Others']])
- ->will($this->returnValue(true));
- $mockTwo->expects($this->never())
- ->method('saveAssociated');
- $result = $this->associations->saveParents(
- $table,
- $entity,
- ['Parents' => ['associated' => ['Others']]],
- $options
- );
- $this->assertTrue($result, 'Save should work.');
- }
- /**
- * Test saving filtered child associations.
- *
- * @return void
- */
- public function testSaveChildrenFiltered()
- {
- $table = $this->getMockBuilder('Cake\ORM\Table')
- ->setMethods(['table'])
- ->getMock();
- $table->setSchema([]);
- $mockOne = $this->getMockBuilder('Cake\ORM\Association\HasMany')
- ->setMethods(['saveAssociated'])
- ->setConstructorArgs(['Comments', [
- 'sourceTable' => $table,
- ]])
- ->getMock();
- $mockTwo = $this->getMockBuilder('Cake\ORM\Association\HasOne')
- ->setMethods(['saveAssociated'])
- ->setConstructorArgs(['Profiles', [
- 'sourceTable' => $table
- ]])
- ->getMock();
- $this->associations->add('Comments', $mockOne);
- $this->associations->add('Profiles', $mockTwo);
- $entity = new Entity();
- $entity->set('comments', ['key' => 'value']);
- $entity->set('profile', ['key' => 'value']);
- $options = ['atomic' => true];
- $mockOne->expects($this->once())
- ->method('saveAssociated')
- ->with($entity, $options + ['associated' => ['Other']])
- ->will($this->returnValue(true));
- $mockTwo->expects($this->never())
- ->method('saveAssociated');
- $result = $this->associations->saveChildren(
- $table,
- $entity,
- ['Comments' => ['associated' => ['Other']]],
- $options
- );
- $this->assertTrue($result, 'Should succeed.');
- }
- /**
- * Test exceptional case.
- *
- */
- public function testErrorOnUnknownAlias()
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Cannot save Profiles, it is not associated to Users');
- $table = $this->getMockBuilder('Cake\ORM\Table')
- ->setMethods(['save'])
- ->setConstructorArgs([['alias' => 'Users']])
- ->getMock();
- $entity = new Entity();
- $entity->set('profile', ['key' => 'value']);
- $this->associations->saveChildren(
- $table,
- $entity,
- ['Profiles'],
- ['atomic' => true]
- );
- }
- /**
- * Tests the normalizeKeys method
- *
- * @return void
- */
- public function testNormalizeKeys()
- {
- $this->assertSame([], $this->associations->normalizeKeys([]));
- $this->assertSame([], $this->associations->normalizeKeys(false));
- $assocs = ['a', 'b', 'd' => ['something']];
- $expected = ['a' => [], 'b' => [], 'd' => ['something']];
- $this->assertSame($expected, $this->associations->normalizeKeys($assocs));
- $belongsTo = new BelongsTo('');
- $this->associations->add('users', $belongsTo);
- $this->associations->add('categories', $belongsTo);
- $expected = ['users' => [], 'categories' => []];
- $this->assertSame($expected, $this->associations->normalizeKeys(true));
- }
- /**
- * Ensure that the association collection can be iterated.
- *
- * @return void
- */
- public function testAssociationsCanBeIterated()
- {
- $belongsTo = new BelongsTo('');
- $this->associations->add('Users', $belongsTo);
- $belongsToMany = new BelongsToMany('');
- $this->associations->add('Cart', $belongsToMany);
- $expected = ['users' => $belongsTo, 'cart' => $belongsToMany];
- $result = iterator_to_array($this->associations, true);
- $this->assertSame($expected, $result);
- }
- }
|