Skip to content

Commit

Permalink
Add tmpdir helper
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlmoon committed Sep 5, 2024
1 parent d8c391c commit 2ae10b0
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 66 deletions.
28 changes: 28 additions & 0 deletions src/TmpDir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace DealNews\TestHelpers;

/**
* Creates a temporary directory with a random name and returns it
*
* @author Brian Moon <[email protected]>
* @copyright 1997-Present DealNews.com, Inc
* @package DealNews\TestHelpers
*/
trait TmpDir {

public function tmpDir(?string $base_dir = null): string {

$base_dir ??= sys_get_temp_dir();

do {
$success = false;
$dir = $base_dir . '/' . base64_encode(random_bytes(32));
if (!file_exists($dir)) {
$success = mkdir($dir);
}
} while (!$success);

return $dir;
}
}
25 changes: 0 additions & 25 deletions tests.old/bootstrap.php

This file was deleted.

3 changes: 0 additions & 3 deletions tests.old/etc/config.d/01-config.ini

This file was deleted.

2 changes: 0 additions & 2 deletions tests.old/etc/config.d/02-config.env

This file was deleted.

4 changes: 0 additions & 4 deletions tests.old/etc/config.d/03-config.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions tests.old/etc/config.d/04-config.json

This file was deleted.

3 changes: 0 additions & 3 deletions tests.old/etc/config.env

This file was deleted.

4 changes: 0 additions & 4 deletions tests.old/etc/config.ini

This file was deleted.

5 changes: 0 additions & 5 deletions tests.old/etc/config.json

This file was deleted.

4 changes: 0 additions & 4 deletions tests.old/etc/config.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions tests.old/etc/config_env_file.ini

This file was deleted.

3 changes: 0 additions & 3 deletions tests.old/etc/get_config_bad.ini

This file was deleted.

1 change: 0 additions & 1 deletion tests.old/etc/get_config_bad.json

This file was deleted.

1 change: 0 additions & 1 deletion tests.old/etc/get_config_bad.txt

This file was deleted.

1 change: 0 additions & 1 deletion tests.old/etc/get_config_bad.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions tests.old/etc/get_config_test.ini

This file was deleted.

23 changes: 23 additions & 0 deletions tests/TmpDirTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace DealNews\TestHelpers\Tests;

use \DealNews\TestHelpers\TmpDir;

class TmpDirTest extends \PHPUnit\Framework\TestCase {

use TmpDir;

public function testTmpDir() {
$dir = $this->tmpDir();

$this->assertTrue(is_dir($dir));

$dir2 = $this->tmpDir($dir);

$this->assertTrue(is_dir($dir));

rmdir($dir2);
rmdir($dir);
}
}

0 comments on commit 2ae10b0

Please sign in to comment.