Passed
Push — master ( 08f45f...aa2439 )
by Der Mundschenk
01:50
created

Mundschenk_WP_Requirements_Test::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 *  This file is part of mundschenk-at/check-wp-requirements.
4
 *
5
 *  Copyright 2017-2018 Peter Putzer.
6
 *
7
 *  This program is free software; you can redistribute it and/or
8
 *  modify it under the terms of the GNU General Public License
9
 *  as published by the Free Software Foundation; either version 2
10
 *  of the License, or ( at your option ) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 *
21
 *  @package mundschenk-at/check-wp-requirements/tests
22
 *  @license http://www.gnu.org/licenses/gpl-2.0.html
23
 */
24
25
namespace Mundschenk\Check_WordPress_Requirements\Tests;
26
27
use Brain\Monkey\Actions;
28
use Brain\Monkey\Filters;
29
use Brain\Monkey\Functions;
30
31
use org\bovigo\vfs\vfsStream;
32
33
use Mockery as m;
34
35
/**
36
 * Mundschenk_WP_Requirements unit test.
37
 *
38
 * @coversDefaultClass \Mundschenk_WP_Requirements
39
 * @usesDefaultClass \Mundschenk_WP_Requirements
40
 *
41
 * @uses Mundschenk_WP_Requirements::__construct
42
 */
