|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpWordTests\Writer\Word2007\Element; |
| 6 | + |
| 7 | +use PhpOffice\PhpWord\Element\TextBox as TextBoxElement; |
| 8 | +use PhpOffice\PhpWord\Shared\XMLWriter; |
| 9 | +use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle; |
| 10 | +use PhpOffice\PhpWord\Writer\Word2007\Element\TextBox; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +class TextBoxTest extends TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @dataProvider textBoxColorProvider |
| 17 | + */ |
| 18 | + public function testTextBoxGeneratesCorrectXml( |
| 19 | + ?string $bgColor, |
| 20 | + ?string $borderColor, |
| 21 | + string $expectedFillColorAttribute, |
| 22 | + string $expectedBorderColorAttribute |
| 23 | + ): void { |
| 24 | + // Arrange |
| 25 | + $xmlWriter = new XMLWriter(); |
| 26 | + $style = new TextBoxStyle(); |
| 27 | + |
| 28 | + if ($bgColor !== null) { |
| 29 | + $style->setBgColor($bgColor); |
| 30 | + } |
| 31 | + |
| 32 | + if ($borderColor !== null) { |
| 33 | + $style->setBorderColor($borderColor); |
| 34 | + } |
| 35 | + |
| 36 | + $textBoxElement = new TextBoxElement($style); |
| 37 | + $textBox = new TextBox($xmlWriter, $textBoxElement); |
| 38 | + |
| 39 | + // Act |
| 40 | + $textBox->write(); |
| 41 | + $output = $xmlWriter->getData(); |
| 42 | + |
| 43 | + // Assert |
| 44 | + self::assertStringContainsString($expectedFillColorAttribute, $output, 'Background color should be applied.'); |
| 45 | + self::assertStringContainsString($expectedBorderColorAttribute, $output, 'Border color should be applied correctly.'); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Data provider for testing different combinations of background and border colors. |
| 50 | + */ |
| 51 | + protected function textBoxColorProvider(): array |
| 52 | + { |
| 53 | + return [ |
| 54 | + // Case 1: Background color set, border color set |
| 55 | + 'With both colors' => [ |
| 56 | + '#FF0000', |
| 57 | + '#000000', |
| 58 | + 'fillcolor="#FF0000"', |
| 59 | + 'stroke color="#000000"', |
| 60 | + ], |
| 61 | + // Case 2: Background color set, no border color |
| 62 | + 'With background only' => [ |
| 63 | + '#00FF00', |
| 64 | + null, |
| 65 | + 'fillcolor="#00FF00"', |
| 66 | + 'stroked="f" strokecolor="white"', |
| 67 | + ], |
| 68 | + // Case 3: No background color, border color set |
| 69 | + 'With border only' => [ |
| 70 | + null, |
| 71 | + '#123456', |
| 72 | + 'filled="f"', |
| 73 | + 'stroke color="#123456"', |
| 74 | + ], |
| 75 | + // Case 4: Neither background nor border color set |
| 76 | + 'Without any colors' => [ |
| 77 | + null, |
| 78 | + null, |
| 79 | + 'filled="f"', |
| 80 | + 'stroked="f" strokecolor="white"', |
| 81 | + ], |
| 82 | + ]; |
| 83 | + } |
| 84 | +} |
0 commit comments