AssociationProxyTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\TableRegistry;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Tests the features related to proxying methods from the Association
  20. * class to the Table class
  21. */
  22. class AssociationProxyTest extends TestCase
  23. {
  24. /**
  25. * Fixtures to be loaded
  26. *
  27. * @var array
  28. */
  29. public $fixtures = [
  30. 'core.articles', 'core.authors', 'core.comments'
  31. ];
  32. /**
  33. * Teardown
  34. *
  35. * @return void
  36. */
  37. public function tearDown()
  38. {
  39. parent::tearDown();
  40. TableRegistry::clear();
  41. }
  42. /**
  43. * Tests that it is possible to get associations as a property
  44. *
  45. * @return void
  46. */
  47. public function testAssociationAsProperty()
  48. {
  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. * @return void
  62. */
  63. public function testGetBadAssociation()
  64. {
  65. $this->expectException(\RuntimeException::class);
  66. $this->expectExceptionMessage('Table "Cake\ORM\Table" is not associated with "posts"');
  67. $articles = TableRegistry::get('articles');
  68. $articles->posts;
  69. }
  70. /**
  71. * Test that find() with empty conditions generates valid SQL
  72. *
  73. * @return void
  74. */
  75. public function testFindEmptyConditions()
  76. {
  77. $table = TableRegistry::get('Users');
  78. $table->hasMany('Articles', [
  79. 'foreignKey' => 'author_id',
  80. 'conditions' => '',
  81. ]);
  82. $query = $table->Articles->find('list', ['limit' => 2]);
  83. $this->assertCount(2, $query->all());
  84. }
  85. /**
  86. * Tests that the proxied updateAll will preserve conditions set for the association
  87. *
  88. * @return void
  89. */
  90. public function testUpdateAllFromAssociation()
  91. {
  92. $articles = TableRegistry::get('articles');
  93. $comments = TableRegistry::get('comments');
  94. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  95. $articles->comments->updateAll(['comment' => 'changed'], ['article_id' => 1]);
  96. $changed = $comments->find()->where(['comment' => 'changed'])->count();
  97. $this->assertEquals(3, $changed);
  98. }
  99. /**
  100. * Tests that the proxied deleteAll preserves conditions set for the association
  101. *
  102. * @return void
  103. */
  104. public function testDeleteAllFromAssociation()
  105. {
  106. $articles = TableRegistry::get('articles');
  107. $comments = TableRegistry::get('comments');
  108. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  109. $articles->comments->deleteAll(['article_id' => 1]);
  110. $remaining = $comments->find()->where(['article_id' => 1])->count();
  111. $this->assertEquals(1, $remaining);
  112. }
  113. /**
  114. * Tests that it is possible to get associations as a property
  115. *
  116. * @return void
  117. */
  118. public function testAssociationAsPropertyProxy()
  119. {
  120. $articles = TableRegistry::get('articles');
  121. $authors = TableRegistry::get('authors');
  122. $articles->belongsTo('authors');
  123. $authors->hasMany('comments');
  124. $this->assertTrue(isset($articles->authors->comments));
  125. $this->assertSame($authors->association('comments'), $articles->authors->comments);
  126. }
  127. /**
  128. * Tests that methods are proxied from the Association to the target table
  129. *
  130. * @return void
  131. */
  132. public function testAssociationMethodProxy()
  133. {
  134. $articles = TableRegistry::get('articles');
  135. $mock = $this->getMockBuilder('Cake\ORM\Table')
  136. ->setMethods(['crazy'])
  137. ->getMock();
  138. $articles->belongsTo('authors', [
  139. 'targetTable' => $mock
  140. ]);
  141. $mock->expects($this->once())->method('crazy')
  142. ->with('a', 'b')
  143. ->will($this->returnValue('thing'));
  144. $this->assertEquals('thing', $articles->authors->crazy('a', 'b'));
  145. }
  146. }