3 namespace DrupalFinder\Tests;
5 use DrupalFinder\DrupalFinder;
7 use PHPUnit_Framework_TestCase;
9 abstract class DrupalFinderTestBase extends PHPUnit_Framework_TestCase
12 * @var \DrupalFinder\DrupalFinder
16 protected function setUp()
19 $this->finder = new DrupalFinder();
22 protected function dumpToFileSystem($fileStructure, $root)
24 $fileStructure = $this->prepareFileStructure($fileStructure);
25 foreach ($fileStructure as $name => $content) {
26 if (is_array($content)) {
27 mkdir($root . '/' . $name);
28 $this->dumpToFileSystem($content, $root . '/' . $name);
30 file_put_contents($root . '/' . $name, $content);
35 protected function prepareFileStructure($fileStructure)
37 foreach ($fileStructure as $name => $content) {
38 if (($name === 'composer.json' || $name === 'composer.lock') && is_array($content)) {
39 $fileStructure[$name] = json_encode($content, JSON_UNESCAPED_SLASHES);
40 } elseif (is_array($content)) {
41 $fileStructure[$name] = $this->prepareFileStructure($content);
44 return $fileStructure;
47 protected function tempdir($dir, $prefix = '', $mode = 0700)
49 if (substr($dir, -1) != '/') {
53 $path = $dir . $prefix . mt_rand(0, 9999999);
54 } while (!mkdir($path, $mode));
55 register_shutdown_function(
56 [get_called_class(), 'tempdir_remove'],
60 return realpath($path);
63 public static function tempdir_remove($path)
66 if (defined('PHP_WINDOWS_VERSION_BUILD')) {
75 foreach (scandir($path) as $child) {
76 if (in_array($child, ['.', '..'])) {
79 $child = "$path/$child";
80 is_dir($child) ? static::tempdir_remove($child) : unlink($child);
89 * @throws \PHPUnit_Framework_SkippedTestError
91 protected function symlink($target, $link)
94 return symlink($target, $link);
95 } catch (Exception $e) {
96 if (defined('PHP_WINDOWS_VERSION_BUILD')
97 && strstr($e->getMessage(), WIN_ERROR_PRIVILEGE_NOT_HELD)
99 $this->markTestSkipped(<<<'MESSAGE'
100 No privilege to create symlinks. Run test as Administrator (elevated process).
109 define('WIN_ERROR_PRIVILEGE_NOT_HELD', '1314');