SaveOptionsBuilderTest.php 5.7 KB

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