From 66dc74af41674616cc1680e2a8f63014d08e2cfa Mon Sep 17 00:00:00 2001 From: dyszczo <krzysztof@dyszczyk.pl> Date: Thu, 26 Apr 2018 12:59:33 +0200 Subject: [PATCH] requirement tests for wp, php, module, wp_plugin --- composer.json | 2 +- phpunit-unit.xml | 2 +- tests/unit/Test_Basic_Requirement_Checker.php | 111 ++++++++++++++++++ 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 tests/unit/Test_Basic_Requirement_Checker.php diff --git a/composer.json b/composer.json index 4e7a18d..95f8e78 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ }, "autoload-dev": { - "classmap": ["src"] + "classmap": ["src", "tests"] }, "scripts": { "phpcs": "phpcs", diff --git a/phpunit-unit.xml b/phpunit-unit.xml index efca695..72701bb 100644 --- a/phpunit-unit.xml +++ b/phpunit-unit.xml @@ -1,7 +1,7 @@ <phpunit bootstrap="tests/unit/bootstrap.php"> <testsuites> <testsuite> - <directory prefix="test-" suffix=".php">./tests/unit/</directory> + <directory prefix="Test_" suffix=".php">./tests/unit/</directory> </testsuite> </testsuites> diff --git a/tests/unit/Test_Basic_Requirement_Checker.php b/tests/unit/Test_Basic_Requirement_Checker.php new file mode 100644 index 0000000..b2b66e3 --- /dev/null +++ b/tests/unit/Test_Basic_Requirement_Checker.php @@ -0,0 +1,111 @@ +<?php + +class Test_Basic_Requirement_Checker extends PHPUnit\Framework\TestCase { + const RANDOM_PLUGIN_FILE = 'file'; + + const RANDOM_PLUGIN_NAME = 'name'; + + const RANDOM_PLUGIN_TEXTDOMAIN = 'text'; + + const ALWAYS_VALID_PHP_VERSION = '5.2'; + + const ALWAYS_VALID_WP_VERSION = '4.0'; + + public function setUp() { + WP_Mock::setUp(); + + WP_Mock::wpFunction( 'get_bloginfo' ) + ->andReturn( self::ALWAYS_VALID_WP_VERSION ); + } + + public function tearDown() { + WP_Mock::tearDown(); + } + + /** + * @param string $php + * @param string $wp + * + * @return WPDesk_Basic_Requirement_Checker + */ + public function create_requirements_for_php_wp( $php, $wp ) { + return new WPDesk_Basic_Requirement_Checker( self::RANDOM_PLUGIN_FILE, self::RANDOM_PLUGIN_NAME, + self::RANDOM_PLUGIN_TEXTDOMAIN, $php, $wp ); + } + + public function test_php_version_check() { + $known_PHP_versions = [ '7.3', '7.2', '7.1', '7.0', '5.6', '5.5', '5.4', '5.3', '5.2' ]; + + $requirements = $this->create_requirements_for_php_wp( + self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + foreach ( $known_PHP_versions as $version ) { + $requirements->set_min_php_require( $version ); + if ( version_compare( PHP_VERSION, $version, '>=' ) ) { + $this->assertTrue( $requirements->are_requirements_met(), + 'Should be ok because WP is OK and PHP is OK' ); + } else { + $this->assertFalse( $requirements->are_requirements_met(), + 'Should fail because required PHP should be at least ' . $version ); + } + + } + } + + public function test_wp_version_check() { + $wp_version_fail = '4.1'; + + $requirements = $this->create_requirements_for_php_wp( + self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + $this->assertTrue( $requirements->are_requirements_met(), 'Should be ok because WP is OK and PHP is OK' ); + $requirements->set_min_wp_require( $wp_version_fail ); + $this->assertFalse( $requirements->are_requirements_met(), + 'Should fail because required WP should be at least ' . $wp_version_fail ); + } + + /** + * @requires extension curl + */ + public function test_module_check() { + $requirements = $this->create_requirements_for_php_wp( + self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + $requirements->add_php_module_require( 'curl' ); + $this->assertTrue( $requirements->are_requirements_met(), 'Curl should exists' ); + } + + public function test_plugin_check_with_multisite() { + $multisite = true; + $exising_plugin_name = 'WooCommerce'; + $exising_multisite_plugin_name = 'Multisite'; + $not_existing_plugin_name = 'Whatever'; + + WP_Mock::wpFunction( 'get_option' ) + ->withArgs( [ 'active_plugins', [] ] ) + ->andReturn( [ $exising_plugin_name ] ); + + WP_Mock::wpFunction( 'is_multisite' ) + ->andReturn( $multisite ); + + WP_Mock::wpFunction( 'get_site_option' ) + ->withArgs( [ 'active_sitewide_plugins', [] ] ) + ->andReturn( [ $exising_multisite_plugin_name ] ); + + + $requirements = $this->create_requirements_for_php_wp( self::ALWAYS_VALID_PHP_VERSION, + self::ALWAYS_VALID_WP_VERSION ); + + $requirements->add_plugin_require( $exising_plugin_name ); + $this->assertTrue( $requirements->are_requirements_met(), 'Plugin should exists' ); + + $requirements->add_plugin_require( $exising_multisite_plugin_name ); + $this->assertTrue( $requirements->are_requirements_met(), 'Multisite plugin should exists' ); + + $requirements->add_plugin_require( $not_existing_plugin_name ); + $this->assertFalse( $requirements->are_requirements_met(), 'Plugin should not exists' ); + } +} \ No newline at end of file -- GitLab