|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHP Version 5.4 |
| 4 | + * |
| 5 | + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
| 6 | + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 7 | + * |
| 8 | + * Licensed under The MIT License |
| 9 | + * For full copyright and license information, please see the LICENSE.txt |
| 10 | + * Redistributions of files must retain the above copyright notice. |
| 11 | + * |
| 12 | + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 13 | + * @link http://cakephp.org CakePHP(tm) Project |
| 14 | + * @since CakePHP(tm) v 3.0.0 |
| 15 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 16 | + */ |
| 17 | +namespace Cake\Test\TestCase\ORM; |
| 18 | + |
| 19 | +use Cake\ORM\Association; |
| 20 | +use Cake\ORM\Table; |
| 21 | + |
| 22 | +/** |
| 23 | + * A Test double used to assert that default tables are created |
| 24 | + * |
| 25 | + **/ |
| 26 | +class TestTable extends Table { |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests Association class |
| 32 | + * |
| 33 | + */ |
| 34 | +class AssociationTest extends \Cake\TestSuite\TestCase { |
| 35 | + |
| 36 | + public function setUp() { |
| 37 | + $config = [ |
| 38 | + 'className' => '\Cake\Test\TestCase\ORM\TestTable', |
| 39 | + 'foreignKey' => 'a_key', |
| 40 | + 'conditions' => ['field' => 'value'], |
| 41 | + 'dependent' => true |
| 42 | + ]; |
| 43 | + $this->association = $this->getMock( |
| 44 | + '\Cake\ORM\Association', |
| 45 | + ['_options', 'attachTo'], |
| 46 | + ['Foo', $config] |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | +/** |
| 51 | + * Tests that _options acts as a callback where subclasses can add their own |
| 52 | + * initialization code based on the passed configuration array |
| 53 | + * |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + public function testOptionsIsCalled() { |
| 57 | + $options = ['foo' => 'bar']; |
| 58 | + $this->association->expects($this->once())->method('_options')->with($options); |
| 59 | + $this->association->__construct('Name', $options); |
| 60 | + } |
| 61 | + |
| 62 | +/** |
| 63 | + * Tests that name() returns the correct configure association name |
| 64 | + * |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + public function testName() { |
| 68 | + $this->assertEquals('Foo', $this->association->name()); |
| 69 | + $this->association->name('Bar'); |
| 70 | + $this->assertEquals('Bar', $this->association->name()); |
| 71 | + } |
| 72 | + |
| 73 | +/** |
| 74 | + * Tests that name() returns the correct configured value |
| 75 | + * |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + public function testForeignKey() { |
| 79 | + $this->assertEquals('a_key', $this->association->foreignKey()); |
| 80 | + $this->association->foreignKey('another_key'); |
| 81 | + $this->assertEquals('another_key', $this->association->foreignKey()); |
| 82 | + } |
| 83 | + |
| 84 | +/** |
| 85 | + * Tests that conditions() returns the correct configured value |
| 86 | + * |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + public function testConditions() { |
| 90 | + $this->assertEquals(['field' => 'value'], $this->association->conditions()); |
| 91 | + $conds = ['another_key' => 'another value']; |
| 92 | + $this->association->conditions($conds); |
| 93 | + $this->assertEquals($conds, $this->association->conditions()); |
| 94 | + } |
| 95 | + |
| 96 | +/** |
| 97 | + * Tests that canBeJoined() returns the correct configured value |
| 98 | + * |
| 99 | + * @return void |
| 100 | + */ |
| 101 | + public function testCanBeJoined() { |
| 102 | + $this->assertFalse($this->association->canBeJoined()); |
| 103 | + } |
| 104 | + |
| 105 | +/** |
| 106 | + * Tests that repository() returns the correct Table object |
| 107 | + * |
| 108 | + * @return void |
| 109 | + */ |
| 110 | + public function testRepository() { |
| 111 | + $table = $this->association->repository(); |
| 112 | + $this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table); |
| 113 | + |
| 114 | + $other = new Table; |
| 115 | + $this->association->repository($other); |
| 116 | + $this->assertSame($other, $this->association->repository()); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments