AssociationProxyTest.php 4.8 KB

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