ConditionalTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PhpOffice\PhpSpreadsheet\Style\Conditional;
  5. use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
  6. use PhpOffice\PhpSpreadsheet\Style\Fill;
  7. use PhpOffice\PhpSpreadsheet\Style\Style;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  9. use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
  10. class ConditionalTest extends AbstractFunctional
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected $cellRange;
  16. /**
  17. * @var Style
  18. */
  19. protected $style;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. $this->cellRange = 'C3:E5';
  24. $this->style = new Style();
  25. $this->style->applyFromArray([
  26. 'fill' => [
  27. 'color' => ['argb' => 'FFFFC000'],
  28. 'fillType' => Fill::FILL_SOLID,
  29. ],
  30. ]);
  31. }
  32. public function testWriteSimpleCellConditionalFromWizard(): void
  33. {
  34. $spreadsheet = new Spreadsheet();
  35. $worksheet = $spreadsheet->getActiveSheet();
  36. $wizard = new Wizard\CellValue($this->cellRange);
  37. $wizard->greaterThan(5);
  38. $condition = $wizard->getConditional();
  39. $condition->setStyle($this->style);
  40. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  41. $writer = new Xlsx($spreadsheet);
  42. $writerWorksheet = new Xlsx\Worksheet($writer);
  43. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  44. $expected = <<<XML
  45. <conditionalFormatting sqref="C3:E5"><cfRule type="cellIs" dxfId="" priority="1" operator="greaterThan"><formula>5</formula></cfRule></conditionalFormatting>
  46. XML;
  47. self::assertStringContainsString($expected, $data);
  48. }
  49. public function testWriteBetweenCellConditionalFromWizard(): void
  50. {
  51. $spreadsheet = new Spreadsheet();
  52. $worksheet = $spreadsheet->getActiveSheet();
  53. $wizard = new Wizard\CellValue($this->cellRange);
  54. $wizard->between(-5)->and(5);
  55. $condition = $wizard->getConditional();
  56. $condition->setStyle($this->style);
  57. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  58. $writer = new Xlsx($spreadsheet);
  59. $writerWorksheet = new Xlsx\Worksheet($writer);
  60. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  61. $expected = <<<XML
  62. <conditionalFormatting sqref="C3:E5"><cfRule type="cellIs" dxfId="" priority="1" operator="between"><formula>-5</formula><formula>5</formula></cfRule></conditionalFormatting>
  63. XML;
  64. self::assertStringContainsString($expected, $data);
  65. }
  66. public function testWriteTextConditionalFromWizard(): void
  67. {
  68. $spreadsheet = new Spreadsheet();
  69. $worksheet = $spreadsheet->getActiveSheet();
  70. $wizard = new Wizard\TextValue($this->cellRange);
  71. $wizard->contains('PHP');
  72. $condition = $wizard->getConditional();
  73. $condition->setStyle($this->style);
  74. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  75. $writer = new Xlsx($spreadsheet);
  76. $writerWorksheet = new Xlsx\Worksheet($writer);
  77. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  78. $expected = <<<XML
  79. <conditionalFormatting sqref="C3:E5"><cfRule type="containsText" dxfId="" priority="1" operator="containsText" text="PHP"><formula>NOT(ISERROR(SEARCH(&quot;PHP&quot;,C3)))</formula></cfRule></conditionalFormatting>
  80. XML;
  81. self::assertStringContainsString($expected, $data);
  82. }
  83. /**
  84. * @dataProvider textConditionalsProvider
  85. */
  86. public function testWriteTextConditionals(string $conditionType, string $operatorType, string $expected): void
  87. {
  88. $spreadsheet = new Spreadsheet();
  89. $worksheet = $spreadsheet->getActiveSheet();
  90. $condition = new Conditional();
  91. $condition->setConditionType($conditionType);
  92. $condition->setOperatorType($operatorType);
  93. $condition->setText('PHP');
  94. $condition->setStyle($this->style);
  95. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  96. $writer = new Xlsx($spreadsheet);
  97. $writerWorksheet = new Xlsx\Worksheet($writer);
  98. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  99. self::assertStringContainsString($expected, $data);
  100. }
  101. public function textConditionalsProvider(): array
  102. {
  103. return [
  104. 'Contains' => [
  105. Conditional::CONDITION_CONTAINSTEXT,
  106. Conditional::OPERATOR_CONTAINSTEXT,
  107. <<<XML
  108. <conditionalFormatting sqref="C3:E5"><cfRule type="containsText" dxfId="" priority="1" operator="containsText" text="PHP"><formula>NOT(ISERROR(SEARCH(&quot;PHP&quot;,C3)))</formula></cfRule></conditionalFormatting>
  109. XML
  110. ],
  111. 'Not Contains' => [
  112. Conditional::CONDITION_NOTCONTAINSTEXT,
  113. Conditional::OPERATOR_NOTCONTAINS,
  114. <<<XML
  115. <conditionalFormatting sqref="C3:E5"><cfRule type="notContainsText" dxfId="" priority="1" operator="notContains" text="PHP"><formula>ISERROR(SEARCH(&quot;PHP&quot;,C3))</formula></cfRule></conditionalFormatting>
  116. XML
  117. ],
  118. 'Begins With' => [
  119. Conditional::CONDITION_BEGINSWITH,
  120. Conditional::OPERATOR_BEGINSWITH,
  121. <<<XML
  122. <conditionalFormatting sqref="C3:E5"><cfRule type="beginsWith" dxfId="" priority="1" operator="beginsWith" text="PHP"><formula>LEFT(C3,LEN(&quot;PHP&quot;))=&quot;PHP&quot;</formula></cfRule></conditionalFormatting>
  123. XML
  124. ],
  125. 'Ends With' => [
  126. Conditional::CONDITION_ENDSWITH,
  127. Conditional::OPERATOR_ENDSWITH,
  128. <<<XML
  129. <conditionalFormatting sqref="C3:E5"><cfRule type="endsWith" dxfId="" priority="1" operator="endsWith" text="PHP"><formula>RIGHT(C3,LEN(&quot;PHP&quot;))=&quot;PHP&quot;</formula></cfRule></conditionalFormatting>
  130. XML
  131. ],
  132. ];
  133. }
  134. public function testWriteDateConditionalFromWizard(): void
  135. {
  136. $spreadsheet = new Spreadsheet();
  137. $worksheet = $spreadsheet->getActiveSheet();
  138. $wizard = new Wizard\DateValue($this->cellRange);
  139. $wizard->today();
  140. $condition = $wizard->getConditional();
  141. $condition->setStyle($this->style);
  142. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  143. $writer = new Xlsx($spreadsheet);
  144. $writerWorksheet = new Xlsx\Worksheet($writer);
  145. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  146. $expected = <<<XML
  147. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="today"><formula>FLOOR(C3,1)=TODAY()</formula></cfRule></conditionalFormatting>
  148. XML;
  149. self::assertStringContainsString($expected, $data);
  150. }
  151. /**
  152. * @dataProvider dateConditionalsProvider
  153. */
  154. public function testWriteDateConditionals(string $timePeriod, string $expected): void
  155. {
  156. $spreadsheet = new Spreadsheet();
  157. $worksheet = $spreadsheet->getActiveSheet();
  158. $condition = new Conditional();
  159. $condition->setConditionType(Conditional::CONDITION_TIMEPERIOD);
  160. $condition->setOperatorType($timePeriod);
  161. $condition->setText($timePeriod);
  162. $condition->setStyle($this->style);
  163. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  164. $writer = new Xlsx($spreadsheet);
  165. $writerWorksheet = new Xlsx\Worksheet($writer);
  166. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  167. self::assertStringContainsString($expected, $data);
  168. }
  169. public function dateConditionalsProvider(): array
  170. {
  171. return [
  172. 'Yesterday' => [
  173. Conditional::TIMEPERIOD_YESTERDAY,
  174. <<<XML
  175. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="yesterday"><formula>FLOOR(C3)=TODAY()-1</formula></cfRule></conditionalFormatting>
  176. XML
  177. ],
  178. 'Today' => [
  179. Conditional::TIMEPERIOD_TODAY,
  180. <<<XML
  181. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="today"><formula>FLOOR(C3)=TODAY()</formula></cfRule></conditionalFormatting>
  182. XML
  183. ],
  184. 'Tomorrow' => [
  185. Conditional::TIMEPERIOD_TOMORROW,
  186. <<<XML
  187. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="tomorrow"><formula>FLOOR(C3)=TODAY()+1</formula></cfRule></conditionalFormatting>
  188. XML
  189. ],
  190. 'Last 7 Days' => [
  191. Conditional::TIMEPERIOD_LAST_7_DAYS,
  192. <<<XML
  193. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="last7Days"><formula>AND(TODAY()-FLOOR(C3,1)&lt;=6,FLOOR(C3,1)&lt;=TODAY())</formula></cfRule></conditionalFormatting>
  194. XML
  195. ],
  196. 'Last Week' => [
  197. Conditional::TIMEPERIOD_LAST_WEEK,
  198. <<<XML
  199. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="lastWeek"><formula>AND(TODAY()-ROUNDDOWN(C3,0)&gt;=(WEEKDAY(TODAY())),TODAY()-ROUNDDOWN(C3,0)&lt;(WEEKDAY(TODAY())+7))</formula></cfRule></conditionalFormatting>
  200. XML
  201. ],
  202. 'This Week' => [
  203. Conditional::TIMEPERIOD_THIS_WEEK,
  204. <<<XML
  205. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="thisWeek"><formula>AND(TODAY()-ROUNDDOWN(C3,0)&lt;=WEEKDAY(TODAY())-1,ROUNDDOWN(C3,0)-TODAY()&lt;=7-WEEKDAY(TODAY()))</formula></cfRule></conditionalFormatting>
  206. XML
  207. ],
  208. 'Next Week' => [
  209. Conditional::TIMEPERIOD_NEXT_WEEK,
  210. <<<XML
  211. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="nextWeek"><formula>AND(ROUNDDOWN(C3,0)-TODAY()&gt;(7-WEEKDAY(TODAY())),ROUNDDOWN(C3,0)-TODAY()&lt;(15-WEEKDAY(TODAY())))</formula></cfRule></conditionalFormatting>
  212. XML
  213. ],
  214. 'Last Month' => [
  215. Conditional::TIMEPERIOD_LAST_MONTH,
  216. <<<XML
  217. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="lastMonth"><formula>AND(MONTH(C3)=MONTH(EDATE(TODAY(),0-1)),YEAR(C3)=YEAR(EDATE(TODAY(),0-1)))</formula></cfRule></conditionalFormatting>
  218. XML
  219. ],
  220. 'This Month' => [
  221. Conditional::TIMEPERIOD_THIS_MONTH,
  222. <<<XML
  223. <conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="thisMonth"><formula>AND(MONTH(C3)=MONTH(TODAY()),YEAR(C3)=YEAR(TODAY()))</formula></cfRule></conditionalFormatting>
  224. XML
  225. ],
  226. 'Next Month' => [
  227. Conditional::TIMEPERIOD_NEXT_MONTH,
  228. <<<XML
  229. ><conditionalFormatting sqref="C3:E5"><cfRule type="timePeriod" dxfId="" priority="1" timePeriod="nextMonth"><formula>AND(MONTH(C3)=MONTH(EDATE(TODAY(),0+1)),YEAR(C3)=YEAR(EDATE(TODAY(),0+1)))</formula></cfRule></conditionalFormatting>
  230. XML
  231. ],
  232. ];
  233. }
  234. public function testWriteBlankConditionalFromWizard(): void
  235. {
  236. $spreadsheet = new Spreadsheet();
  237. $worksheet = $spreadsheet->getActiveSheet();
  238. $wizard = new Wizard\Blanks($this->cellRange);
  239. $wizard->isBlank();
  240. $condition = $wizard->getConditional();
  241. $condition->setStyle($this->style);
  242. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  243. $writer = new Xlsx($spreadsheet);
  244. $writerWorksheet = new Xlsx\Worksheet($writer);
  245. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  246. $expected = <<<XML
  247. <conditionalFormatting sqref="C3:E5"><cfRule type="containsBlanks" dxfId="" priority="1"><formula>LEN(TRIM(C3))=0</formula></cfRule></conditionalFormatting>
  248. XML;
  249. self::assertStringContainsString($expected, $data);
  250. }
  251. public function testWriteNonBlankConditionalFromWizard(): void
  252. {
  253. $spreadsheet = new Spreadsheet();
  254. $worksheet = $spreadsheet->getActiveSheet();
  255. $wizard = new Wizard\Blanks($this->cellRange);
  256. $wizard->notBlank();
  257. $condition = $wizard->getConditional();
  258. $condition->setStyle($this->style);
  259. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  260. $writer = new Xlsx($spreadsheet);
  261. $writerWorksheet = new Xlsx\Worksheet($writer);
  262. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  263. $expected = <<<XML
  264. <conditionalFormatting sqref="C3:E5"><cfRule type="notContainsBlanks" dxfId="" priority="1"><formula>LEN(TRIM(C3))&gt;0</formula></cfRule></conditionalFormatting>
  265. XML;
  266. self::assertStringContainsString($expected, $data);
  267. }
  268. /**
  269. * @dataProvider blanksConditionalsProvider
  270. */
  271. public function testWriteBlanksConditionals(string $conditionalType, string $expected): void
  272. {
  273. $spreadsheet = new Spreadsheet();
  274. $worksheet = $spreadsheet->getActiveSheet();
  275. $condition = new Conditional();
  276. $condition->setConditionType($conditionalType);
  277. $condition->setStyle($this->style);
  278. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  279. $writer = new Xlsx($spreadsheet);
  280. $writerWorksheet = new Xlsx\Worksheet($writer);
  281. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  282. self::assertStringContainsString($expected, $data);
  283. }
  284. public function blanksConditionalsProvider(): array
  285. {
  286. return [
  287. 'Blanks' => [
  288. Conditional::CONDITION_CONTAINSBLANKS,
  289. <<<XML
  290. <conditionalFormatting sqref="C3:E5"><cfRule type="containsBlanks" dxfId="" priority="1"><formula>LEN(TRIM(C3))=0</formula></cfRule></conditionalFormatting>
  291. XML
  292. ],
  293. 'Not Blanks' => [
  294. Conditional::CONDITION_NOTCONTAINSBLANKS,
  295. <<<XML
  296. <conditionalFormatting sqref="C3:E5"><cfRule type="notContainsBlanks" dxfId="" priority="1"><formula>LEN(TRIM(C3))&gt;0</formula></cfRule></conditionalFormatting>
  297. XML
  298. ],
  299. ];
  300. }
  301. public function testWriteNonErrorConditionalFromWizard(): void
  302. {
  303. $spreadsheet = new Spreadsheet();
  304. $worksheet = $spreadsheet->getActiveSheet();
  305. $wizard = new Wizard\Errors($this->cellRange);
  306. $wizard->notError();
  307. $condition = $wizard->getConditional();
  308. $condition->setStyle($this->style);
  309. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  310. $writer = new Xlsx($spreadsheet);
  311. $writerWorksheet = new Xlsx\Worksheet($writer);
  312. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  313. $expected = <<<XML
  314. <conditionalFormatting sqref="C3:E5"><cfRule type="notContainsErrors" dxfId="" priority="1"><formula>NOT(ISERROR(C3))</formula></cfRule></conditionalFormatting>
  315. XML;
  316. self::assertStringContainsString($expected, $data);
  317. }
  318. public function testWriteErrorConditionalFromWizard(): void
  319. {
  320. $spreadsheet = new Spreadsheet();
  321. $worksheet = $spreadsheet->getActiveSheet();
  322. $wizard = new Wizard\Errors($this->cellRange);
  323. $wizard->isError();
  324. $condition = $wizard->getConditional();
  325. $condition->setStyle($this->style);
  326. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  327. $writer = new Xlsx($spreadsheet);
  328. $writerWorksheet = new Xlsx\Worksheet($writer);
  329. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  330. $expected = <<<XML
  331. <conditionalFormatting sqref="C3:E5"><cfRule type="containsErrors" dxfId="" priority="1"><formula>ISERROR(C3)</formula></cfRule></conditionalFormatting>
  332. XML;
  333. self::assertStringContainsString($expected, $data);
  334. }
  335. /**
  336. * @dataProvider errorsConditionalsProvider
  337. */
  338. public function testWriteErrorsConditionals(string $conditionalType, string $expected): void
  339. {
  340. $spreadsheet = new Spreadsheet();
  341. $worksheet = $spreadsheet->getActiveSheet();
  342. $condition = new Conditional();
  343. $condition->setConditionType($conditionalType);
  344. $condition->setStyle($this->style);
  345. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  346. $writer = new Xlsx($spreadsheet);
  347. $writerWorksheet = new Xlsx\Worksheet($writer);
  348. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  349. self::assertStringContainsString($expected, $data);
  350. }
  351. public function errorsConditionalsProvider(): array
  352. {
  353. return [
  354. 'Errors' => [
  355. Conditional::CONDITION_CONTAINSERRORS,
  356. <<<XML
  357. <conditionalFormatting sqref="C3:E5"><cfRule type="containsErrors" dxfId="" priority="1"><formula>ISERROR(C3)</formula></cfRule></conditionalFormatting>
  358. XML
  359. ],
  360. 'Not Errors' => [
  361. Conditional::CONDITION_NOTCONTAINSERRORS,
  362. <<<XML
  363. <conditionalFormatting sqref="C3:E5"><cfRule type="notContainsErrors" dxfId="" priority="1"><formula>NOT(ISERROR(C3))</formula></cfRule></conditionalFormatting>
  364. XML
  365. ],
  366. ];
  367. }
  368. public function testWriteUniqueConditionalFromWizard(): void
  369. {
  370. $spreadsheet = new Spreadsheet();
  371. $worksheet = $spreadsheet->getActiveSheet();
  372. $wizard = new Wizard\Duplicates($this->cellRange);
  373. $wizard->unique();
  374. $condition = $wizard->getConditional();
  375. $condition->setStyle($this->style);
  376. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  377. $writer = new Xlsx($spreadsheet);
  378. $writerWorksheet = new Xlsx\Worksheet($writer);
  379. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  380. $expected = <<<XML
  381. <conditionalFormatting sqref="C3:E5"><cfRule type="uniqueValues" dxfId="" priority="1"/></conditionalFormatting>
  382. XML;
  383. self::assertStringContainsString($expected, $data);
  384. }
  385. public function testWriteDuplicateConditionalFromWizard(): void
  386. {
  387. $spreadsheet = new Spreadsheet();
  388. $worksheet = $spreadsheet->getActiveSheet();
  389. $wizard = new Wizard\Duplicates($this->cellRange);
  390. $wizard->duplicates();
  391. $condition = $wizard->getConditional();
  392. $condition->setStyle($this->style);
  393. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  394. $writer = new Xlsx($spreadsheet);
  395. $writerWorksheet = new Xlsx\Worksheet($writer);
  396. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  397. $expected = <<<XML
  398. <conditionalFormatting sqref="C3:E5"><cfRule type="duplicateValues" dxfId="" priority="1"/></conditionalFormatting>
  399. XML;
  400. self::assertStringContainsString($expected, $data);
  401. }
  402. /**
  403. * @dataProvider duplicatesConditionalsProvider
  404. */
  405. public function testWriteDuplicatesConditionals(string $conditionalType, string $expected): void
  406. {
  407. $spreadsheet = new Spreadsheet();
  408. $worksheet = $spreadsheet->getActiveSheet();
  409. $condition = new Conditional();
  410. $condition->setConditionType($conditionalType);
  411. $condition->setStyle($this->style);
  412. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  413. $writer = new Xlsx($spreadsheet);
  414. $writerWorksheet = new Xlsx\Worksheet($writer);
  415. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  416. self::assertStringContainsString($expected, $data);
  417. }
  418. public function duplicatesConditionalsProvider(): array
  419. {
  420. return [
  421. 'Duplicates' => [
  422. Conditional::CONDITION_DUPLICATES,
  423. <<<XML
  424. <conditionalFormatting sqref="C3:E5"><cfRule type="duplicateValues" dxfId="" priority="1"/></conditionalFormatting>
  425. XML
  426. ],
  427. 'Unique' => [
  428. Conditional::CONDITION_UNIQUE,
  429. <<<XML
  430. <conditionalFormatting sqref="C3:E5"><cfRule type="uniqueValues" dxfId="" priority="1"/></conditionalFormatting>
  431. XML
  432. ],
  433. ];
  434. }
  435. public function testWriteExpressionConditionalFromWizard(): void
  436. {
  437. $spreadsheet = new Spreadsheet();
  438. $worksheet = $spreadsheet->getActiveSheet();
  439. $wizard = new Wizard\Expression($this->cellRange);
  440. $wizard->expression('=ISODD(A1)');
  441. $condition = $wizard->getConditional();
  442. $condition->setStyle($this->style);
  443. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  444. $writer = new Xlsx($spreadsheet);
  445. $writerWorksheet = new Xlsx\Worksheet($writer);
  446. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  447. $expected = <<<XML
  448. <conditionalFormatting sqref="C3:E5"><cfRule type="expression" dxfId="" priority="1"><formula>ISODD(C3)</formula></cfRule></conditionalFormatting>
  449. XML;
  450. self::assertStringContainsString($expected, $data);
  451. }
  452. /**
  453. * @dataProvider expressionsConditionalsProvider
  454. */
  455. public function testWriteExpressionConditionals(string $expression, string $expected): void
  456. {
  457. $spreadsheet = new Spreadsheet();
  458. $worksheet = $spreadsheet->getActiveSheet();
  459. $condition = new Conditional();
  460. $condition->setConditionType(Conditional::CONDITION_EXPRESSION);
  461. $condition->setStyle($this->style);
  462. $condition->setConditions([$expression]);
  463. $worksheet->setConditionalStyles($this->cellRange, [$condition]);
  464. $writer = new Xlsx($spreadsheet);
  465. $writerWorksheet = new Xlsx\Worksheet($writer);
  466. $data = $writerWorksheet->writeWorksheet($worksheet, []);
  467. self::assertStringContainsString($expected, $data);
  468. }
  469. public function expressionsConditionalsProvider(): array
  470. {
  471. return [
  472. 'Odd' => [
  473. 'ISODD(C3)',
  474. <<<XML
  475. <conditionalFormatting sqref="C3:E5"><cfRule type="expression" dxfId="" priority="1"><formula>ISODD(C3)</formula></cfRule></conditionalFormatting>
  476. XML
  477. ],
  478. 'Even' => [
  479. 'ISEVEN(C3)',
  480. <<<XML
  481. <conditionalFormatting sqref="C3:E5"><cfRule type="expression" dxfId="" priority="1"><formula>ISEVEN(C3)</formula></cfRule></conditionalFormatting>
  482. XML
  483. ],
  484. ];
  485. }
  486. }