diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000000000000000000000000000000000000..846a41ad2655db04df1d9df794c00790ffbd37d4 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +## [1.1.0] - 2020-05-20 +### Added +- logical and/or strategies +- WooCommerce shipping methods strategies +- constant strategies + +## [1.0.0] - 2020-05-20 +### Added +- initial version diff --git a/changelog.txt b/changelog.txt deleted file mode 100644 index 8531c034488faafdf5b4bd890e424a615b5234b9..0000000000000000000000000000000000000000 --- a/changelog.txt +++ /dev/null @@ -1 +0,0 @@ -* First release diff --git a/composer.json b/composer.json index 667851ae2627f9638401ce784c8da4a495170c48..71b2111c0c408f2a573c42ce1866ec3541570e39 100644 --- a/composer.json +++ b/composer.json @@ -7,12 +7,12 @@ } ], "require": { - "php": ">=5.6", + "php": ">=7.4", "ext-curl": "*", "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "<7", + "phpunit/phpunit": "^6||^7||^8||^9", "wp-coding-standards/wpcs": "^0.14.1", "squizlabs/php_codesniffer": "^3.0.2", "mockery/mockery": "*", diff --git a/src/AndStrategy.php b/src/AndStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..d0c535f26d83749898254f6b392ac881e2beea4f --- /dev/null +++ b/src/AndStrategy.php @@ -0,0 +1,36 @@ +<?php + +namespace WPDesk\ShowDecision; + +class AndStrategy implements ShouldShowStrategy +{ + + /** + * @var ShouldShowStrategy[] + */ + private array $conditions = []; + + public function __construct(ShouldShowStrategy $strategy) + { + $this->conditions[] = $strategy; + } + + public function addCondition(ShouldShowStrategy $condition): self + { + $this->conditions[] = $condition; + + return $this; + } + + public function shouldDisplay(): bool + { + foreach ($this->conditions as $condition) { + if ( ! $condition->shouldDisplay()) { + return false; + } + } + + return true; + } + +} diff --git a/src/ConstantDefinedStrategy.php b/src/ConstantDefinedStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..07bd571b3de77c327db5c7bca720f5c9d4cf8afe --- /dev/null +++ b/src/ConstantDefinedStrategy.php @@ -0,0 +1,23 @@ +<?php + +namespace WPDesk\ShowDecision; + +class ConstantDefinedStrategy implements ShouldShowStrategy +{ + + /** + * @var string + */ + private string $constant; + + public function __construct(string $constant) + { + $this->constant = $constant; + } + + public function shouldDisplay(): bool + { + return defined($this->constant); + } + +} diff --git a/src/ConstantNotDefinedStrategy.php b/src/ConstantNotDefinedStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..81c739e4b28ca82c458356fa3bbb028fe6abf4fd --- /dev/null +++ b/src/ConstantNotDefinedStrategy.php @@ -0,0 +1,23 @@ +<?php + +namespace WPDesk\ShowDecision; + +class ConstantNotDefinedStrategy implements ShouldShowStrategy +{ + + /** + * @var string + */ + private string $constant; + + public function __construct(string $constant) + { + $this->constant = $constant; + } + + public function shouldDisplay(): bool + { + return ! defined($this->constant); + } + +} diff --git a/src/OrStrategy.php b/src/OrStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..e360f47612aafe86ce4709b1db69b174186d3f25 --- /dev/null +++ b/src/OrStrategy.php @@ -0,0 +1,36 @@ +<?php + +namespace WPDesk\ShowDecision; + +class OrStrategy implements ShouldShowStrategy +{ + + /** + * @var ShouldShowStrategy[] + */ + private array $conditions = []; + + public function __construct(ShouldShowStrategy $strategy) + { + $this->conditions[] = $strategy; + } + + public function addCondition(ShouldShowStrategy $condition): self + { + $this->conditions[] = $condition; + + return $this; + } + + public function shouldDisplay(): bool + { + foreach ($this->conditions as $condition) { + if ($condition->shouldDisplay()) { + return true; + } + } + + return false; + } + +} diff --git a/src/WooCommerce/ShippingMethodInstanceStrategy.php b/src/WooCommerce/ShippingMethodInstanceStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..62933c4ed62434390960daa62b660db725afec05 --- /dev/null +++ b/src/WooCommerce/ShippingMethodInstanceStrategy.php @@ -0,0 +1,46 @@ +<?php + +namespace WPDesk\ShowDecision\WooCommerce; + +use WPDesk\ShowDecision\ShouldShowStrategy; + +class ShippingMethodInstanceStrategy implements ShouldShowStrategy +{ + + private \WC_Shipping_Zones $shipping_zones; + + private string $method_id; + + public function __construct(\WC_Shipping_Zones $shipping_zones, string $method_id) + { + $this->shipping_zones = $shipping_zones; + $this->method_id = $method_id; + } + + public function shouldDisplay(): bool + { + if ($this->isInShippingSettings()) { + if (isset($_GET['instance_id'])) { + $shipping_method = $this->shipping_zones::get_shipping_method(sanitize_key($_GET['instance_id'])); + if ($shipping_method instanceof \WC_Shipping_Method) { + return $shipping_method->id === $this->method_id; + } + } + } + return false; + } + + private function isInShippingSettings() : bool + { + if ($this->isGetParameterWithValue('page', 'wc-settings') && $this->isGetParameterWithValue('tab', 'shipping')) { + return \true; + } + return \false; + } + + private function isGetParameterWithValue(string $parameter, string $value) : bool + { + return isset($_GET[$parameter]) && $_GET[$parameter] === $value; + } + +} diff --git a/src/WooCommerce/ShippingMethodStrategy.php b/src/WooCommerce/ShippingMethodStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..aec24989ef62a9ab3a0be4f31cda930e592c54e5 --- /dev/null +++ b/src/WooCommerce/ShippingMethodStrategy.php @@ -0,0 +1,17 @@ +<?php + +namespace WPDesk\ShowDecision\WooCommerce; + +use WPDesk\ShowDecision\GetStrategy; + +class ShippingMethodStrategy extends GetStrategy +{ + + public function __construct(string $method_id) + { + parent::__construct( + [ [ 'page'=> 'wc-settings', 'tab' => 'shipping', 'section' => $method_id ] ] + ); + } + +} diff --git a/tests/docker-compose.yaml b/tests/docker-compose.yaml deleted file mode 100644 index 2a86b036f682ed318b27b028d2e96dfcd5979afd..0000000000000000000000000000000000000000 --- a/tests/docker-compose.yaml +++ /dev/null @@ -1,172 +0,0 @@ -version: '2.0' - -services: - - wordpress: - image: wpdesknet/phpunit-woocommerce:0-0 - volumes: - - .././:/opt/project - depends_on: - - mysql0 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql0 - - wordpress-0-1: - image: wpdesknet/phpunit-woocommerce:0-1 - volumes: - - .././:/opt/project - depends_on: - - mysql1 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql1 - - wordpress-0-2: - image: wpdesknet/phpunit-woocommerce:0-2 - volumes: - - .././:/opt/project - depends_on: - - mysql2 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql2 - - wordpress-0-3: - image: wpdesknet/phpunit-woocommerce:0-3 - volumes: - - .././:/opt/project - depends_on: - - mysql3 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql3 - - wordpress-0-4: - image: wpdesknet/phpunit-woocommerce:0-4 - volumes: - - .././:/opt/project - depends_on: - - mysql4 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql4 - - wordpress-0-5: - image: wpdesknet/phpunit-woocommerce:0-5 - volumes: - - .././:/opt/project - depends_on: - - mysql5 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql5 - - wordpress-1-0: - image: wpdesknet/phpunit-woocommerce:1-0 - volumes: - - .././:/opt/project - depends_on: - - mysql0 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql0 - - wordpress-2-0: - image: wpdesknet/phpunit-woocommerce:2-0 - volumes: - - .././:/opt/project - depends_on: - - mysql0 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql0 - - wordpress-3-0: - image: wpdesknet/phpunit-woocommerce:3-0 - volumes: - - .././:/opt/project - depends_on: - - mysql0 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql0 - - wordpress-4-0: - image: wpdesknet/phpunit-woocommerce:4-0 - volumes: - - .././:/opt/project - depends_on: - - mysql0 - environment: - WORDPRESS_DB_NAME: wptest - WORDPRESS_DB_USER: mysql - WORDPRESS_DB_PASSWORD: mysql - WORDPRESS_DB_HOST: mysql0 - - mysql0: - image: mysql:5.7 - environment: - MYSQL_ROOT_PASSWORD: mysql - MYSQL_DATABASE: wptest - MYSQL_USER: mysql - MYSQL_PASSWORD: mysql - - mysql1: - image: mysql:5.7 - environment: - MYSQL_ROOT_PASSWORD: mysql - MYSQL_DATABASE: wptest - MYSQL_USER: mysql - MYSQL_PASSWORD: mysql - - mysql2: - image: mysql:5.7 - environment: - MYSQL_ROOT_PASSWORD: mysql - MYSQL_DATABASE: wptest - MYSQL_USER: mysql - MYSQL_PASSWORD: mysql - - mysql3: - image: mysql:5.7 - environment: - MYSQL_ROOT_PASSWORD: mysql - MYSQL_DATABASE: wptest - MYSQL_USER: mysql - MYSQL_PASSWORD: mysql - - mysql4: - image: mysql:5.7 - environment: - MYSQL_ROOT_PASSWORD: mysql - MYSQL_DATABASE: wptest - MYSQL_USER: mysql - MYSQL_PASSWORD: mysql - - mysql5: - image: mysql:5.7 - environment: - MYSQL_ROOT_PASSWORD: mysql - MYSQL_DATABASE: wptest - MYSQL_USER: mysql - MYSQL_PASSWORD: mysql - diff --git a/tests/unit/Stub/WC_Shipping_Method.php b/tests/unit/Stub/WC_Shipping_Method.php new file mode 100644 index 0000000000000000000000000000000000000000..eb03c06e6ced17d09992c7d9de4c2af9b52c0c73 --- /dev/null +++ b/tests/unit/Stub/WC_Shipping_Method.php @@ -0,0 +1,13 @@ +<?php + +class WC_Shipping_Method +{ + + public string $id; + + public function __construct($instance_id) + { + $this->id = 'test_id'; + } + +} diff --git a/tests/unit/Stub/WC_Shipping_Zones.php b/tests/unit/Stub/WC_Shipping_Zones.php new file mode 100644 index 0000000000000000000000000000000000000000..f08f48bda9cc62eea9db13889ba8fc6ff5351909 --- /dev/null +++ b/tests/unit/Stub/WC_Shipping_Zones.php @@ -0,0 +1,12 @@ +<?php + +class WC_Shipping_Zones +{ + + public static function get_shipping_method($instance_id) + { + return new \WC_Shipping_Method($instance_id); + } + + +} diff --git a/tests/unit/TestAndStrategy.php b/tests/unit/TestAndStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..b89d3f4f1daf5ded851a9d777cb9c87871283faa --- /dev/null +++ b/tests/unit/TestAndStrategy.php @@ -0,0 +1,44 @@ +<?php + + +namespace unit; + +use WPDesk\ShowDecision\AndStrategy; + + +class TestAndStrategy extends \WP_Mock\Tools\TestCase +{ + + public function testShouldReturnTrueWhenAllConditionsAreTrue() + { + // Given + $strategy1 = $this->createMock(\WPDesk\ShowDecision\ShouldShowStrategy::class); + $strategy1->method('shouldDisplay')->willReturn(true); + + $strategy2 = $this->createMock(\WPDesk\ShowDecision\ShouldShowStrategy::class); + $strategy2->method('shouldDisplay')->willReturn(true); + + $andStrategy = new AndStrategy($strategy1); + $andStrategy->addCondition($strategy2); + + // When & Then + $this->assertTrue($andStrategy->shouldDisplay()); + } + + public function testShouldReturnFalseWhenAnyConditionIsFalse() + { + // Given + $strategy1 = $this->createMock(\WPDesk\ShowDecision\ShouldShowStrategy::class); + $strategy1->method('shouldDisplay')->willReturn(true); + + $strategy2 = $this->createMock(\WPDesk\ShowDecision\ShouldShowStrategy::class); + $strategy2->method('shouldDisplay')->willReturn(false); + + $andStrategy = new AndStrategy($strategy1); + $andStrategy->addCondition($strategy2); + + // When & Then + $this->assertFalse($andStrategy->shouldDisplay()); + } + +} diff --git a/tests/unit/TestConstantDefinedStrategy.php b/tests/unit/TestConstantDefinedStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..a969de1e41a98bfee72c3734117e80cd999ffbf8 --- /dev/null +++ b/tests/unit/TestConstantDefinedStrategy.php @@ -0,0 +1,30 @@ +<?php + + +namespace unit; + +use WPDesk\ShowDecision\ConstantDefinedStrategy; + +class TestConstantDefinedStrategy extends \WP_Mock\Tools\TestCase +{ + + public function testShouldDisplayWhenConstantIsDefined() + { + // Given + define('TEST_CONSTANT', 'test'); + $strategy = new ConstantDefinedStrategy('TEST_CONSTANT'); + + // When & Then + $this->assertTrue($strategy->shouldDisplay()); + } + + public function testShouldNotDisplayWhenConstantIsNotDefined() + { + // Given + $strategy = new ConstantDefinedStrategy('TEST2_CONSTANT'); + + // When & Then + $this->assertFalse($strategy->shouldDisplay()); + } + +} diff --git a/tests/unit/TestConstantNotDefinedStrategy.php b/tests/unit/TestConstantNotDefinedStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..f26a011b5b3e05ef50c959c8c50216bad6b73d63 --- /dev/null +++ b/tests/unit/TestConstantNotDefinedStrategy.php @@ -0,0 +1,30 @@ +<?php + + +namespace unit; + +use WPDesk\ShowDecision\ConstantNotDefinedStrategy; + +class TestConstantNotDefinedStrategy extends \WP_Mock\Tools\TestCase +{ + + public function testShouldNotDisplayWhenConstantIsDefined() + { + // Given + define('TEST3_CONSTANT', 'test'); + $strategy = new ConstantNotDefinedStrategy('TEST_CONSTANT'); + + // When & Then + $this->assertFalse($strategy->shouldDisplay()); + } + + public function testShouldDisplayWhenConstantIsNotDefined() + { + // Given + $strategy = new ConstantNotDefinedStrategy('TEST4_CONSTANT'); + + // When & Then + $this->assertTrue($strategy->shouldDisplay()); + } + +} diff --git a/tests/unit/TestGetStrategy.php b/tests/unit/TestGetStrategy.php index 7da82b56f829de2cb217d7a431d48da852c6cd31..cc9dc53979e889001e3001c47f96fd031e38b344 100644 --- a/tests/unit/TestGetStrategy.php +++ b/tests/unit/TestGetStrategy.php @@ -24,7 +24,7 @@ class TestGetStrategy extends TestCase { ]; } - protected function setUp() { + protected function setUp(): void { parent::setUp(); $this->prepare_get(); } diff --git a/tests/unit/TestOrStrategy.php b/tests/unit/TestOrStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..8a76bad95dbffd30517d4746572d51eaaefb3de5 --- /dev/null +++ b/tests/unit/TestOrStrategy.php @@ -0,0 +1,43 @@ +<?php + + +namespace unit; + +use WPDesk\ShowDecision\OrStrategy; + +class TestOrStrategy extends \WP_Mock\Tools\TestCase +{ + + public function testShouldReturnTrueWhenAnyConditionsAreTrue() + { + // Given + $strategy1 = $this->createMock(\WPDesk\ShowDecision\ShouldShowStrategy::class); + $strategy1->method('shouldDisplay')->willReturn(true); + + $strategy2 = $this->createMock(\WPDesk\ShowDecision\ShouldShowStrategy::class); + $strategy2->method('shouldDisplay')->willReturn(false); + + $orStrategy = new OrStrategy($strategy1); + $orStrategy->addCondition($strategy2); + + // When & Then + $this->assertTrue($orStrategy->shouldDisplay()); + } + + public function testShouldReturnFalseWhenAllConditionsAreFalse() + { + // Given + $strategy1 = $this->createMock(\WPDesk\ShowDecision\ShouldShowStrategy::class); + $strategy1->method('shouldDisplay')->willReturn(false); + + $strategy2 = $this->createMock(\WPDesk\ShowDecision\ShouldShowStrategy::class); + $strategy2->method('shouldDisplay')->willReturn(false); + + $orStrategy = new OrStrategy($strategy1); + $orStrategy->addCondition($strategy2); + + // When & Then + $this->assertFalse($orStrategy->shouldDisplay()); + } + +} diff --git a/tests/unit/TestPostTypeStrategy.php b/tests/unit/TestPostTypeStrategy.php index 31eb4caa0bbee85a50bf06945b4c58e3b221ab92..f685d05c2c5f7cd408c3a1521254758a492f7244 100644 --- a/tests/unit/TestPostTypeStrategy.php +++ b/tests/unit/TestPostTypeStrategy.php @@ -10,13 +10,13 @@ class TestPostTypeStrategy extends TestCase { const valid_post_type = 'test_post_type'; const invalid_post_type = 'invalid'; - protected function setUp() { + protected function setUp(): void { require_once __DIR__ . DIRECTORY_SEPARATOR . 'Stub' . DIRECTORY_SEPARATOR . 'WP_Post.php'; parent::setUp(); \WP_Mock::setUp(); } - protected function tearDown() { + protected function tearDown(): void { parent::tearDown(); \WP_Mock::tearDown(); } diff --git a/tests/unit/WooCommerce/TestShippingMethodInstanceStrategy.php b/tests/unit/WooCommerce/TestShippingMethodInstanceStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..5dff7091f1cb41433a45270557128cbaa91ecdcb --- /dev/null +++ b/tests/unit/WooCommerce/TestShippingMethodInstanceStrategy.php @@ -0,0 +1,43 @@ +<?php + +namespace unit\WooCommerce; + + +use WP_Mock\Tools\TestCase; +use WPDesk\ShowDecision\WooCommerce\ShippingMethodInstanceStrategy; + +class TestShippingMethodInstanceStrategy extends TestCase +{ + + public function testShouldReturnTrueWhenShipppingMethodInstanceMatches() + { + // Expect + \WP_Mock::passthruFunction('sanitize_key'); + + // Given + $_GET = + [ 'page' => 'wc-settings', 'tab' => 'shipping', 'instance_id' => '1' ] + ; + $strategy = new ShippingMethodInstanceStrategy( new \WC_Shipping_Zones(), 'test_id'); + + // When & Then + $this->assertTrue($strategy->shouldDisplay()); + } + + public function testShouldReturnFalseWhenShipppingMethodInstanceDoesNotMatch() + { + // Expect + \WP_Mock::passthruFunction('sanitize_key'); + + // Given + $_GET = + [ 'page' => 'wc-settings', 'tab' => 'shipping', 'instance_id' => '2' ] + ; + $strategy = new ShippingMethodInstanceStrategy( new \WC_Shipping_Zones(), 'other_test_id'); + + // When & Then + $this->assertFalse($strategy->shouldDisplay()); + } + + +} diff --git a/tests/unit/WooCommerce/TestShippingMethodStrategy.php b/tests/unit/WooCommerce/TestShippingMethodStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..f6800d3df5adf1f585f5b456cf267c1da0b8c4de --- /dev/null +++ b/tests/unit/WooCommerce/TestShippingMethodStrategy.php @@ -0,0 +1,37 @@ +<?php + +namespace unit\WooCommerce; + + +use WP_Mock\Tools\TestCase; +use WPDesk\ShowDecision\WooCommerce\ShippingMethodStrategy; + +class TestShippingMethodStrategy extends TestCase +{ + + public function testShouldReturnTrueWhenShipppingMethodMatches() + { + // Given + $_GET = + [ 'page' => 'wc-settings', 'tab' => 'shipping', 'section' => 'test_id' ] + ; + $strategy = new ShippingMethodStrategy('test_id'); + + // When & Then + $this->assertTrue($strategy->shouldDisplay()); + } + + public function testShouldReturnFalseWhenShipppingMethodDoesNotMatch() + { + // Given + $_GET = + [ 'page' => 'wc-settings', 'tab' => 'shipping', 'section' => 'test_id' ] + ; + $strategy = new ShippingMethodStrategy('other_test_id'); + + // When & Then + $this->assertFalse($strategy->shouldDisplay()); + } + + +} diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 76b8109582ae17560b77a6e0499b232e09047810..e998d1450d581108193d534451d2808b8f89dbd8 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -5,5 +5,9 @@ require_once __DIR__ . '/../../vendor/autoload.php'; +require_once __DIR__ . '/Stub/WP_Post.php'; +require_once __DIR__ . '/Stub/WC_Shipping_Zones.php'; +require_once __DIR__ . '/Stub/WC_Shipping_Method.php'; + WP_Mock::setUsePatchwork( true ); WP_Mock::bootstrap();