Skip to content

Commit

Permalink
Fix #629 JUnit log, create directory if not existent (#630)
Browse files Browse the repository at this point in the history
Co-authored-by: marek <m.necesany@choosit.com>
Co-authored-by: Filippo Tessarotto <zoeslam@gmail.com>
  • Loading branch information
3 people committed Dec 2, 2021
1 parent c32a5c4 commit 4c0ce5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Logging/JUnit/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

use function assert;
use function count;
use function dirname;
use function file_put_contents;
use function get_object_vars;
use function htmlspecialchars;
use function is_dir;
use function is_float;
use function is_scalar;
use function mkdir;
use function preg_match;
use function sprintf;
use function str_replace;
Expand Down Expand Up @@ -96,6 +99,11 @@ public function getXml(): string
*/
public function write(string $path): void
{
$dir = dirname($path);
if (! is_dir($dir)) {
mkdir($dir, 0777, true);
}

file_put_contents($path, $this->getXml());
}

Expand Down
19 changes: 16 additions & 3 deletions test/Unit/Logging/JUnit/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,25 @@ public function testDataProviderWithSpecialCharacters(): void
static::assertXmlStringEqualsXmlString((string) file_get_contents($mixed), $xml);
}

public function testWrite(): void
public function testWriteToFile(): void
{
$output = FIXTURES . DS . 'logs' . DS . 'passing.xml';
$output = TMP_DIR . DS . 'passing.xml';
$this->addPassingReader();
$this->writer->write($output);
static::assertXmlStringEqualsXmlString((string) file_get_contents($this->passing), (string) file_get_contents($output));
static::assertXmlFileEqualsXmlFile($this->passing, $output);
if (! file_exists($output)) {
return;
}

unlink($output);
}

public function testWriteToFileInNonExistentDir(): void
{
$output = TMP_DIR . DS . 'logs' . DS . 'new' . DS . 'dir' . DS . 'passing.xml';
$this->addPassingReader();
$this->writer->write($output);
static::assertXmlFileEqualsXmlFile($this->passing, $output);
if (! file_exists($output)) {
return;
}
Expand Down

0 comments on commit 4c0ce5e

Please sign in to comment.