SaveOptionsBuilderTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\Datasource\ConnectionManager;
  17. use Cake\ORM\Entity;
  18. use Cake\ORM\SaveOptionsBuilder;
  19. use Cake\ORM\Table;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * SaveOptionsBuilder test case.
  24. */
  25. class SaveOptionsBuilderTest extends TestCase
  26. {
  27. public $fixtures = [
  28. 'core.articles',
  29. 'core.authors',
  30. 'core.comments',
  31. 'core.users',
  32. ];
  33. /**
  34. * setup
  35. *
  36. * @return void
  37. */
  38. public function setUp()
  39. {
  40. parent::setUp();
  41. $this->connection = ConnectionManager::get('test');
  42. $this->table = new Table([
  43. 'table' => 'articles',
  44. 'connection' => $this->connection,
  45. ]);
  46. $this->table->belongsTo('Authors');
  47. $this->table->hasMany('Comments');
  48. $this->table->Comments->belongsTo('Users');
  49. }
  50. /**
  51. * testAssociatedChecks
  52. *
  53. * @return void
  54. */
  55. public function testAssociatedChecks()
  56. {
  57. $expected = [
  58. 'associated' => [
  59. 'Comments' => []
  60. ]
  61. ];
  62. $builder = new SaveOptionsBuilder($this->table);
  63. $builder->associated(
  64. 'Comments'
  65. );
  66. $result = $builder->toArray();
  67. $this->assertEquals($expected, $result);
  68. $expected = [
  69. 'associated' => [
  70. 'Comments' => [
  71. 'associated' => [
  72. 'Users' => []
  73. ]
  74. ]
  75. ]
  76. ];
  77. $builder = new SaveOptionsBuilder($this->table);
  78. $builder->associated(
  79. 'Comments.Users'
  80. );
  81. $result = $builder->toArray();
  82. $this->assertEquals($expected, $result);
  83. try {
  84. $builder = new SaveOptionsBuilder($this->table);
  85. $builder->associated(
  86. 'Comments.DoesNotExist'
  87. );
  88. $this->fail('No \RuntimeException throw for invalid association!');
  89. } catch (\RuntimeException $e) {
  90. }
  91. $expected = [
  92. 'associated' => [
  93. 'Comments' => [
  94. 'associated' => [
  95. (int)0 => 'Users'
  96. ]
  97. ]
  98. ]
  99. ];
  100. $builder = new SaveOptionsBuilder($this->table);
  101. $builder->associated([
  102. 'Comments' => [
  103. 'associated' => [
  104. 'Users'
  105. ]
  106. ]
  107. ]);
  108. $result = $builder->toArray();
  109. $this->assertEquals($expected, $result);
  110. $expected = [
  111. 'associated' => [
  112. 'Authors' => [],
  113. 'Comments' => [
  114. 'associated' => [
  115. (int)0 => 'Users'
  116. ]
  117. ]
  118. ]
  119. ];
  120. $builder = new SaveOptionsBuilder($this->table);
  121. $builder->associated([
  122. 'Authors',
  123. 'Comments' => [
  124. 'associated' => [
  125. 'Users'
  126. ]
  127. ]
  128. ]);
  129. $result = $builder->toArray();
  130. $this->assertEquals($expected, $result);
  131. }
  132. /**
  133. * testBuilder
  134. *
  135. * @return void
  136. */
  137. public function testBuilder()
  138. {
  139. $expected = [
  140. 'associated' => [
  141. 'Authors' => [],
  142. 'Comments' => [
  143. 'associated' => [
  144. (int)0 => 'Users'
  145. ]
  146. ]
  147. ],
  148. 'guard' => false,
  149. 'checkRules' => false,
  150. 'checkExisting' => true,
  151. 'atomic' => true,
  152. 'validate' => 'default'
  153. ];
  154. $builder = new SaveOptionsBuilder($this->table);
  155. $builder->associated([
  156. 'Authors',
  157. 'Comments' => [
  158. 'associated' => [
  159. 'Users'
  160. ]
  161. ]
  162. ])
  163. ->guard(false)
  164. ->checkRules(false)
  165. ->checkExisting(true)
  166. ->atomic(true)
  167. ->validate('default');
  168. $result = $builder->toArray();
  169. $this->assertEquals($expected, $result);
  170. }
  171. /**
  172. * testParseOptionsArray
  173. *
  174. * @return void
  175. */
  176. public function testParseOptionsArray()
  177. {
  178. $options = [
  179. 'associated' => [
  180. 'Authors' => [],
  181. 'Comments' => [
  182. 'associated' => [
  183. (int)0 => 'Users'
  184. ]
  185. ]
  186. ],
  187. 'guard' => false,
  188. 'checkRules' => false,
  189. 'checkExisting' => true,
  190. 'atomic' => true,
  191. 'validate' => 'default'
  192. ];
  193. $builder = new SaveOptionsBuilder($this->table, $options);
  194. $this->assertEquals($options, $builder->toArray());
  195. }
  196. /**
  197. * testSettingCustomOptions
  198. *
  199. * @return void
  200. */
  201. public function testSettingCustomOptions()
  202. {
  203. $expected = [
  204. 'myOption' => true,
  205. ];
  206. $builder = new SaveOptionsBuilder($this->table);
  207. $builder->set('myOption', true);
  208. $this->assertEquals($expected, $builder->toArray());
  209. }
  210. }