From 5e54f36b1166a5cc8f65ca69d435de90155ac683 Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Tue, 21 May 2024 13:12:07 +0200 Subject: [PATCH] feature(conditions): additional --- CHANGELOG.md | 9 + changelog.txt | 1 - composer.json | 4 +- src/AndStrategy.php | 36 ++++ src/ConstantDefinedStrategy.php | 23 +++ src/ConstantNotDefinedStrategy.php | 23 +++ src/OrStrategy.php | 36 ++++ .../ShippingMethodInstanceStrategy.php | 46 +++++ src/WooCommerce/ShippingMethodStrategy.php | 17 ++ tests/docker-compose.yaml | 172 ------------------ tests/unit/Stub/WC_Shipping_Method.php | 13 ++ tests/unit/Stub/WC_Shipping_Zones.php | 12 ++ tests/unit/TestAndStrategy.php | 44 +++++ tests/unit/TestConstantDefinedStrategy.php | 30 +++ tests/unit/TestConstantNotDefinedStrategy.php | 30 +++ tests/unit/TestGetStrategy.php | 2 +- tests/unit/TestOrStrategy.php | 43 +++++ tests/unit/TestPostTypeStrategy.php | 4 +- .../TestShippingMethodInstanceStrategy.php | 43 +++++ .../TestShippingMethodStrategy.php | 37 ++++ tests/unit/bootstrap.php | 4 + 21 files changed, 451 insertions(+), 178 deletions(-) create mode 100644 CHANGELOG.md delete mode 100644 changelog.txt create mode 100644 src/AndStrategy.php create mode 100644 src/ConstantDefinedStrategy.php create mode 100644 src/ConstantNotDefinedStrategy.php create mode 100644 src/OrStrategy.php create mode 100644 src/WooCommerce/ShippingMethodInstanceStrategy.php create mode 100644 src/WooCommerce/ShippingMethodStrategy.php delete mode 100644 tests/docker-compose.yaml create mode 100644 tests/unit/Stub/WC_Shipping_Method.php create mode 100644 tests/unit/Stub/WC_Shipping_Zones.php create mode 100644 tests/unit/TestAndStrategy.php create mode 100644 tests/unit/TestConstantDefinedStrategy.php create mode 100644 tests/unit/TestConstantNotDefinedStrategy.php create mode 100644 tests/unit/TestOrStrategy.php create mode 100644 tests/unit/WooCommerce/TestShippingMethodInstanceStrategy.php create mode 100644 tests/unit/WooCommerce/TestShippingMethodStrategy.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..846a41a --- /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 8531c03..0000000 --- a/changelog.txt +++ /dev/null @@ -1 +0,0 @@ -* First release diff --git a/composer.json b/composer.json index 667851a..71b2111 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 0000000..d0c535f --- /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 0000000..07bd571 --- /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 0000000..81c739e --- /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 0000000..e360f47 --- /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 0000000..62933c4 --- /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 0000000..aec2498 --- /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 2a86b03..0000000 --- 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 0000000..eb03c06 --- /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 0000000..f08f48b --- /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 0000000..b89d3f4 --- /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 0000000..a969de1 --- /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 0000000..f26a011 --- /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 7da82b5..cc9dc53 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 0000000..8a76bad --- /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 31eb4ca..f685d05 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 0000000..5dff709 --- /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 0000000..f6800d3 --- /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 76b8109..e998d14 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(); -- GitLab