AssociationProxyTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * Tests that the proxied updateAll will preserve conditions set for the association
  75. *
  76. * @return void
  77. */
  78. public function testUpdateAllFromAssociation()
  79. {
  80. $articles = TableRegistry::get('articles');
  81. $comments = TableRegistry::get('comments');
  82. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  83. $articles->comments->updateAll(['comment' => 'changed'], ['article_id' => 1]);
  84. $changed = $comments->find()->where(['comment' => 'changed'])->count();
  85. $this->assertEquals(3, $changed);
  86. }
  87. /**
  88. * Tests that the proxied deleteAll preserves conditions set for the association
  89. *
  90. * @return void
  91. */
  92. public function testDeleteAllFromAssociation()
  93. {
  94. $articles = TableRegistry::get('articles');
  95. $comments = TableRegistry::get('comments');
  96. $articles->hasMany('comments', ['conditions' => ['published' => 'Y']]);
  97. $articles->comments->deleteAll(['article_id' => 1]);
  98. $remaining = $comments->find()->where(['article_id' => 1])->count();
  99. $this->assertEquals(1, $remaining);
  100. }
  101. /**
  102. * Tests that it is possible to get associations as a property
  103. *
  104. * @return void
  105. */
  106. public function testAssociationAsPropertyProxy()
  107. {
  108. $articles = TableRegistry::get('articles');
  109. $authors = TableRegistry::get('authors');
  110. $articles->belongsTo('authors');
  111. $authors->hasMany('comments');
  112. $this->assertTrue(isset($articles->authors->comments));
  113. $this->assertSame($authors->association('comments'), $articles->authors->comments);
  114. }
  115. /**
  116. * Tests that methods are proxied from the Association to the target table
  117. *
  118. * @return void
  119. */
  120. public function testAssociationMethodProxy()
  121. {
  122. $articles = TableRegistry::get('articles');
  123. $mock = $this->getMock('Cake\ORM\Table', ['crazy']);
  124. $articles->belongsTo('authors', [
  125. 'targetTable' => $mock
  126. ]);
  127. $mock->expects($this->once())->method('crazy')
  128. ->with('a', 'b')
  129. ->will($this->returnValue('thing'));
  130. $this->assertEquals('thing', $articles->authors->crazy('a', 'b'));
  131. }
  132. }