43
class Mundschenk_WP_Requirements_Test extends TestCase {
44
45
	/**
46
	 * Test fixture.
47
	 *
48
	 * @var \Mundschenk_WP_Requirements
49
	 */
50
	protected $req;
51
52
	/**
53
	 * Sets up the fixture, for example, opens a network connection.
54
	 * This method is called before a test is executed.
55
	 */
56
	protected function setUp() { // @codingStandardsIgnoreLine
57
58
		// Set up virtual filesystem.
59
		vfsStream::setup( 'root', null, [
60
			'vendor' => [
61
				'partials' => [
62
					'requirements-error-notice.php' => 'REQUIREMENTS_ERROR',
63
				],
64
			],
65
		] );
66
		set_include_path( 'vfs://root/' ); // @codingStandardsIgnoreLine
67
68
		Functions\expect( 'wp_parse_args' )->once()->andReturnUsing( function( $array, $defaults ) {
69
			return \array_merge( $defaults, $array );
70
		} );
71
72
		$this->req = m::mock( \Mundschenk_WP_Requirements::class, [
73
			'Foobar',
74
			'plugin/plugin.php',
75
			'textdomain',
76
			[
77
				'php'       => '5.6.0',
78
				'multibyte' => true,
79
				'utf-8'     => true,
80
			],
81
		] )->shouldAllowMockingProtectedMethods()->makePartial();
82
83
		parent::setUp();
84
	}
85
86
	/**
87
	 * Necesssary clean-up work.
88
	 */
89
	protected function tearDown() { // @codingStandardsIgnoreLine
90
		parent::tearDown();
91
	}
92
93
94
95
	/**
96
	 * Test constructor.
97
	 *
98
	 * @covers ::__construct
99
	 */
100
	public function test_constructor() {
101
		Functions\expect( 'wp_parse_args' )->once()->andReturnUsing( function( $array, $defaults ) {
102
			return \array_merge( $defaults, $array );
103
		} );
104
105
		$req = m::mock( \Mundschenk_WP_Requirements::class, [ 'Foobar', 'plugin/plugin.php', 'textdomain', [ 'php' => '5.3.5' ] ] );
106
107
		$this->assertAttributeSame( 'plugin/plugin.php', 'plugin_file', $req );
108
		$this->assertAttributeSame( 'Foobar', 'plugin_name', $req );
109
		$this->assertAttributeSame( 'textdomain', 'textdomain', $req );
110
111
		$requirements = $this->getValue( $req, 'install_requirements', \Mundschenk_WP_Requirements::class );
112
		$this->assertArrayHasKey( 'php', $requirements );
113
		$this->assertArrayHasKey( 'multibyte', $requirements );
114
		$this->assertArrayHasKey( 'utf-8', $requirements );
115
116
		$this->assertEquals( '5.3.5', $requirements['php'] );
117
		$this->assertFalse( $requirements['multibyte'] );
118
		$this->assertFalse( $requirements['utf-8'] );
119
	}
120
121
	/**
122
	 * Test display_error_notice.
123
	 *
124
	 * @covers ::display_error_notice
125
	 */
126
	public function test_display_error_notice() {
127
		// Mock dirname( __FILE__ ).
128
		$this->setValue( $this->req, 'base_dir', 'vendor', \Mundschenk_WP_Requirements::class );
129
130
		$this->expectOutputString( 'REQUIREMENTS_ERROR' );
131
		$this->invokeMethod( $this->req, 'display_error_notice', [ 'foo' ] );
132
	}
133
134
	/**
135
	 * Test display_error_notice.
136
	 *
137
	 * @covers ::display_error_notice
138
	 *
139
	 * @expectedExceptionMessage Too few arguments to function
140
	 */
141
	public function test_display_error_notice_no_arguments() {
142
		$this->expectOutputString( '' );
143
144
		// PHP < 7.0 raises an error instead of throwing an "exception".
145
		if ( version_compare( phpversion(), '7.0.0', '<' ) ) {
146
			$this->expectException( \PHPUnit_Framework_Error::class );
147
		} elseif ( version_compare( phpversion(), '7.1.0', '<' ) ) {
148
			$this->expectException( \PHPUnit\Framework\Error\Warning::class );
149
		} else {
150
			$this->expectException( \ArgumentCountError::class );
151
		}
152
153
		$this->invokeMethod( $this->req, 'display_error_notice', [] );
154
	}
155
156
	/**
157
	 * Test display_error_notice.
158
	 *
159
	 * @covers ::display_error_notice
160
	 */
161
	public function test_display_error_notice_empty_format() {
162
		$this->expectOutputString( '' );
163
164
		$this->invokeMethod( $this->req, 'display_error_notice', [ '' ] );
165
	}
166
167
	/**
168
	 * Test admin_notices_php_version_incompatible.
169
	 *
170
	 * @covers ::admin_notices_php_version_incompatible
171
	 */
172
	public function test_admin_notices_php_version_incompatible() {
173
		Functions\expect( '__' )->with( m::type( 'string' ), 'textdomain' )->atLeast()->once()->andReturn( 'translated' );
174
		$this->req->shouldReceive( 'display_error_notice' )->once();
175
176
		$this->assertNull( $this->req->admin_notices_php_version_incompatible() );
177
	}
178
179
	/**
180
	 * Test admin_notices_mbstring_incompatible.
181
	 *
182
	 * @covers ::admin_notices_mbstring_incompatible
183
	 */
184
	public function test_admin_notices_mbstring_incompatible() {
185
		Functions\expect( '__' )->with( m::type( 'string' ), 'textdomain' )->atLeast()->once()->andReturn( 'translated' );
186
		$this->req->shouldReceive( 'display_error_notice' )->once();
187
188
		$this->assertNull( $this->req->admin_notices_mbstring_incompatible() );
189
	}
190
191
	/**
192
	 * Test admin_notices_charset_incompatible.
193
	 *
194
	 * @covers ::admin_notices_charset_incompatible
195
	 */
196
	public function test_admin_notices_charset_incompatible() {
197
		Functions\expect( '__' )->with( m::type( 'string' ), 'textdomain' )->atLeast()->once()->andReturn( 'translated' );
198
		Functions\expect( 'get_bloginfo' )->with( 'charset' )->once()->andReturn( '8859-1' );
199
		$this->req->shouldReceive( 'display_error_notice' )->once();
200
201
		$this->assertNull( $this->req->admin_notices_charset_incompatible() );
202
	}
203
204
	/**
205
	 * Test check_php_support.
206
	 *
207
	 * @covers ::check_php_support
208
	 */
209
	public function test_check_php_support() {
210
		// Fake PHP version check.
211
		$this->setValue( $this->req, 'install_requirements', [ 'php' => '999.0.0' ], \Mundschenk_WP_Requirements::class );
212
		$this->assertFalse( $this->invokeMethod( $this->req, 'check_php_support' ) );
213
214
		$this->setValue( $this->req, 'install_requirements', [ 'php' => PHP_VERSION ], \Mundschenk_WP_Requirements::class );
215
		$this->assertTrue( $this->invokeMethod( $this->req, 'check_php_support' ) );
216
	}
217
218
	/**
219
	 * Provides data for testing check_utf8_support.
220
	 *
221
	 * @return array
222
	 */
223
	public function provide_check_utf8_support_data() {
224
		return [
225
			[ 'utf-8', true ],
226
			[ 'UTF-8', true ],
227
			[ '8859-1', false ],
228
			[ 'foobar', false ],
229
		];
230
	}
231
232
	/**
233
	 * Test check_utf8_support.
234
	 *
235
	 * @covers ::check_utf8_support
236
	 *
237
	 * @dataProvider provide_check_utf8_support_data
238
	 *
239
	 * @param string $charset  The blog charset.
240
	 * @param bool   $expected The expected result.
241
	 */
242
	public function test_check_utf8_support( $charset, $expected ) {
243
		Functions\expect( 'get_bloginfo' )->with( 'charset' )->once()->andReturn( $charset );
244
245
		$this->assertSame( $expected, $this->invokeMethod( $this->req, 'check_utf8_support' ) );
246
	}
247
248
	/**
249
	 * Test check_utf8_support.
250
	 *
251
	 * @covers ::check_multibyte_support
252
	 */
253
	public function test_check_multibyte_support() {
254
		// This will be true because mbstring is a requirement for running the test suite.
255
		$this->assertTrue( $this->invokeMethod( $this->req, 'check_multibyte_support' ) );
256
	}
257
258
	/**
259
	 * Provides data for testing check.
260
	 *
261
	 * @return array
262
	 */
263
	public function provide_check_data() {
264
		return [
265
			[ true, true, true, true, true ],
266
			[ false, false, false, true, false ],
267
			[ true, false, false, true, false ],
268
			[ false, true, false, true, false ],
269
			[ false, false, true, true, false ],
270
			[ true, true, true, false, true ],
271
			[ false, false, false, false, false ],
272
			[ true, false, false, false, false ],
273
			[ false, true, false, false, false ],
274
			[ false, false, true, false, false ],
275
			[ true, true, false, true, false ],
276
		];
277
	}
278
279
	/**
280
	 * Test check.
281
	 *
282
	 * @covers ::check
283
	 * @covers ::get_requirements
284
	 *
285
	 * @dataProvider provide_check_data
286
	 *
287
	 * @param  bool $php_version PHP version check flag.
288
	 * @param  bool $multibyte   Multibyte support check flag.
289
	 * @param  bool $charset     Charset check flag.
290
	 * @param  bool $admin       Result of is_admin().
291
	 * @param  bool $expected    Expected result.
292
	 */
293
	public function test_check( $php_version, $multibyte, $charset, $admin, $expected ) {
294
		Functions\expect( 'is_admin' )->zeroOrMoreTimes()->andReturn( $admin );
295
296
		$this->req->shouldReceive( 'check_php_support' )->once()->andReturn( $php_version );
297
		$this->req->shouldReceive( 'check_multibyte_support' )->times( (int) $php_version )->andReturn( $multibyte );
298
		$this->req->shouldReceive( 'check_utf8_support' )->times( (int) ( $php_version && $multibyte ) )->andReturn( $charset );
299
300
		if ( $admin ) {
301
			$php_times       = $php_version ? 0 : 1;
302
			$multibyte_times = ! $php_version || $multibyte ? 0 : 1;
303
			$charset_times   = ! $php_version || ! $multibyte || $charset ? 0 : 1;
304
305
			if ( ! $expected ) {
306
				Functions\expect( 'load_plugin_textdomain' )->once()->with( 'textdomain' );
307
			}
308
309
			Actions\expectAdded( 'admin_notices' )->with( [ $this->req, 'admin_notices_php_version_incompatible' ] )->times( $php_times );
310
			Actions\expectAdded( 'admin_notices' )->with( [ $this->req, 'admin_notices_mbstring_incompatible' ] )->times( $multibyte_times );
311
			Actions\expectAdded( 'admin_notices' )->with( [ $this->req, 'admin_notices_charset_incompatible' ] )->times( $charset_times );
312
		}
313
314
		$this->assertSame( $expected, $this->invokeMethod( $this->req, 'check' ) );
315
	}
316
317
	/**
318
	 * Test deactivate_plugin.
319
	 *
320
	 * @covers ::deactivate_plugin
321
	 */
322
	public function test_deactivate_plugin() {
323
		Functions\expect( 'plugin_basename' )->with( 'plugin/plugin.php' )->once()->andReturn( 'plugin' );
324
		Functions\expect( 'deactivate_plugins' )->with( 'plugin' )->once();
325
326
		$this->assertNull( $this->req->deactivate_plugin() );
327
	}
328
}
329