CaseStatementExpressionTest.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\Expression;
  17. use Cake\Chronos\Chronos;
  18. use Cake\Chronos\ChronosDate;
  19. use Cake\Database\Expression\CaseStatementExpression;
  20. use Cake\Database\Expression\ComparisonExpression;
  21. use Cake\Database\Expression\FunctionExpression;
  22. use Cake\Database\Expression\IdentifierExpression;
  23. use Cake\Database\Expression\QueryExpression;
  24. use Cake\Database\Expression\WhenThenExpression;
  25. use Cake\Database\TypeFactory;
  26. use Cake\Database\TypeMap;
  27. use Cake\Database\ValueBinder;
  28. use Cake\Datasource\ConnectionManager;
  29. use Cake\I18n\Date;
  30. use Cake\I18n\DateTime;
  31. use Cake\Test\test_app\TestApp\Database\Expression\CustomWhenThenExpression;
  32. use Cake\Test\test_app\TestApp\Stub\CaseStatementExpressionStub;
  33. use Cake\Test\test_app\TestApp\Stub\WhenThenExpressionStub;
  34. use Cake\TestSuite\TestCase;
  35. use InvalidArgumentException;
  36. use LogicException;
  37. use PHPUnit\Framework\Attributes\DataProvider;
  38. use stdClass;
  39. use TestApp\Database\Type\CustomExpressionType;
  40. use TestApp\View\Object\TestObjectWithToString;
  41. use TypeError;
  42. class CaseStatementExpressionTest extends TestCase
  43. {
  44. // region Type handling
  45. public function testExpressionTypeCastingSimpleCase(): void
  46. {
  47. TypeFactory::map('custom', CustomExpressionType::class);
  48. $expression = (new CaseStatementExpression(1, 'custom'))
  49. ->when(1, 'custom')
  50. ->then(2, 'custom')
  51. ->else(3, 'custom');
  52. $valueBinder = new ValueBinder();
  53. $sql = $expression->sql($valueBinder);
  54. $this->assertSame(
  55. 'CASE CUSTOM(:param0) WHEN CUSTOM(:param1) THEN CUSTOM(:param2) ELSE CUSTOM(:param3) END',
  56. $sql
  57. );
  58. }
  59. public function testExpressionTypeCastingNullValues(): void
  60. {
  61. TypeFactory::map('custom', CustomExpressionType::class);
  62. $expression = (new CaseStatementExpression(null, 'custom'))
  63. ->when(1, 'custom')
  64. ->then(null, 'custom')
  65. ->else(null, 'custom');
  66. $valueBinder = new ValueBinder();
  67. $sql = $expression->sql($valueBinder);
  68. $this->assertSame(
  69. 'CASE CUSTOM(:param0) WHEN CUSTOM(:param1) THEN CUSTOM(:param2) ELSE CUSTOM(:param3) END',
  70. $sql
  71. );
  72. }
  73. public function testExpressionTypeCastingSearchedCase(): void
  74. {
  75. TypeFactory::map('custom', CustomExpressionType::class);
  76. $expression = (new CaseStatementExpression())
  77. ->when(['Table.column' => true], ['Table.column' => 'custom'])
  78. ->then(1, 'custom')
  79. ->else(2, 'custom');
  80. $valueBinder = new ValueBinder();
  81. $sql = $expression->sql($valueBinder);
  82. $this->assertSame(
  83. 'CASE WHEN Table.column = (CUSTOM(:param0)) THEN CUSTOM(:param1) ELSE CUSTOM(:param2) END',
  84. $sql
  85. );
  86. }
  87. public function testGetReturnType(): void
  88. {
  89. // all provided `then` and `else` types are the same, return
  90. // type can be inferred
  91. $expression = (new CaseStatementExpression())
  92. ->when(['Table.column_a' => true])
  93. ->then(1, 'integer')
  94. ->when(['Table.column_b' => true])
  95. ->then(2, 'integer')
  96. ->else(3, 'integer');
  97. $this->assertSame('integer', $expression->getReturnType());
  98. // all provided `then` an `else` types are the same, one `then`
  99. // type is `null`, return type can be inferred
  100. $expression = (new CaseStatementExpression())
  101. ->when(['Table.column_a' => true])
  102. ->then(1)
  103. ->when(['Table.column_b' => true])
  104. ->then(2, 'integer')
  105. ->else(3, 'integer');
  106. $this->assertSame('integer', $expression->getReturnType());
  107. // all `then` types are null, an `else` type was provided,
  108. // return type can be inferred
  109. $expression = (new CaseStatementExpression())
  110. ->when(['Table.column_a' => true])
  111. ->then(1)
  112. ->when(['Table.column_b' => true])
  113. ->then(2)
  114. ->else(3, 'integer');
  115. $this->assertSame('integer', $expression->getReturnType());
  116. // all provided `then` types are the same, the `else` type is
  117. // `null`, return type can be inferred
  118. $expression = (new CaseStatementExpression())
  119. ->when(['Table.column_a' => true])
  120. ->then(1, 'integer')
  121. ->when(['Table.column_b' => true])
  122. ->then(2, 'integer')
  123. ->else(3);
  124. $this->assertSame('integer', $expression->getReturnType());
  125. // no `then` or `else` types were provided, they are all `null`,
  126. // and will be derived from the passed value, return type can be
  127. // inferred
  128. $expression = (new CaseStatementExpression())
  129. ->when(['Table.column_a' => true])
  130. ->then(1)
  131. ->when(['Table.column_b' => true])
  132. ->then(2)
  133. ->else(3);
  134. $this->assertSame('integer', $expression->getReturnType());
  135. // all `then` and `else` point to columns of the same type,
  136. // return type can be inferred
  137. $typeMap = new TypeMap([
  138. 'Table.column_a' => 'boolean',
  139. 'Table.column_b' => 'boolean',
  140. 'Table.column_c' => 'boolean',
  141. ]);
  142. $expression = (new CaseStatementExpression())
  143. ->setTypeMap($typeMap)
  144. ->when(['Table.column_a' => true])
  145. ->then(new IdentifierExpression('Table.column_a'))
  146. ->when(['Table.column_b' => true])
  147. ->then(new IdentifierExpression('Table.column_b'))
  148. ->else(new IdentifierExpression('Table.column_c'));
  149. $this->assertSame('boolean', $expression->getReturnType());
  150. // all `then` and `else` use the same custom type, return type
  151. // can be inferred
  152. $expression = (new CaseStatementExpression())
  153. ->when(['Table.column_a' => true])
  154. ->then(1, 'custom')
  155. ->when(['Table.column_b' => true])
  156. ->then(2, 'custom')
  157. ->else(3, 'custom');
  158. $this->assertSame('custom', $expression->getReturnType());
  159. // all `then` and `else` types were provided, but an explicit
  160. // return type was set, return type will be overwritten
  161. $expression = (new CaseStatementExpression())
  162. ->when(['Table.column_a' => true])
  163. ->then(1, 'integer')
  164. ->when(['Table.column_b' => true])
  165. ->then(2, 'integer')
  166. ->else(3, 'integer')
  167. ->setReturnType('string');
  168. $this->assertSame('string', $expression->getReturnType());
  169. // all `then` and `else` types are different, return type
  170. // cannot be inferred
  171. $expression = (new CaseStatementExpression())
  172. ->when(['Table.column_a' => true])
  173. ->then(true)
  174. ->when(['Table.column_b' => true])
  175. ->then(1)
  176. ->else(null);
  177. $this->assertSame('string', $expression->getReturnType());
  178. }
  179. public function testSetReturnType(): void
  180. {
  181. $expression = (new CaseStatementExpression())->else('1');
  182. $this->assertSame('string', $expression->getReturnType());
  183. $expression->setReturnType('float');
  184. $this->assertSame('float', $expression->getReturnType());
  185. }
  186. public static function valueTypeInferenceDataProvider(): array
  187. {
  188. return [
  189. // Values that should have their type inferred because
  190. // they will be bound by the case expression.
  191. ['1', 'string'],
  192. [1, 'integer'],
  193. [1.0, 'float'],
  194. [true, 'boolean'],
  195. [ChronosDate::now(), 'date'],
  196. [Chronos::now(), 'datetime'],
  197. // Values that should not have a type inferred, either
  198. // because they are not bound by the case expression,
  199. // and/or because their type is obtained differently
  200. // (for example from a type map).
  201. [new IdentifierExpression('Table.column'), null],
  202. [new FunctionExpression('SUM', ['Table.column' => 'literal'], [], 'integer'), null],
  203. [new stdClass(), null],
  204. [null, null],
  205. ];
  206. }
  207. /**
  208. * @param mixed $value The value from which to infer the type.
  209. * @param string|null $type The expected type.
  210. */
  211. #[DataProvider('valueTypeInferenceDataProvider')]
  212. public function testInferValueType($value, ?string $type): void
  213. {
  214. $expression = new CaseStatementExpressionStub();
  215. $this->assertNull($expression->getValueType());
  216. $expression = (new CaseStatementExpressionStub($value))
  217. ->setTypeMap(new TypeMap(['Table.column' => 'boolean']))
  218. ->when(1)
  219. ->then(2);
  220. $this->assertSame($type, $expression->getValueType());
  221. }
  222. public static function whenTypeInferenceDataProvider(): array
  223. {
  224. return [
  225. // Values that should have their type inferred because
  226. // they will be bound by the case expression.
  227. ['1', 'string'],
  228. [1, 'integer'],
  229. [1.0, 'float'],
  230. [true, 'boolean'],
  231. [ChronosDate::now(), 'date'],
  232. [Chronos::now(), 'datetime'],
  233. // Values that should not have a type inferred, either
  234. // because they are not bound by the case expression,
  235. // and/or because their type is obtained differently
  236. // (for example from a type map).
  237. [new IdentifierExpression('Table.column'), null],
  238. [new FunctionExpression('SUM', ['Table.column' => 'literal'], [], 'integer'), null],
  239. [['Table.column' => true], null],
  240. [new stdClass(), null],
  241. ];
  242. }
  243. /**
  244. * @param mixed $value The value from which to infer the type.
  245. * @param string|null $type The expected type.
  246. */
  247. #[DataProvider('whenTypeInferenceDataProvider')]
  248. public function testInferWhenType($value, ?string $type): void
  249. {
  250. $expression = (new CaseStatementExpressionStub())
  251. ->setTypeMap(new TypeMap(['Table.column' => 'boolean']));
  252. $expression->when(new WhenThenExpressionStub($expression->getTypeMap()));
  253. $this->assertNull($expression->clause('when')[0]->getWhenType());
  254. $expression->clause('when')[0]
  255. ->when($value)
  256. ->then(1);
  257. $this->assertSame($type, $expression->clause('when')[0]->getWhenType());
  258. }
  259. public static function resultTypeInferenceDataProvider(): array
  260. {
  261. return [
  262. // Unless a result type has been set manually, values
  263. // should have their type inferred when possible.
  264. ['1', 'string'],
  265. [1, 'integer'],
  266. [1.0, 'float'],
  267. [true, 'boolean'],
  268. [ChronosDate::now(), 'date'],
  269. [Chronos::now(), 'datetime'],
  270. [new IdentifierExpression('Table.column'), 'boolean'],
  271. [new FunctionExpression('SUM', ['Table.column' => 'literal'], [], 'integer'), 'integer'],
  272. [new stdClass(), null],
  273. [null, null],
  274. ];
  275. }
  276. /**
  277. * @param mixed $value The value from which to infer the type.
  278. * @param string|null $type The expected type.
  279. */
  280. #[DataProvider('resultTypeInferenceDataProvider')]
  281. public function testInferResultType($value, ?string $type): void
  282. {
  283. $expression = (new CaseStatementExpressionStub())
  284. ->setTypeMap(new TypeMap(['Table.column' => 'boolean']))
  285. ->when(function (WhenThenExpression $whenThen) {
  286. return $whenThen;
  287. });
  288. $this->assertNull($expression->clause('when')[0]->getResultType());
  289. $expression->clause('when')[0]
  290. ->when(['Table.column' => true])
  291. ->then($value);
  292. $this->assertSame($type, $expression->clause('when')[0]->getResultType());
  293. }
  294. /**
  295. * @param mixed $value The value from which to infer the type.
  296. * @param string|null $type The expected type.
  297. */
  298. #[DataProvider('resultTypeInferenceDataProvider')]
  299. public function testInferElseType($value, ?string $type): void
  300. {
  301. $expression = new CaseStatementExpressionStub();
  302. $this->assertNull($expression->getElseType());
  303. $expression = (new CaseStatementExpressionStub())
  304. ->setTypeMap(new TypeMap(['Table.column' => 'boolean']));
  305. $this->assertNull($expression->getElseType());
  306. $expression->else($value);
  307. $this->assertSame($type, $expression->getElseType());
  308. }
  309. public function testWhenArrayValueInheritTypeMap(): void
  310. {
  311. $typeMap = new TypeMap([
  312. 'Table.column_a' => 'boolean',
  313. 'Table.column_b' => 'string',
  314. ]);
  315. $expression = (new CaseStatementExpression())
  316. ->setTypeMap($typeMap)
  317. ->when(['Table.column_a' => true])
  318. ->then(1)
  319. ->when(['Table.column_b' => 'foo'])
  320. ->then(2)
  321. ->else(3);
  322. $valueBinder = new ValueBinder();
  323. $sql = $expression->sql($valueBinder);
  324. $this->assertSame(
  325. 'CASE WHEN Table.column_a = :c0 THEN :c1 WHEN Table.column_b = :c2 THEN :c3 ELSE :c4 END',
  326. $sql
  327. );
  328. $this->assertSame(
  329. [
  330. ':c0' => [
  331. 'value' => true,
  332. 'type' => 'boolean',
  333. 'placeholder' => 'c0',
  334. ],
  335. ':c1' => [
  336. 'value' => 1,
  337. 'type' => 'integer',
  338. 'placeholder' => 'c1',
  339. ],
  340. ':c2' => [
  341. 'value' => 'foo',
  342. 'type' => 'string',
  343. 'placeholder' => 'c2',
  344. ],
  345. ':c3' => [
  346. 'value' => 2,
  347. 'type' => 'integer',
  348. 'placeholder' => 'c3',
  349. ],
  350. ':c4' => [
  351. 'value' => 3,
  352. 'type' => 'integer',
  353. 'placeholder' => 'c4',
  354. ],
  355. ],
  356. $valueBinder->bindings()
  357. );
  358. }
  359. public function testWhenArrayValueWithExplicitTypes(): void
  360. {
  361. $typeMap = new TypeMap([
  362. 'Table.column_a' => 'boolean',
  363. 'Table.column_b' => 'string',
  364. ]);
  365. $expression = (new CaseStatementExpression())
  366. ->setTypeMap($typeMap)
  367. ->when(['Table.column_a' => 123], ['Table.column_a' => 'integer'])
  368. ->then(1)
  369. ->when(['Table.column_b' => 'foo'])
  370. ->then(2)
  371. ->else(3);
  372. $valueBinder = new ValueBinder();
  373. $sql = $expression->sql($valueBinder);
  374. $this->assertSame(
  375. 'CASE WHEN Table.column_a = :c0 THEN :c1 WHEN Table.column_b = :c2 THEN :c3 ELSE :c4 END',
  376. $sql
  377. );
  378. $this->assertSame(
  379. [
  380. ':c0' => [
  381. 'value' => 123,
  382. 'type' => 'integer',
  383. 'placeholder' => 'c0',
  384. ],
  385. ':c1' => [
  386. 'value' => 1,
  387. 'type' => 'integer',
  388. 'placeholder' => 'c1',
  389. ],
  390. ':c2' => [
  391. 'value' => 'foo',
  392. 'type' => 'string',
  393. 'placeholder' => 'c2',
  394. ],
  395. ':c3' => [
  396. 'value' => 2,
  397. 'type' => 'integer',
  398. 'placeholder' => 'c3',
  399. ],
  400. ':c4' => [
  401. 'value' => 3,
  402. 'type' => 'integer',
  403. 'placeholder' => 'c4',
  404. ],
  405. ],
  406. $valueBinder->bindings()
  407. );
  408. }
  409. public function testWhenCallableArrayValueInheritTypeMap(): void
  410. {
  411. $typeMap = new TypeMap([
  412. 'Table.column_a' => 'boolean',
  413. 'Table.column_b' => 'string',
  414. ]);
  415. $expression = (new CaseStatementExpression())
  416. ->setTypeMap($typeMap)
  417. ->when(function (WhenThenExpression $whenThen) {
  418. return $whenThen
  419. ->when(['Table.column_a' => true])
  420. ->then(1);
  421. })
  422. ->when(function (WhenThenExpression $whenThen) {
  423. return $whenThen
  424. ->when(['Table.column_b' => 'foo'])
  425. ->then(2);
  426. })
  427. ->else(3);
  428. $valueBinder = new ValueBinder();
  429. $sql = $expression->sql($valueBinder);
  430. $this->assertSame(
  431. 'CASE WHEN Table.column_a = :c0 THEN :c1 WHEN Table.column_b = :c2 THEN :c3 ELSE :c4 END',
  432. $sql
  433. );
  434. $this->assertSame(
  435. [
  436. ':c0' => [
  437. 'value' => true,
  438. 'type' => 'boolean',
  439. 'placeholder' => 'c0',
  440. ],
  441. ':c1' => [
  442. 'value' => 1,
  443. 'type' => 'integer',
  444. 'placeholder' => 'c1',
  445. ],
  446. ':c2' => [
  447. 'value' => 'foo',
  448. 'type' => 'string',
  449. 'placeholder' => 'c2',
  450. ],
  451. ':c3' => [
  452. 'value' => 2,
  453. 'type' => 'integer',
  454. 'placeholder' => 'c3',
  455. ],
  456. ':c4' => [
  457. 'value' => 3,
  458. 'type' => 'integer',
  459. 'placeholder' => 'c4',
  460. ],
  461. ],
  462. $valueBinder->bindings()
  463. );
  464. }
  465. public function testWhenCallableArrayValueWithExplicitTypes(): void
  466. {
  467. $typeMap = new TypeMap([
  468. 'Table.column_a' => 'boolean',
  469. 'Table.column_b' => 'string',
  470. ]);
  471. $expression = (new CaseStatementExpression())
  472. ->setTypeMap($typeMap)
  473. ->when(function (WhenThenExpression $whenThen) {
  474. return $whenThen
  475. ->when(['Table.column_a' => 123], ['Table.column_a' => 'integer'])
  476. ->then(1);
  477. })
  478. ->when(function (WhenThenExpression $whenThen) {
  479. return $whenThen
  480. ->when(['Table.column_b' => 'foo'])
  481. ->then(2);
  482. })
  483. ->else(3);
  484. $valueBinder = new ValueBinder();
  485. $sql = $expression->sql($valueBinder);
  486. $this->assertSame(
  487. 'CASE WHEN Table.column_a = :c0 THEN :c1 WHEN Table.column_b = :c2 THEN :c3 ELSE :c4 END',
  488. $sql
  489. );
  490. $this->assertSame(
  491. [
  492. ':c0' => [
  493. 'value' => 123,
  494. 'type' => 'integer',
  495. 'placeholder' => 'c0',
  496. ],
  497. ':c1' => [
  498. 'value' => 1,
  499. 'type' => 'integer',
  500. 'placeholder' => 'c1',
  501. ],
  502. ':c2' => [
  503. 'value' => 'foo',
  504. 'type' => 'string',
  505. 'placeholder' => 'c2',
  506. ],
  507. ':c3' => [
  508. 'value' => 2,
  509. 'type' => 'integer',
  510. 'placeholder' => 'c3',
  511. ],
  512. ':c4' => [
  513. 'value' => 3,
  514. 'type' => 'integer',
  515. 'placeholder' => 'c4',
  516. ],
  517. ],
  518. $valueBinder->bindings()
  519. );
  520. }
  521. public function testWhenArrayValueRequiresArrayTypeValue(): void
  522. {
  523. $this->expectException(InvalidArgumentException::class);
  524. $this->expectExceptionMessage(
  525. 'When using an array for the `$when` argument, the `$type` ' .
  526. 'argument must be an array too, `string` given.'
  527. );
  528. (new CaseStatementExpression())
  529. ->when(['Table.column' => 123], 'integer')
  530. ->then(1);
  531. }
  532. public function testWhenNonArrayValueRequiresStringTypeValue(): void
  533. {
  534. $this->expectException(InvalidArgumentException::class);
  535. $this->expectExceptionMessage(
  536. 'When using a non-array value for the `$when` argument, ' .
  537. 'the `$type` argument must be a string, `array` given.'
  538. );
  539. (new CaseStatementExpression())
  540. ->when(123, ['Table.column' => 'integer'])
  541. ->then(1);
  542. }
  543. public function testInternalTypeMapChangesAreNonPersistent(): void
  544. {
  545. $typeMap = new TypeMap([
  546. 'Table.column' => 'integer',
  547. ]);
  548. $expression = (new CaseStatementExpression())
  549. ->setTypeMap($typeMap)
  550. ->when(['Table.column' => 123])
  551. ->then(1)
  552. ->when(['Table.column' => 'foo'], ['Table.column' => 'string'])
  553. ->then('bar')
  554. ->when(['Table.column' => 456])
  555. ->then(2);
  556. $valueBinder = new ValueBinder();
  557. $expression->sql($valueBinder);
  558. $this->assertSame(
  559. [
  560. ':c0' => [
  561. 'value' => 123,
  562. 'type' => 'integer',
  563. 'placeholder' => 'c0',
  564. ],
  565. ':c1' => [
  566. 'value' => 1,
  567. 'type' => 'integer',
  568. 'placeholder' => 'c1',
  569. ],
  570. ':c2' => [
  571. 'value' => 'foo',
  572. 'type' => 'string',
  573. 'placeholder' => 'c2',
  574. ],
  575. ':c3' => [
  576. 'value' => 'bar',
  577. 'type' => 'string',
  578. 'placeholder' => 'c3',
  579. ],
  580. ':c4' => [
  581. 'value' => 456,
  582. 'type' => 'integer',
  583. 'placeholder' => 'c4',
  584. ],
  585. ':c5' => [
  586. 'value' => 2,
  587. 'type' => 'integer',
  588. 'placeholder' => 'c5',
  589. ],
  590. ],
  591. $valueBinder->bindings()
  592. );
  593. $this->assertSame($typeMap, $expression->getTypeMap());
  594. }
  595. // endregion
  596. // region SQL injections
  597. public function testSqlInjectionViaTypedCaseValueIsNotPossible(): void
  598. {
  599. $expression = (new CaseStatementExpression('1 THEN 1 END; DELETE * FROM foo; --', 'integer'))
  600. ->when(1)
  601. ->then(2);
  602. $valueBinder = new ValueBinder();
  603. $sql = $expression->sql($valueBinder);
  604. $this->assertSame(
  605. 'CASE :c0 WHEN :c1 THEN :c2 ELSE NULL END',
  606. $sql
  607. );
  608. $this->assertSame(
  609. [
  610. ':c0' => [
  611. 'value' => '1 THEN 1 END; DELETE * FROM foo; --',
  612. 'type' => 'integer',
  613. 'placeholder' => 'c0',
  614. ],
  615. ':c1' => [
  616. 'value' => 1,
  617. 'type' => 'integer',
  618. 'placeholder' => 'c1',
  619. ],
  620. ':c2' => [
  621. 'value' => 2,
  622. 'type' => 'integer',
  623. 'placeholder' => 'c2',
  624. ],
  625. ],
  626. $valueBinder->bindings()
  627. );
  628. }
  629. public function testSqlInjectionViaUntypedCaseValueIsNotPossible(): void
  630. {
  631. $expression = (new CaseStatementExpression('1 THEN 1 END; DELETE * FROM foo; --'))
  632. ->when(1)
  633. ->then(2);
  634. $valueBinder = new ValueBinder();
  635. $sql = $expression->sql($valueBinder);
  636. $this->assertSame(
  637. 'CASE :c0 WHEN :c1 THEN :c2 ELSE NULL END',
  638. $sql
  639. );
  640. $this->assertSame(
  641. [
  642. ':c0' => [
  643. 'value' => '1 THEN 1 END; DELETE * FROM foo; --',
  644. 'type' => 'string',
  645. 'placeholder' => 'c0',
  646. ],
  647. ':c1' => [
  648. 'value' => 1,
  649. 'type' => 'integer',
  650. 'placeholder' => 'c1',
  651. ],
  652. ':c2' => [
  653. 'value' => 2,
  654. 'type' => 'integer',
  655. 'placeholder' => 'c2',
  656. ],
  657. ],
  658. $valueBinder->bindings()
  659. );
  660. }
  661. public function testSqlInjectionViaTypedWhenValueIsNotPossible(): void
  662. {
  663. $expression = (new CaseStatementExpression())
  664. ->when('1 THEN 1 END; DELETE * FROM foo; --', 'integer')
  665. ->then(1);
  666. $valueBinder = new ValueBinder();
  667. $sql = $expression->sql($valueBinder);
  668. $this->assertSame(
  669. 'CASE WHEN :c0 THEN :c1 ELSE NULL END',
  670. $sql
  671. );
  672. $this->assertSame(
  673. [
  674. ':c0' => [
  675. 'value' => '1 THEN 1 END; DELETE * FROM foo; --',
  676. 'type' => 'integer',
  677. 'placeholder' => 'c0',
  678. ],
  679. ':c1' => [
  680. 'value' => 1,
  681. 'type' => 'integer',
  682. 'placeholder' => 'c1',
  683. ],
  684. ],
  685. $valueBinder->bindings()
  686. );
  687. }
  688. public function testSqlInjectionViaTypedWhenArrayValueIsNotPossible(): void
  689. {
  690. $this->expectException(InvalidArgumentException::class);
  691. $this->expectExceptionMessage(
  692. 'When using an array for the `$when` argument, the `$type` ' .
  693. 'argument must be an array too, `string` given.'
  694. );
  695. (new CaseStatementExpression())
  696. ->when(['1 THEN 1 END; DELETE * FROM foo; --' => '123'], 'integer')
  697. ->then(1);
  698. }
  699. public function testSqlInjectionViaUntypedWhenValueIsNotPossible(): void
  700. {
  701. $expression = (new CaseStatementExpression())
  702. ->when('1 THEN 1 END; DELETE * FROM foo; --')
  703. ->then(1);
  704. $valueBinder = new ValueBinder();
  705. $sql = $expression->sql($valueBinder);
  706. $this->assertSame(
  707. 'CASE WHEN :c0 THEN :c1 ELSE NULL END',
  708. $sql
  709. );
  710. $this->assertSame(
  711. [
  712. ':c0' => [
  713. 'value' => '1 THEN 1 END; DELETE * FROM foo; --',
  714. 'type' => 'string',
  715. 'placeholder' => 'c0',
  716. ],
  717. ':c1' => [
  718. 'value' => 1,
  719. 'type' => 'integer',
  720. 'placeholder' => 'c1',
  721. ],
  722. ],
  723. $valueBinder->bindings()
  724. );
  725. }
  726. public function testSqlInjectionViaTypedThenValueIsNotPossible(): void
  727. {
  728. $expression = (new CaseStatementExpression(1))
  729. ->when(2)
  730. ->then('1 THEN 1 END; DELETE * FROM foo; --', 'integer');
  731. $valueBinder = new ValueBinder();
  732. $sql = $expression->sql($valueBinder);
  733. $this->assertSame(
  734. 'CASE :c0 WHEN :c1 THEN :c2 ELSE NULL END',
  735. $sql
  736. );
  737. $this->assertSame(
  738. [
  739. ':c0' => [
  740. 'value' => 1,
  741. 'type' => 'integer',
  742. 'placeholder' => 'c0',
  743. ],
  744. ':c1' => [
  745. 'value' => 2,
  746. 'type' => 'integer',
  747. 'placeholder' => 'c1',
  748. ],
  749. ':c2' => [
  750. 'value' => '1 THEN 1 END; DELETE * FROM foo; --',
  751. 'type' => 'integer',
  752. 'placeholder' => 'c2',
  753. ],
  754. ],
  755. $valueBinder->bindings()
  756. );
  757. }
  758. public function testSqlInjectionViaUntypedThenValueIsNotPossible(): void
  759. {
  760. $expression = (new CaseStatementExpression(1))
  761. ->when(2)
  762. ->then('1 THEN 1 END; DELETE * FROM foo; --');
  763. $valueBinder = new ValueBinder();
  764. $sql = $expression->sql($valueBinder);
  765. $this->assertSame(
  766. 'CASE :c0 WHEN :c1 THEN :c2 ELSE NULL END',
  767. $sql
  768. );
  769. $this->assertSame(
  770. [
  771. ':c0' => [
  772. 'value' => 1,
  773. 'type' => 'integer',
  774. 'placeholder' => 'c0',
  775. ],
  776. ':c1' => [
  777. 'value' => 2,
  778. 'type' => 'integer',
  779. 'placeholder' => 'c1',
  780. ],
  781. ':c2' => [
  782. 'value' => '1 THEN 1 END; DELETE * FROM foo; --',
  783. 'type' => 'string',
  784. 'placeholder' => 'c2',
  785. ],
  786. ],
  787. $valueBinder->bindings()
  788. );
  789. }
  790. public function testSqlInjectionViaTypedElseValueIsNotPossible(): void
  791. {
  792. $expression = (new CaseStatementExpression(1))
  793. ->when(2)
  794. ->then(3)
  795. ->else('1 THEN 1 END; DELETE * FROM foo; --', 'integer');
  796. $valueBinder = new ValueBinder();
  797. $sql = $expression->sql($valueBinder);
  798. $this->assertSame(
  799. 'CASE :c0 WHEN :c1 THEN :c2 ELSE :c3 END',
  800. $sql
  801. );
  802. $this->assertSame(
  803. [
  804. ':c0' => [
  805. 'value' => 1,
  806. 'type' => 'integer',
  807. 'placeholder' => 'c0',
  808. ],
  809. ':c1' => [
  810. 'value' => 2,
  811. 'type' => 'integer',
  812. 'placeholder' => 'c1',
  813. ],
  814. ':c2' => [
  815. 'value' => 3,
  816. 'type' => 'integer',
  817. 'placeholder' => 'c2',
  818. ],
  819. ':c3' => [
  820. 'value' => '1 THEN 1 END; DELETE * FROM foo; --',
  821. 'type' => 'integer',
  822. 'placeholder' => 'c3',
  823. ],
  824. ],
  825. $valueBinder->bindings()
  826. );
  827. }
  828. public function testSqlInjectionViaUntypedElseValueIsNotPossible(): void
  829. {
  830. $expression = (new CaseStatementExpression(1))
  831. ->when(2)
  832. ->then(3)
  833. ->else('1 THEN 1 END; DELETE * FROM foo; --');
  834. $valueBinder = new ValueBinder();
  835. $sql = $expression->sql($valueBinder);
  836. $this->assertSame(
  837. 'CASE :c0 WHEN :c1 THEN :c2 ELSE :c3 END',
  838. $sql
  839. );
  840. $this->assertSame(
  841. [
  842. ':c0' => [
  843. 'value' => 1,
  844. 'type' => 'integer',
  845. 'placeholder' => 'c0',
  846. ],
  847. ':c1' => [
  848. 'value' => 2,
  849. 'type' => 'integer',
  850. 'placeholder' => 'c1',
  851. ],
  852. ':c2' => [
  853. 'value' => 3,
  854. 'type' => 'integer',
  855. 'placeholder' => 'c2',
  856. ],
  857. ':c3' => [
  858. 'value' => '1 THEN 1 END; DELETE * FROM foo; --',
  859. 'type' => 'string',
  860. 'placeholder' => 'c3',
  861. ],
  862. ],
  863. $valueBinder->bindings()
  864. );
  865. }
  866. // endregion
  867. // region Getters
  868. public function testGetInvalidCaseExpressionClause(): void
  869. {
  870. $this->expectException(InvalidArgumentException::class);
  871. $this->expectExceptionMessage(
  872. 'The `$clause` argument must be one of `value`, `when`, `else`, the given value `invalid` is invalid.'
  873. );
  874. (new CaseStatementExpression())->clause('invalid');
  875. }
  876. public function testGetInvalidWhenThenExpressionClause(): void
  877. {
  878. $this->expectException(InvalidArgumentException::class);
  879. $this->expectExceptionMessage(
  880. 'The `$clause` argument must be one of `when`, `then`, the given value `invalid` is invalid.'
  881. );
  882. (new WhenThenExpression())->clause('invalid');
  883. }
  884. public function testGetValueClause(): void
  885. {
  886. $expression = new CaseStatementExpression();
  887. $this->assertNull($expression->clause('value'));
  888. $expression = (new CaseStatementExpression(1))
  889. ->when(1)
  890. ->then(2);
  891. $this->assertSame(1, $expression->clause('value'));
  892. }
  893. public function testGetWhenClause(): void
  894. {
  895. $when = ['Table.column' => true];
  896. $expression = new CaseStatementExpression();
  897. $this->assertSame([], $expression->clause('when'));
  898. $expression
  899. ->when($when)
  900. ->then(1);
  901. $this->assertCount(1, $expression->clause('when'));
  902. $this->assertInstanceOf(WhenThenExpression::class, $expression->clause('when')[0]);
  903. }
  904. public function testWhenArrayValueGetWhenClause(): void
  905. {
  906. $when = ['Table.column' => true];
  907. $expression = new CaseStatementExpression();
  908. $this->assertSame([], $expression->clause('when'));
  909. $expression
  910. ->when($when)
  911. ->then(1);
  912. $this->assertEquals(
  913. new QueryExpression($when),
  914. $expression->clause('when')[0]->clause('when')
  915. );
  916. }
  917. public function testWhenNonArrayValueGetWhenClause(): void
  918. {
  919. $expression = new CaseStatementExpression();
  920. $this->assertSame([], $expression->clause('when'));
  921. $expression
  922. ->when(1)
  923. ->then(2);
  924. $this->assertSame(1, $expression->clause('when')[0]->clause('when'));
  925. }
  926. public function testWhenGetThenClause(): void
  927. {
  928. $expression = (new CaseStatementExpression())
  929. ->when(function (WhenThenExpression $whenThen) {
  930. return $whenThen;
  931. });
  932. $this->assertNull($expression->clause('when')[0]->clause('then'));
  933. $expression->clause('when')[0]->then(1);
  934. $this->assertSame(1, $expression->clause('when')[0]->clause('then'));
  935. }
  936. public function testGetElseClause(): void
  937. {
  938. $expression = new CaseStatementExpression();
  939. $this->assertNull($expression->clause('else'));
  940. $expression
  941. ->when(['Table.column' => true])
  942. ->then(1)
  943. ->else(2);
  944. $this->assertSame(2, $expression->clause('else'));
  945. }
  946. // endregion
  947. // region Order based syntax
  948. public function testWhenThenElse(): void
  949. {
  950. $expression = (new CaseStatementExpression())
  951. ->when(['Table.column_a' => true, 'Table.column_b IS' => null])
  952. ->then(1)
  953. ->when(['Table.column_c' => true, 'Table.column_d IS NOT' => null])
  954. ->then(2)
  955. ->else(3);
  956. $valueBinder = new ValueBinder();
  957. $sql = $expression->sql($valueBinder);
  958. $this->assertSame(
  959. 'CASE ' .
  960. 'WHEN (Table.column_a = :c0 AND (Table.column_b) IS NULL) THEN :c1 ' .
  961. 'WHEN (Table.column_c = :c2 AND (Table.column_d) IS NOT NULL) THEN :c3 ' .
  962. 'ELSE :c4 ' .
  963. 'END',
  964. $sql
  965. );
  966. }
  967. public function testWhenBeforeClosingThenFails(): void
  968. {
  969. $this->expectException(LogicException::class);
  970. $this->expectExceptionMessage('Cannot call `when()` between `when()` and `then()`.');
  971. (new CaseStatementExpression())
  972. ->when(['Table.column_a' => true])
  973. ->when(['Table.column_b' => true]);
  974. }
  975. public function testElseBeforeClosingThenFails(): void
  976. {
  977. $this->expectException(LogicException::class);
  978. $this->expectExceptionMessage('Cannot call `else()` between `when()` and `then()`.');
  979. (new CaseStatementExpression())
  980. ->when(['Table.column' => true])
  981. ->else(1);
  982. }
  983. public function testThenBeforeOpeningWhenFails(): void
  984. {
  985. $this->expectException(LogicException::class);
  986. $this->expectExceptionMessage('Cannot call `then()` before `when()`.');
  987. (new CaseStatementExpression())
  988. ->then(1);
  989. }
  990. // endregion
  991. // region Callable syntax
  992. public function testWhenCallables(): void
  993. {
  994. $expression = (new CaseStatementExpression())
  995. ->when(function (WhenThenExpression $whenThen) {
  996. return $whenThen
  997. ->when([
  998. 'Table.column_a' => true,
  999. 'Table.column_b IS' => null,
  1000. ])
  1001. ->then(1);
  1002. })
  1003. ->when(function (WhenThenExpression $whenThen) {
  1004. return $whenThen
  1005. ->when([
  1006. 'Table.column_c' => true,
  1007. 'Table.column_d IS NOT' => null,
  1008. ])
  1009. ->then(2);
  1010. })
  1011. ->else(3);
  1012. $valueBinder = new ValueBinder();
  1013. $sql = $expression->sql($valueBinder);
  1014. $this->assertSame(
  1015. 'CASE ' .
  1016. 'WHEN (Table.column_a = :c0 AND (Table.column_b) IS NULL) THEN :c1 ' .
  1017. 'WHEN (Table.column_c = :c2 AND (Table.column_d) IS NOT NULL) THEN :c3 ' .
  1018. 'ELSE :c4 ' .
  1019. 'END',
  1020. $sql
  1021. );
  1022. }
  1023. public function testWhenCallablesWithCustomWhenThenExpressions(): void
  1024. {
  1025. $expression = (new CaseStatementExpression())
  1026. ->when(function () {
  1027. return (new CustomWhenThenExpression())
  1028. ->when([
  1029. 'Table.column_a' => true,
  1030. 'Table.column_b IS' => null,
  1031. ])
  1032. ->then(1);
  1033. })
  1034. ->when(function () {
  1035. return (new CustomWhenThenExpression())
  1036. ->when([
  1037. 'Table.column_c' => true,
  1038. 'Table.column_d IS NOT' => null,
  1039. ])
  1040. ->then(2);
  1041. })
  1042. ->else(3);
  1043. $valueBinder = new ValueBinder();
  1044. $sql = $expression->sql($valueBinder);
  1045. $this->assertSame(
  1046. 'CASE ' .
  1047. 'WHEN (Table.column_a = :c0 AND (Table.column_b) IS NULL) THEN :c1 ' .
  1048. 'WHEN (Table.column_c = :c2 AND (Table.column_d) IS NOT NULL) THEN :c3 ' .
  1049. 'ELSE :c4 ' .
  1050. 'END',
  1051. $sql
  1052. );
  1053. }
  1054. public function testWhenCallablesWithInvalidReturnTypeFails(): void
  1055. {
  1056. $this->expectException(LogicException::class);
  1057. $this->expectExceptionMessage(
  1058. '`when()` callables must return an instance of ' .
  1059. '`\Cake\Database\Expression\WhenThenExpression`, `null` given.'
  1060. );
  1061. $this->deprecated(function (): void {
  1062. (new CaseStatementExpression())
  1063. ->when(function () {
  1064. return null;
  1065. });
  1066. });
  1067. }
  1068. // endregion
  1069. // region Self-contained values
  1070. public function testSelfContainedWhenThenExpressions(): void
  1071. {
  1072. $expression = (new CaseStatementExpression())
  1073. ->when(
  1074. (new WhenThenExpression())
  1075. ->when([
  1076. 'Table.column_a' => true,
  1077. 'Table.column_b IS' => null,
  1078. ])
  1079. ->then(1)
  1080. )
  1081. ->when(
  1082. (new WhenThenExpression())
  1083. ->when([
  1084. 'Table.column_c' => true,
  1085. 'Table.column_d IS NOT' => null,
  1086. ])
  1087. ->then(2)
  1088. )
  1089. ->else(3);
  1090. $valueBinder = new ValueBinder();
  1091. $sql = $expression->sql($valueBinder);
  1092. $this->assertSame(
  1093. 'CASE ' .
  1094. 'WHEN (Table.column_a = :c0 AND (Table.column_b) IS NULL) THEN :c1 ' .
  1095. 'WHEN (Table.column_c = :c2 AND (Table.column_d) IS NOT NULL) THEN :c3 ' .
  1096. 'ELSE :c4 ' .
  1097. 'END',
  1098. $sql
  1099. );
  1100. }
  1101. public function testSelfContainedCustomWhenThenExpressions(): void
  1102. {
  1103. $expression = (new CaseStatementExpression())
  1104. ->when(
  1105. (new CustomWhenThenExpression())
  1106. ->when([
  1107. 'Table.column_a' => true,
  1108. 'Table.column_b IS' => null,
  1109. ])
  1110. ->then(1)
  1111. )
  1112. ->when(
  1113. (new CustomWhenThenExpression())
  1114. ->when([
  1115. 'Table.column_c' => true,
  1116. 'Table.column_d IS NOT' => null,
  1117. ])
  1118. ->then(2)
  1119. )
  1120. ->else(3);
  1121. $valueBinder = new ValueBinder();
  1122. $sql = $expression->sql($valueBinder);
  1123. $this->assertSame(
  1124. 'CASE ' .
  1125. 'WHEN (Table.column_a = :c0 AND (Table.column_b) IS NULL) THEN :c1 ' .
  1126. 'WHEN (Table.column_c = :c2 AND (Table.column_d) IS NOT NULL) THEN :c3 ' .
  1127. 'ELSE :c4 ' .
  1128. 'END',
  1129. $sql
  1130. );
  1131. }
  1132. // endregion
  1133. // region Incomplete states
  1134. public function testCompilingEmptyCaseExpressionFails(): void
  1135. {
  1136. $this->expectException(LogicException::class);
  1137. $this->expectExceptionMessage('Case expression must have at least one when statement.');
  1138. $this->deprecated(function (): void {
  1139. (new CaseStatementExpression())->sql(new ValueBinder());
  1140. });
  1141. }
  1142. public function testCompilingNonClosedWhenFails(): void
  1143. {
  1144. $this->expectException(LogicException::class);
  1145. $this->expectExceptionMessage('Case expression has incomplete when clause. Missing `then()` after `when()`.');
  1146. $this->deprecated(function (): void {
  1147. (new CaseStatementExpression())
  1148. ->when(['Table.column' => true])
  1149. ->sql(new ValueBinder());
  1150. });
  1151. }
  1152. public function testCompilingWhenThenExpressionWithMissingWhenFails(): void
  1153. {
  1154. $this->expectException(LogicException::class);
  1155. $this->expectExceptionMessage('Case expression has incomplete when clause. Missing `when()`.');
  1156. $this->deprecated(function (): void {
  1157. (new CaseStatementExpression())
  1158. ->when(function (WhenThenExpression $whenThen) {
  1159. return $whenThen->then(1);
  1160. })
  1161. ->sql(new ValueBinder());
  1162. });
  1163. }
  1164. public function testCompilingWhenThenExpressionWithMissingThenFails(): void
  1165. {
  1166. $this->expectException(LogicException::class);
  1167. $this->expectExceptionMessage('Case expression has incomplete when clause. Missing `then()` after `when()`.');
  1168. $this->deprecated(function (): void {
  1169. (new CaseStatementExpression())
  1170. ->when(function (WhenThenExpression $whenThen) {
  1171. return $whenThen->when(1);
  1172. })
  1173. ->sql(new ValueBinder());
  1174. });
  1175. }
  1176. // endregion
  1177. // region Valid values
  1178. public static function validCaseValuesDataProvider(): array
  1179. {
  1180. return [
  1181. [null, 'NULL', null],
  1182. ['0', null, 'string'],
  1183. [0, null, 'integer'],
  1184. [0.0, null, 'float'],
  1185. ['foo', null, 'string'],
  1186. [true, null, 'boolean'],
  1187. [Date::now(), null, 'date'],
  1188. [ChronosDate::now(), null, 'date'],
  1189. [DateTime::now(), null, 'datetime'],
  1190. [Chronos::now(), null, 'datetime'],
  1191. [new IdentifierExpression('Table.column'), 'Table.column', null],
  1192. [new QueryExpression('Table.column'), 'Table.column', null],
  1193. [ConnectionManager::get('test')->selectQuery('a'), '(SELECT a)', null],
  1194. [new TestObjectWithToString(), null, 'string'],
  1195. [new stdClass(), null, null],
  1196. ];
  1197. }
  1198. /**
  1199. * @param mixed $value The case value.
  1200. * @param string|null $sqlValue The expected SQL string value.
  1201. * @param string|null $type The expected bound type.
  1202. */
  1203. #[DataProvider('validCaseValuesDataProvider')]
  1204. public function testValidCaseValue($value, ?string $sqlValue, ?string $type): void
  1205. {
  1206. $expression = (new CaseStatementExpression($value))
  1207. ->when(1)
  1208. ->then(2);
  1209. $valueBinder = new ValueBinder();
  1210. $sql = $expression->sql($valueBinder);
  1211. if ($sqlValue) {
  1212. $this->assertEqualsSql(
  1213. "CASE {$sqlValue} WHEN :c0 THEN :c1 ELSE NULL END",
  1214. $sql
  1215. );
  1216. $this->assertSame(
  1217. [
  1218. ':c0' => [
  1219. 'value' => 1,
  1220. 'type' => 'integer',
  1221. 'placeholder' => 'c0',
  1222. ],
  1223. ':c1' => [
  1224. 'value' => 2,
  1225. 'type' => 'integer',
  1226. 'placeholder' => 'c1',
  1227. ],
  1228. ],
  1229. $valueBinder->bindings()
  1230. );
  1231. } else {
  1232. $this->assertEqualsSql(
  1233. 'CASE :c0 WHEN :c1 THEN :c2 ELSE NULL END',
  1234. $sql
  1235. );
  1236. $this->assertSame(
  1237. [
  1238. ':c0' => [
  1239. 'value' => $value,
  1240. 'type' => $type,
  1241. 'placeholder' => 'c0',
  1242. ],
  1243. ':c1' => [
  1244. 'value' => 1,
  1245. 'type' => 'integer',
  1246. 'placeholder' => 'c1',
  1247. ],
  1248. ':c2' => [
  1249. 'value' => 2,
  1250. 'type' => 'integer',
  1251. 'placeholder' => 'c2',
  1252. ],
  1253. ],
  1254. $valueBinder->bindings()
  1255. );
  1256. }
  1257. }
  1258. public static function validWhenValuesSimpleCaseDataProvider(): array
  1259. {
  1260. return [
  1261. ['0', null, 'string'],
  1262. [0, null, 'integer'],
  1263. [0.0, null, 'float'],
  1264. ['foo', null, 'string'],
  1265. [true, null, 'boolean'],
  1266. [new stdClass(), null, null],
  1267. [new TestObjectWithToString(), null, 'string'],
  1268. [Date::now(), null, 'date'],
  1269. [ChronosDate::now(), null, 'date'],
  1270. [DateTime::now(), null, 'datetime'],
  1271. [Chronos::now(), null, 'datetime'],
  1272. [
  1273. new IdentifierExpression('Table.column'),
  1274. 'CASE :c0 WHEN Table.column THEN :c1 ELSE NULL END',
  1275. [
  1276. ':c0' => [
  1277. 'value' => true,
  1278. 'type' => 'boolean',
  1279. 'placeholder' => 'c0',
  1280. ],
  1281. ':c1' => [
  1282. 'value' => 2,
  1283. 'type' => 'integer',
  1284. 'placeholder' => 'c1',
  1285. ],
  1286. ],
  1287. ],
  1288. [
  1289. new QueryExpression('Table.column'),
  1290. 'CASE :c0 WHEN Table.column THEN :c1 ELSE NULL END',
  1291. [
  1292. ':c0' => [
  1293. 'value' => true,
  1294. 'type' => 'boolean',
  1295. 'placeholder' => 'c0',
  1296. ],
  1297. ':c1' => [
  1298. 'value' => 2,
  1299. 'type' => 'integer',
  1300. 'placeholder' => 'c1',
  1301. ],
  1302. ],
  1303. ],
  1304. [
  1305. ConnectionManager::get('test')->selectQuery('a'),
  1306. 'CASE :c0 WHEN (SELECT a) THEN :c1 ELSE NULL END',
  1307. [
  1308. ':c0' => [
  1309. 'value' => true,
  1310. 'type' => 'boolean',
  1311. 'placeholder' => 'c0',
  1312. ],
  1313. ':c1' => [
  1314. 'value' => 2,
  1315. 'type' => 'integer',
  1316. 'placeholder' => 'c1',
  1317. ],
  1318. ],
  1319. ],
  1320. [
  1321. [
  1322. 'Table.column_a' => 1,
  1323. 'Table.column_b' => 'foo',
  1324. ],
  1325. 'CASE :c0 WHEN (Table.column_a = :c1 AND Table.column_b = :c2) THEN :c3 ELSE NULL END',
  1326. [
  1327. ':c0' => [
  1328. 'value' => true,
  1329. 'type' => 'boolean',
  1330. 'placeholder' => 'c0',
  1331. ],
  1332. ':c1' => [
  1333. 'value' => 1,
  1334. 'type' => 'integer',
  1335. 'placeholder' => 'c1',
  1336. ],
  1337. ':c2' => [
  1338. 'value' => 'foo',
  1339. 'type' => 'string',
  1340. 'placeholder' => 'c2',
  1341. ],
  1342. ':c3' => [
  1343. 'value' => 2,
  1344. 'type' => 'integer',
  1345. 'placeholder' => 'c3',
  1346. ],
  1347. ],
  1348. ],
  1349. ];
  1350. }
  1351. /**
  1352. * @param mixed $value The when value.
  1353. * @param string|null $expectedSql The expected SQL string.
  1354. * @param array|string|null $typeOrBindings The expected bound type(s).
  1355. */
  1356. #[DataProvider('validWhenValuesSimpleCaseDataProvider')]
  1357. public function testValidWhenValueSimpleCase($value, ?string $expectedSql, $typeOrBindings = null): void
  1358. {
  1359. $typeMap = new TypeMap([
  1360. 'Table.column_a' => 'integer',
  1361. 'Table.column_b' => 'string',
  1362. ]);
  1363. $expression = (new CaseStatementExpression(true))
  1364. ->setTypeMap($typeMap)
  1365. ->when($value)
  1366. ->then(2);
  1367. $valueBinder = new ValueBinder();
  1368. $sql = $expression->sql($valueBinder);
  1369. if ($expectedSql) {
  1370. $this->assertEqualsSql($expectedSql, $sql);
  1371. $this->assertSame($typeOrBindings, $valueBinder->bindings());
  1372. } else {
  1373. $this->assertEqualsSql('CASE :c0 WHEN :c1 THEN :c2 ELSE NULL END', $sql);
  1374. $this->assertSame(
  1375. [
  1376. ':c0' => [
  1377. 'value' => true,
  1378. 'type' => 'boolean',
  1379. 'placeholder' => 'c0',
  1380. ],
  1381. ':c1' => [
  1382. 'value' => $value,
  1383. 'type' => $typeOrBindings,
  1384. 'placeholder' => 'c1',
  1385. ],
  1386. ':c2' => [
  1387. 'value' => 2,
  1388. 'type' => 'integer',
  1389. 'placeholder' => 'c2',
  1390. ],
  1391. ],
  1392. $valueBinder->bindings()
  1393. );
  1394. }
  1395. }
  1396. public static function validWhenValuesSearchedCaseDataProvider(): array
  1397. {
  1398. return [
  1399. ['0', null, 'string'],
  1400. [0, null, 'integer'],
  1401. [0.0, null, 'float'],
  1402. ['foo', null, 'string'],
  1403. [true, null, 'boolean'],
  1404. [new stdClass(), null, null],
  1405. [new TestObjectWithToString(), null, 'string'],
  1406. [Date::now(), null, 'date'],
  1407. [ChronosDate::now(), null, 'date'],
  1408. [DateTime::now(), null, 'datetime'],
  1409. [Chronos::now(), null, 'datetime'],
  1410. [
  1411. new IdentifierExpression('Table.column'),
  1412. 'CASE WHEN Table.column THEN :c0 ELSE NULL END',
  1413. [
  1414. ':c0' => [
  1415. 'value' => 2,
  1416. 'type' => 'integer',
  1417. 'placeholder' => 'c0',
  1418. ],
  1419. ],
  1420. ],
  1421. [
  1422. new QueryExpression('Table.column'),
  1423. 'CASE WHEN Table.column THEN :c0 ELSE NULL END',
  1424. [
  1425. ':c0' => [
  1426. 'value' => 2,
  1427. 'type' => 'integer',
  1428. 'placeholder' => 'c0',
  1429. ],
  1430. ],
  1431. ],
  1432. [
  1433. ConnectionManager::get('test')->selectQuery('a'),
  1434. 'CASE WHEN (SELECT a) THEN :c0 ELSE NULL END',
  1435. [
  1436. ':c0' => [
  1437. 'value' => 2,
  1438. 'type' => 'integer',
  1439. 'placeholder' => 'c0',
  1440. ],
  1441. ],
  1442. ],
  1443. [
  1444. [
  1445. 'Table.column_a' => 1,
  1446. 'Table.column_b' => 'foo',
  1447. ],
  1448. 'CASE WHEN (Table.column_a = :c0 AND Table.column_b = :c1) THEN :c2 ELSE NULL END',
  1449. [
  1450. ':c0' => [
  1451. 'value' => 1,
  1452. 'type' => 'integer',
  1453. 'placeholder' => 'c0',
  1454. ],
  1455. ':c1' => [
  1456. 'value' => 'foo',
  1457. 'type' => 'string',
  1458. 'placeholder' => 'c1',
  1459. ],
  1460. ':c2' => [
  1461. 'value' => 2,
  1462. 'type' => 'integer',
  1463. 'placeholder' => 'c2',
  1464. ],
  1465. ],
  1466. ],
  1467. ];
  1468. }
  1469. /**
  1470. * @param mixed $value The when value.
  1471. * @param string|null $expectedSql The expected SQL string.
  1472. * @param array|string|null $typeOrBindings The expected bound type(s).
  1473. */
  1474. #[DataProvider('validWhenValuesSearchedCaseDataProvider')]
  1475. public function testValidWhenValueSearchedCase($value, ?string $expectedSql, $typeOrBindings = null): void
  1476. {
  1477. $typeMap = new TypeMap([
  1478. 'Table.column_a' => 'integer',
  1479. 'Table.column_b' => 'string',
  1480. ]);
  1481. $expression = (new CaseStatementExpression())
  1482. ->setTypeMap($typeMap)
  1483. ->when($value)
  1484. ->then(2);
  1485. $valueBinder = new ValueBinder();
  1486. $sql = $expression->sql($valueBinder);
  1487. if ($expectedSql) {
  1488. $this->assertEqualsSql($expectedSql, $sql);
  1489. $this->assertSame($typeOrBindings, $valueBinder->bindings());
  1490. } else {
  1491. $this->assertEqualsSql('CASE WHEN :c0 THEN :c1 ELSE NULL END', $sql);
  1492. $this->assertSame(
  1493. [
  1494. ':c0' => [
  1495. 'value' => $value,
  1496. 'type' => $typeOrBindings,
  1497. 'placeholder' => 'c0',
  1498. ],
  1499. ':c1' => [
  1500. 'value' => 2,
  1501. 'type' => 'integer',
  1502. 'placeholder' => 'c1',
  1503. ],
  1504. ],
  1505. $valueBinder->bindings()
  1506. );
  1507. }
  1508. }
  1509. public static function validThenValuesDataProvider(): array
  1510. {
  1511. return [
  1512. [null, 'NULL', null],
  1513. ['0', null, 'string'],
  1514. [0, null, 'integer'],
  1515. [0.0, null, 'float'],
  1516. ['foo', null, 'string'],
  1517. [true, null, 'boolean'],
  1518. [Date::now(), null, 'date'],
  1519. [ChronosDate::now(), null, 'date'],
  1520. [DateTime::now(), null, 'datetime'],
  1521. [Chronos::now(), null, 'datetime'],
  1522. [new IdentifierExpression('Table.column'), 'Table.column', null],
  1523. [new QueryExpression('Table.column'), 'Table.column', null],
  1524. [ConnectionManager::get('test')->selectQuery('a'), '(SELECT a)', null],
  1525. [new TestObjectWithToString(), null, 'string'],
  1526. [new stdClass(), null, null],
  1527. ];
  1528. }
  1529. /**
  1530. * @param mixed $value The then value.
  1531. * @param string|null $sqlValue The expected SQL string value.
  1532. * @param string|null $type The expected bound type.
  1533. */
  1534. #[DataProvider('validThenValuesDataProvider')]
  1535. public function testValidThenValue($value, ?string $sqlValue, ?string $type): void
  1536. {
  1537. $expression = (new CaseStatementExpression())
  1538. ->when(1)
  1539. ->then($value);
  1540. $valueBinder = new ValueBinder();
  1541. $sql = $expression->sql($valueBinder);
  1542. if ($sqlValue) {
  1543. $this->assertEqualsSql(
  1544. "CASE WHEN :c0 THEN {$sqlValue} ELSE NULL END",
  1545. $sql
  1546. );
  1547. $this->assertSame(
  1548. [
  1549. ':c0' => [
  1550. 'value' => 1,
  1551. 'type' => 'integer',
  1552. 'placeholder' => 'c0',
  1553. ],
  1554. ],
  1555. $valueBinder->bindings()
  1556. );
  1557. } else {
  1558. $this->assertEqualsSql(
  1559. 'CASE WHEN :c0 THEN :c1 ELSE NULL END',
  1560. $sql
  1561. );
  1562. $this->assertSame(
  1563. [
  1564. ':c0' => [
  1565. 'value' => 1,
  1566. 'type' => 'integer',
  1567. 'placeholder' => 'c0',
  1568. ],
  1569. ':c1' => [
  1570. 'value' => $value,
  1571. 'type' => $type,
  1572. 'placeholder' => 'c1',
  1573. ],
  1574. ],
  1575. $valueBinder->bindings()
  1576. );
  1577. }
  1578. }
  1579. public static function validElseValuesDataProvider(): array
  1580. {
  1581. return [
  1582. [null, 'NULL', null],
  1583. ['0', null, 'string'],
  1584. [0, null, 'integer'],
  1585. [0.0, null, 'float'],
  1586. ['foo', null, 'string'],
  1587. [true, null, 'boolean'],
  1588. [Date::now(), null, 'date'],
  1589. [ChronosDate::now(), null, 'date'],
  1590. [DateTime::now(), null, 'datetime'],
  1591. [Chronos::now(), null, 'datetime'],
  1592. [new IdentifierExpression('Table.column'), 'Table.column', null],
  1593. [new QueryExpression('Table.column'), 'Table.column', null],
  1594. [ConnectionManager::get('test')->selectQuery('a'), '(SELECT a)', null],
  1595. [new TestObjectWithToString(), null, 'string'],
  1596. [new stdClass(), null, null],
  1597. ];
  1598. }
  1599. /**
  1600. * @param mixed $value The else value.
  1601. * @param string|null $sqlValue The expected SQL string value.
  1602. * @param string|null $type The expected bound type.
  1603. */
  1604. #[DataProvider('validElseValuesDataProvider')]
  1605. public function testValidElseValue($value, ?string $sqlValue, ?string $type): void
  1606. {
  1607. $expression = (new CaseStatementExpression())
  1608. ->when(1)
  1609. ->then(2)
  1610. ->else($value);
  1611. $valueBinder = new ValueBinder();
  1612. $sql = $expression->sql($valueBinder);
  1613. if ($sqlValue) {
  1614. $this->assertEqualsSql(
  1615. "CASE WHEN :c0 THEN :c1 ELSE {$sqlValue} END",
  1616. $sql
  1617. );
  1618. $this->assertSame(
  1619. [
  1620. ':c0' => [
  1621. 'value' => 1,
  1622. 'type' => 'integer',
  1623. 'placeholder' => 'c0',
  1624. ],
  1625. ':c1' => [
  1626. 'value' => 2,
  1627. 'type' => 'integer',
  1628. 'placeholder' => 'c1',
  1629. ],
  1630. ],
  1631. $valueBinder->bindings()
  1632. );
  1633. } else {
  1634. $this->assertEqualsSql(
  1635. 'CASE WHEN :c0 THEN :c1 ELSE :c2 END',
  1636. $sql
  1637. );
  1638. $this->assertSame(
  1639. [
  1640. ':c0' => [
  1641. 'value' => 1,
  1642. 'type' => 'integer',
  1643. 'placeholder' => 'c0',
  1644. ],
  1645. ':c1' => [
  1646. 'value' => 2,
  1647. 'type' => 'integer',
  1648. 'placeholder' => 'c1',
  1649. ],
  1650. ':c2' => [
  1651. 'value' => $value,
  1652. 'type' => $type,
  1653. 'placeholder' => 'c2',
  1654. ],
  1655. ],
  1656. $valueBinder->bindings()
  1657. );
  1658. }
  1659. }
  1660. // endregion
  1661. // region Invalid values
  1662. public static function invalidCaseValuesDataProvider(): array
  1663. {
  1664. $res = fopen('data:text/plain,123', 'rb');
  1665. fclose($res);
  1666. return [
  1667. [[], 'array'],
  1668. [
  1669. function (): void {
  1670. },
  1671. 'Closure',
  1672. ],
  1673. [$res, 'resource (closed)'],
  1674. ];
  1675. }
  1676. /**
  1677. * @param mixed $value The case value.
  1678. * @param string $typeName The expected error type name.
  1679. */
  1680. #[DataProvider('invalidCaseValuesDataProvider')]
  1681. public function testInvalidCaseValue($value, string $typeName): void
  1682. {
  1683. $this->expectException(InvalidArgumentException::class);
  1684. $this->expectExceptionMessage(
  1685. 'The `$value` argument must be either `null`, a scalar value, an object, ' .
  1686. "or an instance of `\\Cake\\Database\\ExpressionInterface`, `{$typeName}` given."
  1687. );
  1688. new CaseStatementExpression($value);
  1689. }
  1690. public function testInvalidWhenValue(): void
  1691. {
  1692. $this->expectException(InvalidArgumentException::class);
  1693. $this->expectExceptionMessage(
  1694. 'The `$when` argument must be a non-empty array'
  1695. );
  1696. (new CaseStatementExpression())
  1697. ->when([])
  1698. ->then(1);
  1699. }
  1700. public static function invalidThenValueDataProvider(): array
  1701. {
  1702. $res = fopen('data:text/plain,123', 'rb');
  1703. fclose($res);
  1704. return [
  1705. [[], 'array'],
  1706. [
  1707. function (): void {
  1708. },
  1709. 'Closure',
  1710. ],
  1711. [$res, 'resource (closed)'],
  1712. ];
  1713. }
  1714. /**
  1715. * @param mixed $value The then value.
  1716. * @param string $typeName The expected error type name.
  1717. */
  1718. #[DataProvider('invalidThenValueDataProvider')]
  1719. public function testInvalidThenValue($value, string $typeName): void
  1720. {
  1721. $this->expectException(InvalidArgumentException::class);
  1722. $this->expectExceptionMessage(
  1723. 'The `$result` argument must be either `null`, a scalar value, an object, ' .
  1724. "or an instance of `\\Cake\\Database\\ExpressionInterface`, `{$typeName}` given."
  1725. );
  1726. (new CaseStatementExpression())
  1727. ->when(1)
  1728. ->then($value);
  1729. }
  1730. public static function invalidThenTypeDataProvider(): array
  1731. {
  1732. $res = fopen('data:text/plain,123', 'rb');
  1733. fclose($res);
  1734. return [
  1735. [1],
  1736. [1.0],
  1737. [new stdClass()],
  1738. [
  1739. function (): void {
  1740. },
  1741. ],
  1742. [$res, 'resource (closed)'],
  1743. ];
  1744. }
  1745. /**
  1746. * @param mixed $type The then type.
  1747. */
  1748. #[DataProvider('invalidThenTypeDataProvider')]
  1749. public function testInvalidThenType($type): void
  1750. {
  1751. $this->expectException(TypeError::class);
  1752. (new CaseStatementExpression())
  1753. ->when(1)
  1754. ->then(1, $type);
  1755. }
  1756. public static function invalidElseValueDataProvider(): array
  1757. {
  1758. $res = fopen('data:text/plain,123', 'rb');
  1759. fclose($res);
  1760. return [
  1761. [[], 'array'],
  1762. [
  1763. function (): void {
  1764. },
  1765. 'Closure',
  1766. ],
  1767. [$res, 'resource (closed)'],
  1768. ];
  1769. }
  1770. /**
  1771. * @param mixed $value The else value.
  1772. * @param string $typeName The expected error type name.
  1773. */
  1774. #[DataProvider('invalidElseValueDataProvider')]
  1775. public function testInvalidElseValue($value, string $typeName): void
  1776. {
  1777. $this->expectException(InvalidArgumentException::class);
  1778. $this->expectExceptionMessage(
  1779. 'The `$result` argument must be either `null`, a scalar value, an object, ' .
  1780. "or an instance of `\\Cake\\Database\\ExpressionInterface`, `{$typeName}` given."
  1781. );
  1782. (new CaseStatementExpression())
  1783. ->when(1)
  1784. ->then(1)
  1785. ->else($value);
  1786. }
  1787. public static function invalidElseTypeDataProvider(): array
  1788. {
  1789. $res = fopen('data:text/plain,123', 'rb');
  1790. fclose($res);
  1791. return [
  1792. [1],
  1793. [1.0],
  1794. [new stdClass()],
  1795. [
  1796. function (): void {
  1797. },
  1798. 'Closure',
  1799. ],
  1800. [$res, 'resource (closed)'],
  1801. ];
  1802. }
  1803. /**
  1804. * @param mixed $type The else type.
  1805. */
  1806. #[DataProvider('invalidElseTypeDataProvider')]
  1807. public function testInvalidElseType($type): void
  1808. {
  1809. $this->expectException(TypeError::class);
  1810. (new CaseStatementExpression())
  1811. ->when(1)
  1812. ->then(1)
  1813. ->else(1, $type);
  1814. }
  1815. // endregion
  1816. // region Traversal
  1817. public function testTraverse(): void
  1818. {
  1819. $value = new IdentifierExpression('Table.column');
  1820. $conditionsA = ['Table.column_a' => true, 'Table.column_b IS' => null];
  1821. $resultA = new QueryExpression('1');
  1822. $conditionsB = ['Table.column_c' => true, 'Table.column_d IS NOT' => null];
  1823. $resultB = new QueryExpression('2');
  1824. $else = new QueryExpression('3');
  1825. $expression = (new CaseStatementExpression($value))
  1826. ->when($conditionsA)
  1827. ->then($resultA)
  1828. ->when($conditionsB)
  1829. ->then($resultB)
  1830. ->else($else);
  1831. $expressions = [];
  1832. $expression->traverse(function ($expression) use (&$expressions): void {
  1833. $expressions[] = $expression;
  1834. });
  1835. $this->assertCount(14, $expressions);
  1836. $this->assertInstanceOf(IdentifierExpression::class, $expressions[0]);
  1837. $this->assertSame($value, $expressions[0]);
  1838. $this->assertInstanceOf(WhenThenExpression::class, $expressions[1]);
  1839. $this->assertEquals(new QueryExpression($conditionsA), $expressions[2]);
  1840. $this->assertEquals(new ComparisonExpression('Table.column_a', true), $expressions[3]);
  1841. $this->assertSame($resultA, $expressions[6]);
  1842. $this->assertInstanceOf(WhenThenExpression::class, $expressions[7]);
  1843. $this->assertEquals(new QueryExpression($conditionsB), $expressions[8]);
  1844. $this->assertEquals(new ComparisonExpression('Table.column_c', true), $expressions[9]);
  1845. $this->assertSame($resultB, $expressions[12]);
  1846. $this->assertSame($else, $expressions[13]);
  1847. }
  1848. public function testTraverseBeforeClosingThenFails(): void
  1849. {
  1850. $this->expectException(LogicException::class);
  1851. $this->expectExceptionMessage('Case expression has incomplete when clause. Missing `then()` after `when()`.');
  1852. $this->deprecated(function (): void {
  1853. $expression = (new CaseStatementExpression())
  1854. ->when(['Table.column' => true]);
  1855. $expression->traverse(
  1856. function (): void {
  1857. }
  1858. );
  1859. });
  1860. }
  1861. // endregion
  1862. // region Cloning
  1863. public function testClone(): void
  1864. {
  1865. $value = new IdentifierExpression('Table.column');
  1866. $conditionsA = ['Table.column_a' => true, 'Table.column_b IS' => null];
  1867. $resultA = new QueryExpression('1');
  1868. $conditionsB = ['Table.column_c' => true, 'Table.column_d IS NOT' => null];
  1869. $resultB = new QueryExpression('2');
  1870. $else = new QueryExpression('3');
  1871. $expression = (new CaseStatementExpression($value))
  1872. ->when($conditionsA)
  1873. ->then($resultA)
  1874. ->when($conditionsB)
  1875. ->then($resultB)
  1876. ->else($else);
  1877. $clone = clone $expression;
  1878. $this->assertEquals($clone, $expression);
  1879. $this->assertNotSame($clone, $expression);
  1880. }
  1881. public function testCloneBeforeClosingThenFails(): void
  1882. {
  1883. $this->expectException(LogicException::class);
  1884. $this->expectExceptionMessage('Case expression has incomplete when clause. Missing `then()` after `when()`.');
  1885. $this->deprecated(function (): void {
  1886. $expression = (new CaseStatementExpression())
  1887. ->when(['Table.column' => true]);
  1888. clone $expression;
  1889. });
  1890. }
  1891. // endregion
  1892. }