| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.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\TestSuite\TestCase;
- /**
- * AssociationCollection test case.
- */
- class AssociationCollectionTest extends TestCase
- {
- /**
- * setup
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $this->associations = new AssociationCollection();
- }
- /**
- * 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 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()
- {
- $belongsTo = new BelongsTo('Users', []);
- $this->assertEquals('user', $belongsTo->property());
- $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());
- }
- /**
- * Test getting association names by type.
- *
- * @return void
- */
- public function testType()
- {
- $belongsTo = new BelongsTo('');
- $this->associations->add('Users', $belongsTo);
- $belongsToMany = new BelongsToMany('');
- $this->associations->add('Tags', $belongsToMany);
- $this->assertSame([$belongsTo], $this->associations->type('BelongsTo'));
- $this->assertSame([$belongsToMany], $this->associations->type('BelongsToMany'));
- $this->assertSame([], $this->associations->type('HasMany'));
- $this->assertSame(
- [$belongsTo, $belongsToMany],
- $this->associations->type(['BelongsTo', 'BelongsToMany'])
- );
- }
- /**
- * test cascading deletes.
- *
- * @return void
- */
- public function testCascadeDelete()
- {
- $mockOne = $this->getMock('Cake\ORM\Association\BelongsTo', [], ['']);
- $mockTwo = $this->getMock('Cake\ORM\Association\HasMany', [], ['']);
- $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->getMock('Cake\ORM\Table', [], [[]]);
- $mockOne = $this->getMock(
- 'Cake\ORM\Association\BelongsTo',
- ['saveAssociated'],
- ['Parent', [
- 'sourceTable' => $table,
- ]]
- );
- $mockTwo = $this->getMock(
- 'Cake\ORM\Association\HasMany',
- ['saveAssociated'],
- ['Child', [
- 'sourceTable' => $table
- ]]
- );
- $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->getMock('Cake\ORM\Table', [], [[]]);
- $mockOne = $this->getMock(
- 'Cake\ORM\Association\BelongsTo',
- ['saveAssociated'],
- ['Parents', [
- 'sourceTable' => $table,
- ]]
- );
- $mockTwo = $this->getMock(
- 'Cake\ORM\Association\BelongsTo',
- ['saveAssociated'],
- ['Categories', [
- 'sourceTable' => $table
- ]]
- );
- $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->getMock('Cake\ORM\Table', [], [[]]);
- $mockOne = $this->getMock(
- 'Cake\ORM\Association\HasMany',
- ['saveAssociated'],
- ['Comments', [
- 'sourceTable' => $table,
- ]]
- );
- $mockTwo = $this->getMock(
- 'Cake\ORM\Association\HasOne',
- ['saveAssociated'],
- ['Profiles', [
- 'sourceTable' => $table
- ]]
- );
- $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.
- *
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Cannot save Profiles, it is not associated to Users
- */
- public function testErrorOnUnknownAlias()
- {
- $table = $this->getMock(
- 'Cake\ORM\Table',
- ['save'],
- [['alias' => 'Users']]
- );
- $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);
- }
- }
|