Skip to content

Commit

Permalink
Write a test for the OpenDocument class
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Mar 5, 2022
1 parent a4417e0 commit ed133f2
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/classes/OpenDocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Tests;

use DateTime;
use PhpMyAdmin\OpenDocument;
use PhpMyAdmin\ZipExtension;
use ZipArchive;
use function tempnam;
use function file_put_contents;
use function unlink;

/**
* @requires extension zip
*/
class OpenDocumentTest extends AbstractTestCase
{
public function testCreateDocument(): void
{
parent::defineVersionConstants();
$document = OpenDocument::create(
'application/vnd.oasis.opendocument.text',
'<data>'
);
$this->assertNotFalse($document);

$tmpFile = (string) tempnam('./', 'open-document-test');
$this->assertNotFalse(file_put_contents($tmpFile, $document), 'The temp file should be written');

$zipExtension = new ZipExtension(new ZipArchive());
$this->assertSame([
'error' => '',
'data' => 'application/vnd.oasis.opendocument.text',
], $zipExtension->getContents($tmpFile));

$this->assertSame([
'error' => '',
'data' => '<data>',
], $zipExtension->getContents($tmpFile, '/content\.xml/'));

$dateTimeCreation = (new DateTime())->format('Y-m-d\TH:i');
$this->assertStringContainsString(
// Do not use a full version or seconds could be out of sync and cause flaky test failures
'<meta:creation-date>' . $dateTimeCreation,
$zipExtension->getContents($tmpFile, '/meta\.xml/')['data']
);

$this->assertSame(5, $zipExtension->getNumberOfFiles($tmpFile));
$this->assertTrue(unlink($tmpFile));
}
}

0 comments on commit ed133f2

Please sign in to comment.