AssociationProxyTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /**
  28. * Fixtures to be loaded
  29. *
  30. * @var array
  31. */
  32. public $fixtures = [
  33. 'core.articles', 'core.authors', 'core.comments'
  34. ];
  35. /**
  36. * Teardown
  37. *
  38. * @return void
  39. */
  40. public function tearDown()
  41. {
  42. parent::tearDown();
  43. TableRegistry::clear();
  44. }
  45. /**
  46. * Tests that it is possible to get associations as a property
  47. *
  48. * @return void
  49. */
  50. public function testAssociationAsProperty()
  51. {
  52. $articles = TableRegistry::get('articles');
  53. $articles->hasMany('comments');
  54. $articles->belongsTo('authors');
  55. $this->assertTrue(isset($articles->authors));
  56. $this->assertTrue(isset($articles->comments));
  57. $this->assertFalse(isset($articles->posts));
  58. $this->assertSame($articles->association('authors'), $articles->authors);
  59. $this->assertSame($articles->association('comments'), $articles->comments);
  60. }
  61. /**
  62. * Tests that getting a bad property throws exception
  63. *
  64. * @expectedException \RuntimeException
  65. * @expectedExceptionMessage Table "Cake\ORM\Table" is not associated with "posts"
  66. * @return void
  67. */
  68. public function testGetBadAssociation()
  69. {
  70. $articles = TableRegistry::get('articles');
  71. $articles->posts;
  72. }
  73. /**
  74. * Test that find() with empty conditions generates valid SQL
  75. *
  76. * @return void
  77. */
  78. public function testFindEmptyConditions()
  79. {
  80. $table = TableRegistry::get('Users');
  81. $table->hasMany('Articles', [
  82. 'foreignKey' => 'author_id',
  83. 'conditions' => '',
  84. ]);
  85. $query = $table->Articles->find('list', ['limit' => 2]);
  86. $this->assertCount(2, $query->all());
  87. }
  88. /**
  89. * Tests that the proxied updateAll will preserve conditions set for the association
  90. *
  91. * @return void
  92. */
  93. public function testUpdateAllFromAssociation()
  94. {
  95. $articles = TableRegistry::get('articles');
  96. $comments = TableRegistry::get('comments');
  97. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  98. $articles->comments->updateAll(['comment' => 'changed'], ['article_id' => 1]);
  99. $changed = $comments->find()->where(['comment' => 'changed'])->count();
  100. $this->assertEquals(3, $changed);
  101. }
  102. /**
  103. * Tests that the proxied deleteAll preserves conditions set for the association
  104. *
  105. * @return void
  106. */
  107. public function testDeleteAllFromAssociation()
  108. {
  109. $articles = TableRegistry::get('articles');
  110. $comments = TableRegistry::get('comments');
  111. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  112. $articles->comments->deleteAll(['article_id' => 1]);
  113. $remaining = $comments->find()->where(['article_id' => 1])->count();
  114. $this->assertEquals(1, $remaining);
  115. }
  116. /**
  117. * Tests that it is possible to get associations as a property
  118. *
  119. * @return void
  120. */
  121. public function testAssociationAsPropertyProxy()
  122. {
  123. $articles = TableRegistry::get('articles');
  124. $authors = TableRegistry::get('authors');
  125. $articles->belongsTo('authors');
  126. $authors->hasMany('comments');
  127. $this->assertTrue(isset($articles->authors->comments));
  128. $this->assertSame($authors->association('comments'), $articles->authors->comments);
  129. }
  130. /**
  131. * Tests that methods are proxied from the Association to the target table
  132. *
  133. * @return void
  134. */
  135. public function testAssociationMethodProxy()
  136. {
  137. $articles = TableRegistry::get('articles');
  138. $mock = $this->getMock('Cake\ORM\Table', ['crazy']);
  139. $articles->belongsTo('authors', [
  140. 'targetTable' => $mock
  141. ]);
  142. $mock->expects($this->once())->method('crazy')
  143. ->with('a', 'b')
  144. ->will($this->returnValue('thing'));
  145. $this->assertEquals('thing', $articles->authors->crazy('a', 'b'));
  146. }
  147. }