Completed
Push — add/changelog-tooling ( 257a85 )
by
unknown
149:15 queued 138:48
created

TestCase::resetConfigCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Base test case for the changelogger tool.
4
 *
5
 * @package automattic/jetpack-changelogger
6
 */
7
8
// phpcs:disable WordPress.WP.AlternativeFunctions, WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv, WordPress.NamingConventions.ValidVariableName
9
10
namespace Automattic\Jetpack\Changelogger\Tests;
11
12
use Automattic\Jetpack\Changelogger\Config;
13
use PHPUnit\Framework\TestCase as PHPUnit_TestCase;
14
use Wikimedia\TestingAccessWrapper;
15
use function Wikimedia\quietCall;
16
17
/**
18
 * Base test case for the changelogger tool.
19
 */
20
class TestCase extends PHPUnit_TestCase {
21
22
	/**
23
	 * Value of COMPOSER environment variable to restore in tear_down.
24
	 *
25
	 * @var string|false
26
	 */
27
	private $oldenv = false;
28
29
	/**
30
	 * Temporary directory to remove in tear_down.
31
	 *
32
	 * @var string|false
33
	 */
34
	private $tmpdir = false;
35
36
	/**
37
	 * Working directory to restore in tear_down.
38
	 *
39
	 * @var string|false
40
	 */
41
	private $oldcwd = false;
42
43
	/**
44
	 * Setup test.
45
	 *
46
	 * @before
47
	 */
48
	public function set_up() {
49
		$this->oldenv = getenv( 'COMPOSER' );
50
		$this->resetConfigCache();
51
	}
52
53
	/**
54
	 * Teardown test.
55
	 *
56
	 * @after
57
	 */
58
	public function tear_down() {
59
		$this->cleanupTempDir();
60
		$this->resetConfigCache();
61
		putenv( false === $this->oldenv ? 'COMPOSER' : "COMPOSER=$this->oldenv" );
62
	}
63
64
	/**
65
	 * Create (and chdir to) a temporary directory to test commands.
66
	 *
67
	 * @return string
68
	 * @throws \LogicException If called when already in effect.
69
	 * @throws \RuntimeException If a runtime exception occurs. Which isn't normally noted, but some sniff insists.
70
	 */
71
	protected function useTempDir() {
72
		if ( false !== $this->tmpdir ) {
73
			throw new \LogicException( 'useTempDir() called while a temp dir already exists' );
74
		}
75
76
		$base = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phpunit-changelogger-';
77
		$mask = rand( 0, 0xffffff );
78
		for ( $i = 0; $i < 0xffffff; $i++ ) {
79
			$tmpdir = $base . sprintf( '%06x', $i ^ $mask );
80
			if ( quietCall( 'mkdir', $tmpdir, 0700 ) ) {
81
				// Success!
82
				file_put_contents( "$tmpdir/composer.json", "{}\n" );
83
				$this->oldcwd = getcwd();
84
				$this->tmpdir = $tmpdir;
85
				chdir( $tmpdir );
86
				return $tmpdir;
87
			}
88
		}
89
90
		throw new \RuntimeException( 'Failed to create temporary directory' );
91
	}
92
93
	/**
94
	 * Cleanup after a call to `useTempDir()`.
95
	 *
96
	 * This is called automatically during teardown, but may be called manually
97
	 * as well.
98
	 */
99
	protected function cleanupTempDir() {
100
		if ( false === $this->tmpdir ) {
101
			return;
102
		}
103
104
		chdir( $this->oldcwd );
105
106
		$iter = new \RecursiveIteratorIterator(
107
			new \RecursiveDirectoryIterator( $this->tmpdir, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS ),
108
			\RecursiveIteratorIterator::CHILD_FIRST
109
		);
110
		foreach ( $iter as $path ) {
111
			if ( is_dir( $path ) ) {
112
				rmdir( $path );
113
			} else {
114
				unlink( $path );
115
			}
116
		}
117
		rmdir( $this->tmpdir );
118
119
		$this->oldcwd = false;
120
		$this->tmpdir = false;
121
	}
122
123
	/**
124
	 * Reset the internal caches in Config.
125
	 */
126
	protected function resetConfigCache() {
127
		$w         = TestingAccessWrapper::newFromClass( Config::class );
128
		$w->config = array();
129
		$w->cache  = array();
130
		$w->loaded = false;
131
		$w->out    = null;
132
	}
133
134
}
135