AssociationProxyTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\Association;
  17. use Cake\ORM\Table;
  18. use Cake\ORM\TableRegistry;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests the features related to proxying methods from the Association
  22. * class to the Table class
  23. *
  24. */
  25. class AssociationProxyTest extends TestCase {
  26. /**
  27. * Fixtures to be loaded
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.article', 'core.author', 'core.comment'
  33. ];
  34. /**
  35. * Teardown
  36. *
  37. * @return void
  38. */
  39. public function tearDown() {
  40. parent::tearDown();
  41. TableRegistry::clear();
  42. }
  43. /**
  44. * Tests that it is possible to get associations as a property
  45. *
  46. * @return void
  47. */
  48. public function testAssociationAsProperty() {
  49. $articles = TableRegistry::get('articles');
  50. $articles->hasMany('comments');
  51. $articles->belongsTo('authors');
  52. $this->assertTrue(isset($articles->authors));
  53. $this->assertTrue(isset($articles->comments));
  54. $this->assertFalse(isset($articles->posts));
  55. $this->assertSame($articles->association('authors'), $articles->authors);
  56. $this->assertSame($articles->association('comments'), $articles->comments);
  57. }
  58. /**
  59. * Tests that getting a bad property throws exception
  60. *
  61. * @expectedException \RuntimeException
  62. * @expectedExceptionMessage Table "Cake\ORM\Table" is not associated with "posts"
  63. * @return void
  64. */
  65. public function testGetBadAssociation() {
  66. $articles = TableRegistry::get('articles');
  67. $articles->posts;
  68. }
  69. /**
  70. * Tests that the proxied updateAll will preserve conditions set for the association
  71. *
  72. * @return void
  73. */
  74. public function testUpdateAllFromAssociation() {
  75. $articles = TableRegistry::get('articles');
  76. $comments = TableRegistry::get('comments');
  77. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  78. $articles->comments->updateAll(['comment' => 'changed'], ['article_id' => 1]);
  79. $changed = $comments->find()->where(['comment' => 'changed'])->count();
  80. $this->assertEquals(3, $changed);
  81. }
  82. /**
  83. * Tests that the proxied deleteAll preserves conditions set for the association
  84. *
  85. * @return void
  86. */
  87. public function testDeleteAllFromAssociation() {
  88. $articles = TableRegistry::get('articles');
  89. $comments = TableRegistry::get('comments');
  90. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  91. $articles->comments->deleteAll(['article_id' => 1]);
  92. $remaining = $comments->find()->where(['article_id' => 1])->count();
  93. $this->assertEquals(1, $remaining);
  94. }
  95. /**
  96. * Tests that it is possible to get associations as a property
  97. *
  98. * @return void
  99. */
  100. public function testAssociationAsPropertyProxy() {
  101. $articles = TableRegistry::get('articles');
  102. $authors = TableRegistry::get('authors');
  103. $articles->belongsTo('authors');
  104. $authors->hasMany('comments');
  105. $this->assertTrue(isset($articles->authors->comments));
  106. $this->assertSame($authors->association('comments'), $articles->authors->comments);
  107. }
  108. /**
  109. * Tests that methods are proxied from the Association to the target table
  110. *
  111. * @return void
  112. */
  113. public function testAssociationMethodProxy() {
  114. $articles = TableRegistry::get('articles');
  115. $mock = $this->getMock('Cake\ORM\Table', ['crazy']);
  116. $articles->belongsTo('authors', [
  117. 'targetTable' => $mock
  118. ]);
  119. $mock->expects($this->once())->method('crazy')
  120. ->with('a', 'b')
  121. ->will($this->returnValue('thing'));
  122. $this->assertEquals('thing', $articles->authors->crazy('a', 'b'));
  123. }
  124. }