|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mpdf; |
| 4 | + |
| 5 | +/** |
| 6 | + * mPDF's rotate property turns an absolutely positioned block a quarter or half turn, whether the angle is written |
| 7 | + * with "deg" or without, and on every PHP version |
| 8 | + */ |
| 9 | +class PositionedBlockRotateTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase |
| 10 | +{ |
| 11 | + |
| 12 | + /** |
| 13 | + * The first four numbers of the matrix transformRotate() writes for each turn |
| 14 | + */ |
| 15 | + const CLOCKWISE = '0.0000 -1.0000 1.0000 0.0000'; |
| 16 | + const COUNTER_CLOCKWISE = '0.0000 1.0000 -1.0000 0.0000'; |
| 17 | + const HALF_TURN = '-1.0000 -0.0000 0.0000 -1.0000'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The turns a positioned block with the given rotate value is drawn with |
| 21 | + * |
| 22 | + * @param string $rotate |
| 23 | + * |
| 24 | + * @return string[] |
| 25 | + */ |
| 26 | + private function turns($rotate) |
| 27 | + { |
| 28 | + $mpdf = new Mpdf(); |
| 29 | + $mpdf->compress = false; |
| 30 | + $mpdf->WriteHTML('<div style="position: absolute; top: 50mm; left: 50mm; width: 40mm; rotate: ' . $rotate . '">Turned</div>'); |
| 31 | + $pdf = $mpdf->Output('', 'S'); |
| 32 | + |
| 33 | + return array_values(array_filter([self::CLOCKWISE, self::COUNTER_CLOCKWISE, self::HALF_TURN], function ($turn) use ($pdf) { |
| 34 | + return strpos($pdf, $turn . ' ') !== false; |
| 35 | + })); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * The block is drawn turned the way the value gives, or not turned at all |
| 40 | + * |
| 41 | + * @dataProvider rotateProvider |
| 42 | + * |
| 43 | + * @param string $rotate |
| 44 | + * @param string[] $expected |
| 45 | + */ |
| 46 | + public function testRotate($rotate, $expected) |
| 47 | + { |
| 48 | + $this->assertSame($expected, $this->turns($rotate)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * rotate values, with the turn each gives |
| 53 | + * |
| 54 | + * @return array |
| 55 | + */ |
| 56 | + public function rotateProvider() |
| 57 | + { |
| 58 | + return [ |
| 59 | + '90' => ['90', [self::CLOCKWISE]], |
| 60 | + '90deg' => ['90deg', [self::CLOCKWISE]], |
| 61 | + '-90' => ['-90', [self::COUNTER_CLOCKWISE]], |
| 62 | + '-90deg' => ['-90deg', [self::COUNTER_CLOCKWISE]], |
| 63 | + '270deg is -90deg' => ['270deg', [self::COUNTER_CLOCKWISE]], |
| 64 | + '-270deg is 90deg' => ['-270deg', [self::CLOCKWISE]], |
| 65 | + '180' => ['180', [self::HALF_TURN]], |
| 66 | + '180deg' => ['180deg', [self::HALF_TURN]], |
| 67 | + '-180deg is 180deg' => ['-180deg', [self::HALF_TURN]], |
| 68 | + '45deg is not supported' => ['45deg', []], |
| 69 | + 'not an angle' => ['auto', []], |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments