Pull merge.
[yaffs-website] / vendor / webflo / drupal-finder / tests / DrupalFinderTestBase.php
1 <?php
2
3 namespace DrupalFinder\Tests;
4
5 use DrupalFinder\DrupalFinder;
6 use Exception;
7 use PHPUnit_Framework_TestCase;
8
9 abstract class DrupalFinderTestBase extends PHPUnit_Framework_TestCase
10 {
11     /**
12      * @var \DrupalFinder\DrupalFinder
13      */
14     protected $finder;
15
16     protected function setUp()
17     {
18         parent::setUp();
19         $this->finder = new DrupalFinder();
20     }
21
22     protected function dumpToFileSystem($fileStructure, $root)
23     {
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);
29             } else {
30                 file_put_contents($root . '/' . $name, $content);
31             }
32         }
33     }
34
35     protected function prepareFileStructure($fileStructure)
36     {
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);
42             }
43         }
44         return $fileStructure;
45     }
46
47     protected function tempdir($dir, $prefix = '', $mode = 0700)
48     {
49         if (substr($dir, -1) != '/') {
50             $dir .= '/';
51         }
52         do {
53             $path = $dir . $prefix . mt_rand(0, 9999999);
54         } while (!mkdir($path, $mode));
55         register_shutdown_function(
56             [get_called_class(), 'tempdir_remove'],
57             $path
58         );
59
60         return realpath($path);
61     }
62
63     public static function tempdir_remove($path)
64     {
65         if (is_link($path)) {
66             if (defined('PHP_WINDOWS_VERSION_BUILD')) {
67                 rmdir($path);
68             } else {
69                 unlink($path);
70             }
71
72             return;
73         }
74
75         foreach (scandir($path) as $child) {
76             if (in_array($child, ['.', '..'])) {
77                 continue;
78             }
79             $child = "$path/$child";
80             is_dir($child) ? static::tempdir_remove($child) : unlink($child);
81         }
82         rmdir($path);
83     }
84
85     /**
86      * @param $target
87      * @param $link
88      *
89      * @throws \PHPUnit_Framework_SkippedTestError
90      */
91     protected function symlink($target, $link)
92     {
93         try {
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)
98             ) {
99                 $this->markTestSkipped(<<<'MESSAGE'
100 No privilege to create symlinks. Run test as Administrator (elevated process).
101 MESSAGE
102                 );
103             }
104             throw $e;
105         }
106     }
107 }
108
109 define('WIN_ERROR_PRIVILEGE_NOT_HELD', '1314');