|
20 | 20 |
|
21 | 21 | namespace PhpOffice\PhpPresentation\Tests\Shape; |
22 | 22 |
|
| 23 | +use PhpOffice\PhpPresentation\PhpPresentation; |
23 | 24 | use PhpOffice\PhpPresentation\Shape\AutoShape; |
| 25 | +use PhpOffice\PhpPresentation\Style\Color; |
| 26 | +use PhpOffice\PhpPresentation\Style\Fill; |
24 | 27 | use PhpOffice\PhpPresentation\Style\Outline; |
| 28 | +use PhpOffice\PhpPresentation\Writer\PowerPoint2007; |
25 | 29 | use PHPUnit\Framework\TestCase; |
| 30 | +use ZipArchive; |
26 | 31 |
|
27 | 32 | class AutoShapeTest extends TestCase |
28 | 33 | { |
@@ -64,4 +69,79 @@ public function testType(): void |
64 | 69 | self::assertInstanceOf(AutoShape::class, $object->setType(AutoShape::TYPE_HEXAGON)); |
65 | 70 | self::assertEquals(AutoShape::TYPE_HEXAGON, $object->getType()); |
66 | 71 | } |
| 72 | + |
| 73 | + public function testPixelSetterComputesAdjAndAffectsHash(): void |
| 74 | + { |
| 75 | + $w = 200; // px |
| 76 | + $h = 100; // px |
| 77 | + $px1 = 5; // softer radius |
| 78 | + $px2 = 10; // larger radius |
| 79 | + |
| 80 | + $s1 = (new AutoShape()) |
| 81 | + ->setType(AutoShape::TYPE_ROUNDED_RECTANGLE) |
| 82 | + ->setWidth($w)->setHeight($h) |
| 83 | + ->setRoundRectCorner($px1); |
| 84 | + |
| 85 | + $s2 = (clone $s1)->setRoundRectCorner($px2); |
| 86 | + |
| 87 | + // adj expected: round(px / (min(w,h)/2) * 50000) |
| 88 | + $minHalf = (int) floor(min($w, $h) / 2); // 50 |
| 89 | + $expectedAdj1 = (int) round($px1 / $minHalf * 50000); // 5/50 * 50000 = 5000 |
| 90 | + $expectedAdj2 = (int) round($px2 / $minHalf * 50000); // 10/50 * 50000 = 10000 |
| 91 | + |
| 92 | + self::assertSame($expectedAdj1, $s1->getRoundRectAdj()); |
| 93 | + self::assertSame($expectedAdj2, $s2->getRoundRectAdj()); |
| 94 | + |
| 95 | + // Hash must differ when radius differs |
| 96 | + self::assertNotSame($s1->getHashCode(), $s2->getHashCode()); |
| 97 | + } |
| 98 | + |
| 99 | + public function testNoRadiusByDefaultIsNull(): void |
| 100 | + { |
| 101 | + $shape = new AutoShape(); |
| 102 | + self::assertNull($shape->getRoundRectAdj()); |
| 103 | + } |
| 104 | + |
| 105 | + public function testWriterEmitsAdjGuideForRoundRect(): void |
| 106 | + { |
| 107 | + $ppt = new PhpPresentation(); |
| 108 | + $slide = $ppt->getActiveSlide(); |
| 109 | + |
| 110 | + $width = 200; |
| 111 | + $height = 100; |
| 112 | + $padding = 5; |
| 113 | + $minHalf = (int) floor(min($width, $height) / 2); |
| 114 | + $expectedAdj = (int) round($padding / $minHalf * 50000); // 5000 |
| 115 | + |
| 116 | + $shape = (new AutoShape()) |
| 117 | + ->setType(AutoShape::TYPE_ROUNDED_RECTANGLE) |
| 118 | + ->setWidth($width)->setHeight($height) |
| 119 | + ->setRoundRectCorner($padding); |
| 120 | + |
| 121 | + // Give it a fill so it's an obvious shape |
| 122 | + $shape->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFFFFFFF')); |
| 123 | + $slide->addShape($shape); |
| 124 | + |
| 125 | + $tmpFile = tempnam(sys_get_temp_dir(), 'pptx_'); |
| 126 | + $writer = new PowerPoint2007($ppt); |
| 127 | + $writer->save($tmpFile); |
| 128 | + |
| 129 | + // Open the pptx and read slide1.xml |
| 130 | + $zip = new ZipArchive(); |
| 131 | + $this->assertTrue($zip->open($tmpFile) === true, 'Failed to open pptx zip'); |
| 132 | + $xml = $zip->getFromName('ppt/slides/slide1.xml'); |
| 133 | + $zip->close(); |
| 134 | + @unlink($tmpFile); |
| 135 | + |
| 136 | + $this->assertIsString($xml); |
| 137 | + |
| 138 | + // Must contain roundRect geometry and the adj guide with expected value |
| 139 | + $this->assertStringContainsString('<a:prstGeom prst="roundRect">', $xml); |
| 140 | + |
| 141 | + // fmla="val N" (there is a space after 'val' in writer) |
| 142 | + $this->assertMatchesRegularExpression( |
| 143 | + sprintf('/<a:gd[^>]+name="adj"[^>]+fmla="val %d"/', $expectedAdj), |
| 144 | + $xml |
| 145 | + ); |
| 146 | + } |
67 | 147 | } |
0 commit comments