Skip to content
Snippets Groups Projects
Commit b38cb0e4 authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

Merge branch 'feature/conditions' into 'master'

feature(conditions): additional

See merge request !2
parents c2220557 5e54f36b
No related branches found
No related tags found
1 merge request!2feature(conditions): additional
Pipeline #367648 passed with stages
in 40 seconds
Showing
with 447 additions and 178 deletions
## [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
* First release
......@@ -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": "*",
......
<?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;
}
}
<?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);
}
}
<?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);
}
}
<?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;
}
}
<?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;
}
}
<?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 ] ]
);
}
}
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
<?php
class WC_Shipping_Method
{
public string $id;
public function __construct($instance_id)
{
$this->id = 'test_id';
}
}
<?php
class WC_Shipping_Zones
{
public static function get_shipping_method($instance_id)
{
return new \WC_Shipping_Method($instance_id);
}
}
<?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());
}
}
<?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());
}
}
<?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());
}
}
......@@ -24,7 +24,7 @@ class TestGetStrategy extends TestCase {
];
}
protected function setUp() {
protected function setUp(): void {
parent::setUp();
$this->prepare_get();
}
......
<?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());
}
}
......@@ -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();
}
......
<?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());
}
}
<?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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment