value_parser_test.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. <?php
  2. /**
  3. * ezcDocumentPdfStyleInferenceTests
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Document
  23. * @version //autogen//
  24. * @subpackage Tests
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Test suite for class.
  29. *
  30. * @package Document
  31. * @subpackage Tests
  32. */
  33. class ezcDocumentPcssValueParserTests extends ezcTestCase
  34. {
  35. protected $document;
  36. protected $xpath;
  37. public static function suite()
  38. {
  39. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  40. }
  41. public static function getMeasureBoxValues()
  42. {
  43. return array(
  44. array(
  45. "11",
  46. array(
  47. 'top' => 11.,
  48. 'right' => 11.,
  49. 'bottom' => 11.,
  50. 'left' => 11.,
  51. ),
  52. '11.00mm 11.00mm 11.00mm 11.00mm',
  53. ),
  54. array(
  55. "11pt",
  56. array(
  57. 'top' => 3.9,
  58. 'right' => 3.9,
  59. 'bottom' => 3.9,
  60. 'left' => 3.9,
  61. ),
  62. '3.88mm 3.88mm 3.88mm 3.88mm',
  63. ),
  64. array(
  65. "11 12",
  66. array(
  67. 'top' => 11.,
  68. 'right' => 12.,
  69. 'bottom' => 11.,
  70. 'left' => 12.,
  71. ),
  72. '11.00mm 12.00mm 11.00mm 12.00mm',
  73. ),
  74. array(
  75. "11\t \r \n \t12",
  76. array(
  77. 'top' => 11.,
  78. 'right' => 12.,
  79. 'bottom' => 11.,
  80. 'left' => 12.,
  81. ),
  82. '11.00mm 12.00mm 11.00mm 12.00mm',
  83. ),
  84. array(
  85. "11 12 13",
  86. array(
  87. 'top' => 11.,
  88. 'right' => 12.,
  89. 'bottom' => 13.,
  90. 'left' => 12.,
  91. ),
  92. '11.00mm 12.00mm 13.00mm 12.00mm',
  93. ),
  94. array(
  95. "11 12 13 14",
  96. array(
  97. 'top' => 11.,
  98. 'right' => 12.,
  99. 'bottom' => 13.,
  100. 'left' => 14.,
  101. ),
  102. '11.00mm 12.00mm 13.00mm 14.00mm'
  103. ),
  104. array(
  105. "11mm 12in 13px 14pt",
  106. array(
  107. 'top' => 11.,
  108. 'right' => 304.8,
  109. 'bottom' => 4.6,
  110. 'left' => 4.94,
  111. ),
  112. '11.00mm 304.80mm 4.59mm 4.94mm',
  113. ),
  114. );
  115. }
  116. /**
  117. * @dataProvider getMeasureBoxValues
  118. */
  119. public function testMeasureBoxValueHandler( $input, $expectation, $string )
  120. {
  121. $value = new ezcDocumentPcssStyleMeasureBoxValue();
  122. $value->parse( $input );
  123. $this->assertEquals(
  124. $expectation,
  125. $value->value,
  126. 'Invalid box measures read.', .1
  127. );
  128. $this->assertEquals(
  129. $string,
  130. (string) $value,
  131. 'Invalid measure box string serialization.'
  132. );
  133. }
  134. public static function getColorValues()
  135. {
  136. return array(
  137. array(
  138. "#000000",
  139. array(
  140. 'red' => 0.,
  141. 'green' => 0.,
  142. 'blue' => 0.,
  143. 'alpha' => 0.,
  144. ),
  145. '#000000',
  146. ),
  147. array(
  148. "#ffffff",
  149. array(
  150. 'red' => 1.,
  151. 'green' => 1.,
  152. 'blue' => 1.,
  153. 'alpha' => 0.,
  154. ),
  155. '#ffffff',
  156. ),
  157. array(
  158. "#babdb6",
  159. array(
  160. 'red' => .73,
  161. 'green' => .74,
  162. 'blue' => .71,
  163. 'alpha' => 0.,
  164. ),
  165. '#babdb6',
  166. ),
  167. array(
  168. "#babdb6b0",
  169. array(
  170. 'red' => .73,
  171. 'green' => .74,
  172. 'blue' => .71,
  173. 'alpha' => .69,
  174. ),
  175. '#babdb6b0',
  176. ),
  177. array(
  178. "#BABDB6",
  179. array(
  180. 'red' => .73,
  181. 'green' => .74,
  182. 'blue' => .71,
  183. 'alpha' => 0.,
  184. ),
  185. '#babdb6',
  186. ),
  187. array(
  188. "#000",
  189. array(
  190. 'red' => 0.,
  191. 'green' => 0.,
  192. 'blue' => 0.,
  193. 'alpha' => 0.,
  194. ),
  195. '#000000',
  196. ),
  197. array(
  198. "#fff",
  199. array(
  200. 'red' => 1.,
  201. 'green' => 1.,
  202. 'blue' => 1.,
  203. 'alpha' => 0.,
  204. ),
  205. '#ffffff',
  206. ),
  207. array(
  208. "#bad",
  209. array(
  210. 'red' => .73,
  211. 'green' => .67,
  212. 'blue' => .87,
  213. 'alpha' => 0.,
  214. ),
  215. '#bbaadd',
  216. ),
  217. array(
  218. "#bad6",
  219. array(
  220. 'red' => .73,
  221. 'green' => .67,
  222. 'blue' => .87,
  223. 'alpha' => .4,
  224. ),
  225. '#bbaadd66',
  226. ),
  227. array(
  228. "#BAD",
  229. array(
  230. 'red' => .73,
  231. 'green' => .67,
  232. 'blue' => .87,
  233. 'alpha' => 0.,
  234. ),
  235. '#bbaadd',
  236. ),
  237. array(
  238. "rgb( 0, 255, 9823 )",
  239. array(
  240. 'red' => .0,
  241. 'green' => 1.,
  242. 'blue' => .37,
  243. 'alpha' => 0.,
  244. ),
  245. '#00ff5f',
  246. ),
  247. array(
  248. " RGB ( 0 , 10 , 127 ) ",
  249. array(
  250. 'red' => .0,
  251. 'green' => .04,
  252. 'blue' => .5,
  253. 'alpha' => 0.,
  254. ),
  255. '#000a7f',
  256. ),
  257. array(
  258. "rgba( 0, 255, 1023, 127 )",
  259. array(
  260. 'red' => .0,
  261. 'green' => 1.,
  262. 'blue' => 1.,
  263. 'alpha' => .5,
  264. ),
  265. '#00ffff7f',
  266. ),
  267. array(
  268. " RGBA ( 12 , 23 , 1023 , 12 ) ",
  269. array(
  270. 'red' => .05,
  271. 'green' => .1,
  272. 'blue' => 1.,
  273. 'alpha' => .05,
  274. ),
  275. '#0c17ff0c',
  276. ),
  277. array(
  278. "transparent",
  279. array(
  280. 'red' => 0.,
  281. 'green' => 0.,
  282. 'blue' => 0.,
  283. 'alpha' => 1.,
  284. ),
  285. '#000000ff',
  286. ),
  287. array(
  288. "none",
  289. array(
  290. 'red' => 0.,
  291. 'green' => 0.,
  292. 'blue' => 0.,
  293. 'alpha' => 1.,
  294. ),
  295. '#000000ff',
  296. ),
  297. );
  298. }
  299. /**
  300. * @dataProvider getColorValues
  301. */
  302. public function testColorValueHandler( $input, $expectation, $string = '' )
  303. {
  304. $value = new ezcDocumentPcssStyleColorValue();
  305. $value->parse( $input );
  306. $this->assertEquals(
  307. $expectation,
  308. $value->value,
  309. 'Invalid color values read.', .01
  310. );
  311. $this->assertEquals(
  312. $string,
  313. (string) $value,
  314. 'Invalid color string serialization.'
  315. );
  316. }
  317. public function testInvalidColorSpecification()
  318. {
  319. try {
  320. $value = new ezcDocumentPcssStyleColorValue();
  321. $value->parse( 'something invalid' );
  322. $this->fail( 'Expected ezcDocumentParserException.' );
  323. } catch ( ezcDocumentParserException $e )
  324. { /* Expected */ }
  325. }
  326. public static function getLineStyleValues()
  327. {
  328. return array(
  329. array(
  330. "solid",
  331. "solid",
  332. 'solid',
  333. ),
  334. array(
  335. " dotted ",
  336. 'dotted',
  337. 'dotted',
  338. ),
  339. array(
  340. "\t\ngroove\r",
  341. 'groove',
  342. 'groove',
  343. ),
  344. );
  345. }
  346. /**
  347. * @dataProvider getLineStyleValues
  348. */
  349. public function testLineValueHandler( $input, $expectation, $string = '' )
  350. {
  351. $value = new ezcDocumentPcssStyleLineValue();
  352. $value->parse( $input );
  353. $this->assertEquals(
  354. $expectation,
  355. $value->value,
  356. 'Invalid style style value read.', .01
  357. );
  358. $this->assertEquals(
  359. $string,
  360. (string) $value,
  361. 'Invalid style style string serialization.'
  362. );
  363. }
  364. public static function getBorderStyleValues()
  365. {
  366. return array(
  367. array(
  368. "1mm",
  369. array(
  370. 'width' => 1,
  371. 'style' => 'solid',
  372. 'color' => array(
  373. 'red' => 1.,
  374. 'green' => 1.,
  375. 'blue' => 1.,
  376. 'alpha' => 0.,
  377. ),
  378. ),
  379. '1.00mm solid #ffffff',
  380. ),
  381. array(
  382. "dashed",
  383. array(
  384. 'width' => 0,
  385. 'style' => 'dashed',
  386. 'color' => array(
  387. 'red' => 1.,
  388. 'green' => 1.,
  389. 'blue' => 1.,
  390. 'alpha' => 0.,
  391. ),
  392. ),
  393. '0.00mm dashed #ffffff',
  394. ),
  395. array(
  396. "rgb( 255, 0, 0 )",
  397. array(
  398. 'width' => 0,
  399. 'style' => 'solid',
  400. 'color' => array(
  401. 'red' => 1.,
  402. 'green' => 0.,
  403. 'blue' => 0.,
  404. 'alpha' => 0.,
  405. ),
  406. ),
  407. '0.00mm solid #ff0000',
  408. ),
  409. array(
  410. "1pt #F00",
  411. array(
  412. 'width' => .35,
  413. 'style' => 'solid',
  414. 'color' => array(
  415. 'red' => 1.,
  416. 'green' => 0.,
  417. 'blue' => 0.,
  418. 'alpha' => 0.,
  419. ),
  420. ),
  421. '0.35mm solid #ff0000',
  422. ),
  423. array(
  424. "1 inset #0f0",
  425. array(
  426. 'width' => 1.,
  427. 'style' => 'inset',
  428. 'color' => array(
  429. 'red' => 0.,
  430. 'green' => 1.,
  431. 'blue' => 0.,
  432. 'alpha' => 0.,
  433. ),
  434. ),
  435. '1.00mm inset #00ff00',
  436. ),
  437. );
  438. }
  439. /**
  440. * @dataProvider getBorderStyleValues
  441. */
  442. public function testBorderValueHandler( $input, $expectation, $string = '' )
  443. {
  444. $value = new ezcDocumentPcssStyleBorderValue();
  445. $value->parse( $input );
  446. $this->assertEquals(
  447. $expectation,
  448. $value->value,
  449. 'Invalid border style value read.', .01
  450. );
  451. $this->assertEquals(
  452. $string,
  453. (string) $value,
  454. 'Invalid border style string serialization.'
  455. );
  456. }
  457. public static function getColorBoxStyleValues()
  458. {
  459. return array(
  460. array(
  461. "#0f0 #ff0000 rgb( 0, 0, 255 )",
  462. array(
  463. 'top' => array(
  464. 'red' => 0.,
  465. 'green' => 1.,
  466. 'blue' => 0.,
  467. 'alpha' => 0.,
  468. ),
  469. 'right' => array(
  470. 'red' => 1.,
  471. 'green' => 0.,
  472. 'blue' => 0.,
  473. 'alpha' => 0.,
  474. ),
  475. 'bottom' => array(
  476. 'red' => 0.,
  477. 'green' => 0.,
  478. 'blue' => 1.,
  479. 'alpha' => 0.,
  480. ),
  481. 'left' => array(
  482. 'red' => 1.,
  483. 'green' => 0.,
  484. 'blue' => 0.,
  485. 'alpha' => 0.,
  486. ),
  487. ),
  488. '#00ff00 #ff0000 #0000ff #ff0000',
  489. ),
  490. array(
  491. "#fF0",
  492. array(
  493. 'top' => array(
  494. 'red' => 1.,
  495. 'green' => 1.,
  496. 'blue' => 0.,
  497. 'alpha' => 0.,
  498. ),
  499. 'right' => array(
  500. 'red' => 1.,
  501. 'green' => 1.,
  502. 'blue' => 0.,
  503. 'alpha' => 0.,
  504. ),
  505. 'bottom' => array(
  506. 'red' => 1.,
  507. 'green' => 1.,
  508. 'blue' => 0.,
  509. 'alpha' => 0.,
  510. ),
  511. 'left' => array(
  512. 'red' => 1.,
  513. 'green' => 1.,
  514. 'blue' => 0.,
  515. 'alpha' => 0.,
  516. ),
  517. ),
  518. '#ffff00 #ffff00 #ffff00 #ffff00',
  519. ),
  520. );
  521. }
  522. /**
  523. * @dataProvider getColorBoxStyleValues
  524. */
  525. public function testColorBoxValueHandler( $input, $expectation, $string = '' )
  526. {
  527. $value = new ezcDocumentPcssStyleColorBoxValue();
  528. $value->parse( $input );
  529. $this->assertEquals(
  530. $expectation,
  531. $value->value,
  532. 'Invalid color value read.', .01
  533. );
  534. $this->assertEquals(
  535. $string,
  536. (string) $value,
  537. 'Invalid color box string serialization.'
  538. );
  539. }
  540. public static function getLineBoxStyleValues()
  541. {
  542. return array(
  543. array(
  544. "solid double outset",
  545. array(
  546. 'top' => 'solid',
  547. 'right' => 'double',
  548. 'bottom' => 'outset',
  549. 'left' => 'double',
  550. ),
  551. 'solid double outset double',
  552. ),
  553. array(
  554. "inset",
  555. array(
  556. 'top' => 'inset',
  557. 'right' => 'inset',
  558. 'bottom' => 'inset',
  559. 'left' => 'inset',
  560. ),
  561. 'inset inset inset inset',
  562. ),
  563. );
  564. }
  565. /**
  566. * @dataProvider getLineBoxStyleValues
  567. */
  568. public function testLineBoxValueHandler( $input, $expectation, $string = '' )
  569. {
  570. $value = new ezcDocumentPcssStyleLineBoxValue();
  571. $value->parse( $input );
  572. $this->assertEquals(
  573. $expectation,
  574. $value->value,
  575. 'Invalid style value read.', .01
  576. );
  577. $this->assertEquals(
  578. $string,
  579. (string) $value,
  580. 'Invalid style box string serialization.'
  581. );
  582. }
  583. public static function getBorderBoxStyleValues()
  584. {
  585. return array(
  586. array(
  587. "1 inset #0f0",
  588. array(
  589. 'top' => array(
  590. 'width' => 1.,
  591. 'style' => 'inset',
  592. 'color' => array(
  593. 'red' => 0.,
  594. 'green' => 1.,
  595. 'blue' => 0.,
  596. 'alpha' => 0.,
  597. ),
  598. ),
  599. 'right' => array(
  600. 'width' => 1.,
  601. 'style' => 'inset',
  602. 'color' => array(
  603. 'red' => 0.,
  604. 'green' => 1.,
  605. 'blue' => 0.,
  606. 'alpha' => 0.,
  607. ),
  608. ),
  609. 'bottom' => array(
  610. 'width' => 1.,
  611. 'style' => 'inset',
  612. 'color' => array(
  613. 'red' => 0.,
  614. 'green' => 1.,
  615. 'blue' => 0.,
  616. 'alpha' => 0.,
  617. ),
  618. ),
  619. 'left' => array(
  620. 'width' => 1.,
  621. 'style' => 'inset',
  622. 'color' => array(
  623. 'red' => 0.,
  624. 'green' => 1.,
  625. 'blue' => 0.,
  626. 'alpha' => 0.,
  627. ),
  628. ),
  629. ),
  630. '1.00mm inset #00ff00 1.00mm inset #00ff00 1.00mm inset #00ff00 1.00mm inset #00ff00',
  631. ),
  632. array(
  633. "1mm #fF0 outset 2mm",
  634. array(
  635. 'top' => array(
  636. 'width' => 1.,
  637. 'style' => 'solid',
  638. 'color' => array(
  639. 'red' => 1.,
  640. 'green' => 1.,
  641. 'blue' => 0.,
  642. 'alpha' => 0.,
  643. ),
  644. ),
  645. 'right' => array(
  646. 'width' => 0.,
  647. 'style' => 'outset',
  648. 'color' => array(
  649. 'red' => 1.,
  650. 'green' => 1.,
  651. 'blue' => 1.,
  652. 'alpha' => 0.,
  653. ),
  654. ),
  655. 'bottom' => array(
  656. 'width' => 2.,
  657. 'style' => 'solid',
  658. 'color' => array(
  659. 'red' => 1.,
  660. 'green' => 1.,
  661. 'blue' => 1.,
  662. 'alpha' => 0.,
  663. ),
  664. ),
  665. 'left' => array(
  666. 'width' => 0.,
  667. 'style' => 'outset',
  668. 'color' => array(
  669. 'red' => 1.,
  670. 'green' => 1.,
  671. 'blue' => 1.,
  672. 'alpha' => 0.,
  673. ),
  674. ),
  675. ),
  676. '1.00mm solid #ffff00 0.00mm outset #ffffff 2.00mm solid #ffffff 0.00mm outset #ffffff',
  677. ),
  678. );
  679. }
  680. /**
  681. * @dataProvider getBorderBoxStyleValues
  682. */
  683. public function testBorderBoxValueHandler( $input, $expectation, $string = '' )
  684. {
  685. $value = new ezcDocumentPcssStyleBorderBoxValue();
  686. $value->parse( $input );
  687. $this->assertEquals(
  688. $expectation,
  689. $value->value,
  690. 'Invalid border style value read.', .01
  691. );
  692. $this->assertEquals(
  693. $string,
  694. (string) $value,
  695. 'Invalid border style string serialization.'
  696. );
  697. }
  698. public static function getUrlStyleValues()
  699. {
  700. return array(
  701. array(
  702. "url( foo.ttf )",
  703. array(
  704. "foo.ttf"
  705. ),
  706. "url( foo.ttf )",
  707. ),
  708. array(
  709. "url(foo.ttf),local(font.pfb),url(/some/../path/to/font.foo)",
  710. array(
  711. "foo.ttf",
  712. "font.pfb",
  713. "/some/../path/to/font.foo",
  714. ),
  715. "url( foo.ttf ), url( font.pfb ), url( /some/../path/to/font.foo )",
  716. ),
  717. );
  718. }
  719. /**
  720. * @dataProvider getUrlStyleValues
  721. */
  722. public function testSrcValueHandler( $input, $expectation, $string = '' )
  723. {
  724. $value = new ezcDocumentPcssStyleSrcValue();
  725. $value->parse( $input );
  726. $this->assertEquals(
  727. $expectation,
  728. $value->value,
  729. 'Invalid src style value read.', .01
  730. );
  731. $this->assertEquals(
  732. $string,
  733. (string) $value,
  734. 'Invalid src style string serialization.'
  735. );
  736. }
  737. public static function getListStyleValues()
  738. {
  739. return array(
  740. array(
  741. 'single',
  742. array( 'single' ),
  743. 'single'
  744. ),
  745. array(
  746. 'single ',
  747. array( 'single' ),
  748. 'single'
  749. ),
  750. array(
  751. 'first second',
  752. array( 'first', 'second' ),
  753. 'first second'
  754. ),
  755. array(
  756. 'first second ',
  757. array( 'first', 'second' ),
  758. 'first second'
  759. ),
  760. array(
  761. ' first second ',
  762. array( 'first', 'second' ),
  763. 'first second'
  764. ),
  765. array(
  766. ' first second third',
  767. array( 'first', 'second', 'third' ),
  768. 'first second third'
  769. ),
  770. );
  771. }
  772. /**
  773. * @dataProvider getListStyleValues
  774. */
  775. public function testListValueHandler( $input, $expectation, $string = '' )
  776. {
  777. $value = new ezcDocumentPcssStyleListValue();
  778. $value->parse( $input );
  779. $this->assertEquals(
  780. $expectation,
  781. $value->value,
  782. 'Incorrect list value read.'
  783. );
  784. $this->assertEquals(
  785. $string,
  786. (string) $value,
  787. 'Invalid list value string serialization.'
  788. );
  789. }
  790. }
  791. ?>