Skip to content

Commit 1431994

Browse files
authored
Merge pull request #40 from Progi1984/improveCoverage
Improved Unit Tests
2 parents 751b70e + 84f4947 commit 1431994

File tree

6 files changed

+148
-18
lines changed

6 files changed

+148
-18
lines changed

src/Common/File.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717

1818
namespace PhpOffice\Common;
1919

20-
/**
21-
* Drawing
22-
*/
20+
use ZipArchive;
21+
2322
class File
2423
{
2524
/**
@@ -29,7 +28,7 @@ class File
2928
*
3029
* @return bool
3130
*/
32-
public static function fileExists($pFilename)
31+
public static function fileExists(string $pFilename): bool
3332
{
3433
// Sick construction, but it seems that
3534
// file_exists returns strange values when
@@ -39,7 +38,7 @@ public static function fileExists($pFilename)
3938
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
4039
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
4140

42-
$zip = new \ZipArchive();
41+
$zip = new ZipArchive();
4342
if ($zip->open($zipFile) === true) {
4443
$returnValue = ($zip->getFromName($archiveFile) !== false);
4544
$zip->close();
@@ -71,7 +70,7 @@ public static function fileGetContents(string $pFilename): ?string
7170
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
7271
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
7372

74-
$zip = new \ZipArchive();
73+
$zip = new ZipArchive();
7574
if ($zip->open($zipFile) === true) {
7675
$returnValue = $zip->getFromName($archiveFile);
7776
$zip->close();

src/Common/Text.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ public static function isUTF8(string $value = ''): bool
140140
*
141141
* @param string|null $value
142142
*
143-
* @return string
143+
* @return string|null
144144
*/
145-
public static function toUTF8(?string $value = ''): string
145+
public static function toUTF8(?string $value = ''): ?string
146146
{
147147
if (!is_null($value) && !self::isUTF8($value)) {
148148
$value = utf8_encode($value);

src/Common/XMLWriter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ public function writeElementBlock(string $element, $attributes, string $value =
138138
*
139139
* @param bool $condition
140140
* @param string $element
141-
* @param string $attribute
141+
* @param string|null $attribute
142142
* @param mixed $value
143143
*
144144
* @return void
145145
*/
146-
public function writeElementIf($condition, $element, $attribute = null, $value = null)
146+
public function writeElementIf(bool $condition, string $element, ?string $attribute = null, $value = null)
147147
{
148-
if ($condition == true) {
148+
if ($condition) {
149149
if (is_null($attribute)) {
150150
$this->writeElement($element, $value);
151151
} else {
@@ -165,9 +165,9 @@ public function writeElementIf($condition, $element, $attribute = null, $value =
165165
*
166166
* @return void
167167
*/
168-
public function writeAttributeIf($condition, $attribute, $value)
168+
public function writeAttributeIf(bool $condition, string $attribute, $value)
169169
{
170-
if ($condition == true) {
170+
if ($condition) {
171171
$this->writeAttribute($attribute, $value);
172172
}
173173
}

tests/Common/Tests/FileTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public function testFileExists(): void
3434
$this->assertTrue(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#[Content_Types].xml'));
3535
$this->assertFalse(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#404.xml'));
3636
$this->assertFalse(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . '404.pptx#404.xml'));
37+
38+
// Set a ZIP en ReadOnly Mode
39+
$zipTest = tempnam(sys_get_temp_dir(), 'PhpOfficeCommon');
40+
copy($pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx', $zipTest);
41+
chmod($zipTest, 333);
42+
$this->assertFalse(File::fileExists('zip://' . $zipTest));
43+
unlink($zipTest);
3744
}
3845

3946
public function testGetFileContents(): void

tests/Common/Tests/TextTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@
2727
*/
2828
class TextTest extends TestCase
2929
{
30-
public function testControlCharacters(): void
30+
public function testControlCharactersPHP2OOXML(): void
3131
{
3232
$this->assertEquals('', Text::controlCharacterPHP2OOXML());
3333
$this->assertEquals('aeiou', Text::controlCharacterPHP2OOXML('aeiou'));
3434
$this->assertEquals('àéîöù', Text::controlCharacterPHP2OOXML('àéîöù'));
3535

3636
$value = rand(0, 8);
37-
$this->assertEquals('_x' . sprintf('%04s', strtoupper(dechex($value))) . '_', Text::controlCharacterPHP2OOXML(chr($value)));
37+
$this->assertEquals(
38+
'_x' . sprintf('%04s', strtoupper(dechex($value))) . '_',
39+
Text::controlCharacterPHP2OOXML(chr($value))
40+
);
41+
}
3842

43+
public function testControlCharactersOOXML2PHP(): void
44+
{
3945
$this->assertEquals('', Text::controlCharacterOOXML2PHP(''));
4046
$this->assertEquals(chr(0x08), Text::controlCharacterOOXML2PHP('_x0008_'));
4147
}
@@ -60,16 +66,20 @@ public function testChr(): void
6066
$this->assertEquals('', Text::chr(2097152));
6167
}
6268

63-
/**
64-
* Is UTF8
65-
*/
6669
public function testIsUTF8(): void
6770
{
6871
$this->assertTrue(Text::isUTF8(''));
6972
$this->assertTrue(Text::isUTF8('éééé'));
7073
$this->assertFalse(Text::isUTF8(utf8_decode('éééé')));
7174
}
7275

76+
public function testToUtf8(): void
77+
{
78+
$this->assertNull(Text::toUTF8(null));
79+
$this->assertEquals('eeee', Text::toUTF8('eeee'));
80+
$this->assertEquals('éééé', Text::toUTF8('éééé'));
81+
}
82+
7383
/**
7484
* Test unicode conversion
7585
*/

tests/Common/Tests/XMLWriterTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ public function testConstruct(): void
4343
$this->assertEquals('<element>BBB</element>' . chr(10), $object->getData());
4444
}
4545

46+
public function testConstructCompatibility(): void
47+
{
48+
$object = new XMLWriter(XMLWriter::STORAGE_MEMORY, null, false);
49+
$object->startElement('element');
50+
$object->startElement('sub');
51+
$object->text('CCC');
52+
$object->endElement();
53+
$object->endElement();
54+
$this->assertEquals(
55+
'<element>' . PHP_EOL . ' <sub>CCC</sub>' . PHP_EOL . '</element>' . PHP_EOL,
56+
$object->getData()
57+
);
58+
$object = new XMLWriter(XMLWriter::STORAGE_MEMORY, null, true);
59+
$object->startElement('element');
60+
$object->startElement('sub');
61+
$object->text('CCC');
62+
$object->endElement();
63+
$object->endElement();
64+
$this->assertEquals(
65+
'<element><sub>CCC</sub></element>',
66+
$object->getData()
67+
);
68+
}
69+
4670
public function testWriteAttribute(): void
4771
{
4872
$xmlWriter = new XMLWriter();
@@ -53,6 +77,23 @@ public function testWriteAttribute(): void
5377
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
5478
}
5579

80+
public function testWriteAttributeIf(): void
81+
{
82+
$xmlWriter = new XMLWriter();
83+
$xmlWriter->startElement('element');
84+
$xmlWriter->writeAttributeIf(true, 'name', 'value');
85+
$xmlWriter->endElement();
86+
87+
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
88+
89+
$xmlWriter = new XMLWriter();
90+
$xmlWriter->startElement('element');
91+
$xmlWriter->writeAttributeIf(false, 'name', 'value');
92+
$xmlWriter->endElement();
93+
94+
$this->assertSame('<element/>' . chr(10), $xmlWriter->getData());
95+
}
96+
5697
public function testWriteAttributeShouldWriteFloatValueLocaleIndependent(): void
5798
{
5899
$value = 1.2;
@@ -73,4 +114,77 @@ public function testWriteAttributeShouldWriteFloatValueLocaleIndependent(): void
73114
}
74115
$this->assertSame('<element name="1.2"/>' . chr(10), $xmlWriter->getData());
75116
}
117+
118+
public function testWriteElementBlock(): void
119+
{
120+
$xmlWriter = new XMLWriter();
121+
$xmlWriter->writeElementBlock('element', 'name');
122+
123+
$this->assertSame('<element name=""/>' . chr(10), $xmlWriter->getData());
124+
125+
$xmlWriter = new XMLWriter();
126+
$xmlWriter->writeElementBlock('element', 'name', 'value');
127+
128+
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
129+
130+
$xmlWriter = new XMLWriter();
131+
$xmlWriter->writeElementBlock('element', ['name' => 'value']);
132+
133+
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
134+
135+
$xmlWriter = new XMLWriter();
136+
$xmlWriter->writeElementBlock('element', ['name' => 'value'], 'value2');
137+
138+
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
139+
}
140+
141+
/**
142+
* @dataProvider dataProviderWriteElementIf
143+
*/
144+
public function testWriteElementIf(bool $condition, ?string $attribute, ?string $value, string $expected): void
145+
{
146+
$xmlWriter = new XMLWriter();
147+
$xmlWriter->writeElementIf($condition, 'element', $attribute, $value);
148+
149+
$this->assertSame($expected, $xmlWriter->getData());
150+
}
151+
152+
/**
153+
* @return array<array<bool|string|null>>
154+
*/
155+
public function dataProviderWriteElementIf(): array
156+
{
157+
return [
158+
[
159+
false,
160+
null,
161+
null,
162+
'',
163+
],
164+
[
165+
true,
166+
null,
167+
null,
168+
'<element/>' . chr(10),
169+
],
170+
[
171+
true,
172+
'attribute',
173+
null,
174+
'<element attribute=""/>' . chr(10),
175+
],
176+
[
177+
true,
178+
null,
179+
'value',
180+
'<element>value</element>' . chr(10),
181+
],
182+
[
183+
true,
184+
'attribute',
185+
'value',
186+
'<element attribute="value"/>' . chr(10),
187+
],
188+
];
189+
}
76190
}

0 commit comments

Comments
 (0)