| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class TestPlugins |
| 4 |
* |
| 5 |
* @package micropackage/requirements |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Micropackage\Requirements\Test\Checker; |
| 9 |
|
| 10 |
use phpmock\Mock; |
| 11 |
use phpmock\MockBuilder; |
| 12 |
use Micropackage\Requirements\Requirements; |
| 13 |
use Micropackage\Requirements\Checker\Plugins as TestedChecker; |
| 14 |
|
| 15 |
/** |
| 16 |
* Plugins checker test case. |
| 17 |
*/ |
| 18 |
class TestPlugins extends \WP_UnitTestCase { |
| 19 |
|
| 20 |
use \phpmock\phpunit\PHPMock; |
| 21 |
|
| 22 |
public function setUp() { |
| 23 |
parent::setUp(); |
| 24 |
$this->checker = new TestedChecker(); |
| 25 |
} |
| 26 |
|
| 27 |
public function tearDown() { |
| 28 |
parent::tearDown(); |
| 29 |
Mock::disableAll(); |
| 30 |
} |
| 31 |
|
| 32 |
public function bad_params() { |
| 33 |
return [ |
| 34 |
[ '5.3' ], |
| 35 |
[ 1 ], |
| 36 |
[ true ], |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
public function test_get_name_should_return_valid_name() { |
| 41 |
$this->assertSame( 'plugins', $this->checker->get_name() ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @dataProvider bad_params |
| 46 |
* @expectedException Exception |
| 47 |
*/ |
| 48 |
public function test_check_should_throw_exception_if_passed_not_array( $param ) { |
| 49 |
$this->checker->check( $param ); |
| 50 |
} |
| 51 |
|
| 52 |
public function test_check_should_pass_if_one_required_plugin_is_active() { |
| 53 |
|
| 54 |
$wp_get_active_and_valid_plugins = $this->getFunctionMock( 'Micropackage\Requirements\Checker', 'wp_get_active_and_valid_plugins' ); |
| 55 |
$wp_get_active_and_valid_plugins->expects( $this->once() )->willReturn( [ |
| 56 |
WP_PLUGIN_DIR . '/plugin-one/plugin-one.php', |
| 57 |
WP_PLUGIN_DIR . '/plugin-two/plugin-two.php', |
| 58 |
] ); |
| 59 |
|
| 60 |
$this->checker->check( [ |
| 61 |
[ 'file' => 'plugin-one/plugin-one.php', 'name' => 'Plugin One' ], |
| 62 |
] ); |
| 63 |
|
| 64 |
$this->assertEmpty( $this->checker->get_errors() ); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
public function test_check_should_pass_if_all_required_plugins_are_active() { |
| 69 |
|
| 70 |
$wp_get_active_and_valid_plugins = $this->getFunctionMock( 'Micropackage\Requirements\Checker', 'wp_get_active_and_valid_plugins' ); |
| 71 |
$wp_get_active_and_valid_plugins->expects( $this->once() )->willReturn( [ |
| 72 |
WP_PLUGIN_DIR . '/plugin-one/plugin-one.php', |
| 73 |
WP_PLUGIN_DIR . '/plugin-two/plugin-two.php', |
| 74 |
] ); |
| 75 |
|
| 76 |
$this->checker->check( [ |
| 77 |
[ 'file' => 'plugin-one/plugin-one.php', 'name' => 'Plugin One' ], |
| 78 |
[ 'file' => 'plugin-two/plugin-two.php', 'name' => 'Plugin Two' ], |
| 79 |
] ); |
| 80 |
|
| 81 |
$this->assertEmpty( $this->checker->get_errors() ); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
public function test_check_should_fail_if_at_least_one_required_plugin_is_not_active() { |
| 86 |
|
| 87 |
$wp_get_active_and_valid_plugins = $this->getFunctionMock( 'Micropackage\Requirements\Checker', 'wp_get_active_and_valid_plugins' ); |
| 88 |
$wp_get_active_and_valid_plugins->expects( $this->once() )->willReturn( [ |
| 89 |
WP_PLUGIN_DIR . '/plugin-one/plugin-one.php', |
| 90 |
] ); |
| 91 |
|
| 92 |
$this->checker->check( [ |
| 93 |
[ 'file' => 'plugin-two/plugin-two.php', 'name' => 'Plugin Two' ], |
| 94 |
] ); |
| 95 |
|
| 96 |
$errors = $this->checker->get_errors(); |
| 97 |
|
| 98 |
$this->assertNotEmpty( $errors ); |
| 99 |
$this->assertCount( 1, $errors ); |
| 100 |
$this->assertContains( 'Plugin Two', $errors[0] ); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
public function test_check_should_fail_if_two_required_plugins_are_not_active() { |
| 105 |
|
| 106 |
$wp_get_active_and_valid_plugins = $this->getFunctionMock( 'Micropackage\Requirements\Checker', 'wp_get_active_and_valid_plugins' ); |
| 107 |
$wp_get_active_and_valid_plugins->expects( $this->once() )->willReturn( [] ); |
| 108 |
|
| 109 |
$this->checker->check( [ |
| 110 |
[ 'file' => 'plugin-one/plugin-one.php', 'name' => 'Plugin One' ], |
| 111 |
[ 'file' => 'plugin-two/plugin-two.php', 'name' => 'Plugin Two' ], |
| 112 |
] ); |
| 113 |
|
| 114 |
$errors = $this->checker->get_errors(); |
| 115 |
|
| 116 |
$this->assertNotEmpty( $errors ); |
| 117 |
$this->assertCount( 2, $errors ); |
| 118 |
$this->assertContains( 'Plugin One', $errors[0] ); |
| 119 |
$this->assertContains( 'Plugin Two', $errors[1] ); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|