|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
| 4 | + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 5 | + * |
| 6 | + * Licensed under The MIT License |
| 7 | + * For full copyright and license information, please see the LICENSE.txt |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 11 | + * @link http://cakephp.org CakePHP(tm) Project |
| 12 | + * @since 3.0.0 |
| 13 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 14 | + */ |
| 15 | +namespace Cake\Test\TestCase\ORM; |
| 16 | + |
| 17 | +use Cake\ORM\Association; |
| 18 | +use Cake\ORM\Table; |
| 19 | +use Cake\ORM\TableRegistry; |
| 20 | +use Cake\TestSuite\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests the features related to proxying methods from the Association |
| 24 | + * class to the Table class |
| 25 | + * |
| 26 | + */ |
| 27 | +class AssociationProxyTest extends TestCase { |
| 28 | + |
| 29 | +/** |
| 30 | + * Fixtures to be loaded |
| 31 | + * |
| 32 | + * @var array |
| 33 | + */ |
| 34 | + public $fixtures = [ |
| 35 | + 'core.article', 'core.author', 'core.comment' |
| 36 | + ]; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests that it is possible to get associations as a property |
| 40 | + * |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function testAssociationAsProperty() { |
| 44 | + $articles = TableRegistry::get('articles'); |
| 45 | + $articles->hasMany('comments'); |
| 46 | + $articles->belongsTo('authors'); |
| 47 | + $this->assertTrue(isset($articles->authors)); |
| 48 | + $this->assertTrue(isset($articles->comments)); |
| 49 | + $this->assertFalse(isset($articles->posts)); |
| 50 | + $this->assertSame($articles->association('authors'), $articles->authors); |
| 51 | + $this->assertSame($articles->association('comments'), $articles->comments); |
| 52 | + } |
| 53 | + |
| 54 | +/** |
| 55 | + * Tests that getting a bad property throws exception |
| 56 | + * |
| 57 | + * @expectedException \RuntimeException |
| 58 | + * @expectedExceptionMessage Table "TestApp\Model\Table\ArticlesTable" is not associated with "posts" |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + public function testGetBadAssociation() { |
| 62 | + $articles = TableRegistry::get('articles'); |
| 63 | + $articles->posts; |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments