Skip to content

Commit

Permalink
Improved Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Jul 21, 2021
1 parent 751b70e commit 84f4947
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 18 deletions.
11 changes: 5 additions & 6 deletions src/Common/File.php
Expand Up @@ -17,9 +17,8 @@

namespace PhpOffice\Common;

/**
* Drawing
*/
use ZipArchive;

class File
{
/**
Expand All @@ -29,7 +28,7 @@ class File
*
* @return bool
*/
public static function fileExists($pFilename)
public static function fileExists(string $pFilename): bool
{
// Sick construction, but it seems that
// file_exists returns strange values when
Expand All @@ -39,7 +38,7 @@ public static function fileExists($pFilename)
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);

$zip = new \ZipArchive();
$zip = new ZipArchive();
if ($zip->open($zipFile) === true) {
$returnValue = ($zip->getFromName($archiveFile) !== false);
$zip->close();
Expand Down Expand Up @@ -71,7 +70,7 @@ public static function fileGetContents(string $pFilename): ?string
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6);
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);

$zip = new \ZipArchive();
$zip = new ZipArchive();
if ($zip->open($zipFile) === true) {
$returnValue = $zip->getFromName($archiveFile);
$zip->close();
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Text.php
Expand Up @@ -140,9 +140,9 @@ public static function isUTF8(string $value = ''): bool
*
* @param string|null $value
*
* @return string
* @return string|null
*/
public static function toUTF8(?string $value = ''): string
public static function toUTF8(?string $value = ''): ?string
{
if (!is_null($value) && !self::isUTF8($value)) {
$value = utf8_encode($value);
Expand Down
10 changes: 5 additions & 5 deletions src/Common/XMLWriter.php
Expand Up @@ -138,14 +138,14 @@ public function writeElementBlock(string $element, $attributes, string $value =
*
* @param bool $condition
* @param string $element
* @param string $attribute
* @param string|null $attribute
* @param mixed $value
*
* @return void
*/
public function writeElementIf($condition, $element, $attribute = null, $value = null)
public function writeElementIf(bool $condition, string $element, ?string $attribute = null, $value = null)
{
if ($condition == true) {
if ($condition) {
if (is_null($attribute)) {
$this->writeElement($element, $value);
} else {
Expand All @@ -165,9 +165,9 @@ public function writeElementIf($condition, $element, $attribute = null, $value =
*
* @return void
*/
public function writeAttributeIf($condition, $attribute, $value)
public function writeAttributeIf(bool $condition, string $attribute, $value)
{
if ($condition == true) {
if ($condition) {
$this->writeAttribute($attribute, $value);
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Common/Tests/FileTest.php
Expand Up @@ -34,6 +34,13 @@ public function testFileExists(): void
$this->assertTrue(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#[Content_Types].xml'));
$this->assertFalse(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#404.xml'));
$this->assertFalse(File::fileExists('zip://' . $pathResources . 'files' . DIRECTORY_SEPARATOR . '404.pptx#404.xml'));

// Set a ZIP en ReadOnly Mode
$zipTest = tempnam(sys_get_temp_dir(), 'PhpOfficeCommon');
copy($pathResources . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx', $zipTest);
chmod($zipTest, 333);
$this->assertFalse(File::fileExists('zip://' . $zipTest));
unlink($zipTest);
}

public function testGetFileContents(): void
Expand Down
20 changes: 15 additions & 5 deletions tests/Common/Tests/TextTest.php
Expand Up @@ -27,15 +27,21 @@
*/
class TextTest extends TestCase
{
public function testControlCharacters(): void
public function testControlCharactersPHP2OOXML(): void
{
$this->assertEquals('', Text::controlCharacterPHP2OOXML());
$this->assertEquals('aeiou', Text::controlCharacterPHP2OOXML('aeiou'));
$this->assertEquals('àéîöù', Text::controlCharacterPHP2OOXML('àéîöù'));

$value = rand(0, 8);
$this->assertEquals('_x' . sprintf('%04s', strtoupper(dechex($value))) . '_', Text::controlCharacterPHP2OOXML(chr($value)));
$this->assertEquals(
'_x' . sprintf('%04s', strtoupper(dechex($value))) . '_',
Text::controlCharacterPHP2OOXML(chr($value))
);
}

public function testControlCharactersOOXML2PHP(): void
{
$this->assertEquals('', Text::controlCharacterOOXML2PHP(''));
$this->assertEquals(chr(0x08), Text::controlCharacterOOXML2PHP('_x0008_'));
}
Expand All @@ -60,16 +66,20 @@ public function testChr(): void
$this->assertEquals('', Text::chr(2097152));
}

/**
* Is UTF8
*/
public function testIsUTF8(): void
{
$this->assertTrue(Text::isUTF8(''));
$this->assertTrue(Text::isUTF8('éééé'));
$this->assertFalse(Text::isUTF8(utf8_decode('éééé')));
}

public function testToUtf8(): void
{
$this->assertNull(Text::toUTF8(null));
$this->assertEquals('eeee', Text::toUTF8('eeee'));
$this->assertEquals('éééé', Text::toUTF8('éééé'));
}

/**
* Test unicode conversion
*/
Expand Down
114 changes: 114 additions & 0 deletions tests/Common/Tests/XMLWriterTest.php
Expand Up @@ -43,6 +43,30 @@ public function testConstruct(): void
$this->assertEquals('<element>BBB</element>' . chr(10), $object->getData());
}

public function testConstructCompatibility(): void
{
$object = new XMLWriter(XMLWriter::STORAGE_MEMORY, null, false);
$object->startElement('element');
$object->startElement('sub');
$object->text('CCC');
$object->endElement();
$object->endElement();
$this->assertEquals(
'<element>' . PHP_EOL . ' <sub>CCC</sub>' . PHP_EOL . '</element>' . PHP_EOL,
$object->getData()
);
$object = new XMLWriter(XMLWriter::STORAGE_MEMORY, null, true);
$object->startElement('element');
$object->startElement('sub');
$object->text('CCC');
$object->endElement();
$object->endElement();
$this->assertEquals(
'<element><sub>CCC</sub></element>',
$object->getData()
);
}

public function testWriteAttribute(): void
{
$xmlWriter = new XMLWriter();
Expand All @@ -53,6 +77,23 @@ public function testWriteAttribute(): void
$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
}

public function testWriteAttributeIf(): void
{
$xmlWriter = new XMLWriter();
$xmlWriter->startElement('element');
$xmlWriter->writeAttributeIf(true, 'name', 'value');
$xmlWriter->endElement();

$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());

$xmlWriter = new XMLWriter();
$xmlWriter->startElement('element');
$xmlWriter->writeAttributeIf(false, 'name', 'value');
$xmlWriter->endElement();

$this->assertSame('<element/>' . chr(10), $xmlWriter->getData());
}

public function testWriteAttributeShouldWriteFloatValueLocaleIndependent(): void
{
$value = 1.2;
Expand All @@ -73,4 +114,77 @@ public function testWriteAttributeShouldWriteFloatValueLocaleIndependent(): void
}
$this->assertSame('<element name="1.2"/>' . chr(10), $xmlWriter->getData());
}

public function testWriteElementBlock(): void
{
$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', 'name');

$this->assertSame('<element name=""/>' . chr(10), $xmlWriter->getData());

$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', 'name', 'value');

$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());

$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', ['name' => 'value']);

$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());

$xmlWriter = new XMLWriter();
$xmlWriter->writeElementBlock('element', ['name' => 'value'], 'value2');

$this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
}

/**
* @dataProvider dataProviderWriteElementIf
*/
public function testWriteElementIf(bool $condition, ?string $attribute, ?string $value, string $expected): void
{
$xmlWriter = new XMLWriter();
$xmlWriter->writeElementIf($condition, 'element', $attribute, $value);

$this->assertSame($expected, $xmlWriter->getData());
}

/**
* @return array<array<bool|string|null>>
*/
public function dataProviderWriteElementIf(): array
{
return [
[
false,
null,
null,
'',
],
[
true,
null,
null,
'<element/>' . chr(10),
],
[
true,
'attribute',
null,
'<element attribute=""/>' . chr(10),
],
[
true,
null,
'value',
'<element>value</element>' . chr(10),
],
[
true,
'attribute',
'value',
'<element attribute="value"/>' . chr(10),
],
];
}
}

0 comments on commit 84f4947

Please sign in to comment.