-
Notifications
You must be signed in to change notification settings - Fork 897
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21598 from Yoast/21597-refactor-iswoocommerceactive
21597 refactor iswoocommerceactive
- Loading branch information
Showing
9 changed files
with
173 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
// @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class. | ||
namespace Yoast\WP\SEO\Editors\Framework\Integrations; | ||
|
||
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional; | ||
use Yoast\WP\SEO\Editors\Domain\Integrations\Integration_Data_Provider_Interface; | ||
|
||
/** | ||
* Describes if the Woocommerce plugin is enabled. | ||
*/ | ||
class WooCommerce implements Integration_Data_Provider_Interface { | ||
|
||
/** | ||
* The WooCommerce conditional. | ||
* | ||
* @var WooCommerce_Conditional $woocommerce_conditional | ||
*/ | ||
private $woocommerce_conditional; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param WooCommerce_Conditional $woocommerce_conditional The WooCommerce conditional. | ||
*/ | ||
public function __construct( WooCommerce_Conditional $woocommerce_conditional ) { | ||
$this->woocommerce_conditional = $woocommerce_conditional; | ||
} | ||
|
||
/** | ||
* If the plugin is activated. | ||
* | ||
* @return bool If the plugin is activated. | ||
*/ | ||
public function is_enabled(): bool { | ||
return $this->woocommerce_conditional->is_met(); | ||
} | ||
|
||
/** | ||
* Return this object represented by a key value array. | ||
* | ||
* @return array<string,bool> Returns the name and if the feature is enabled. | ||
*/ | ||
public function to_array(): array { | ||
return [ 'isWooCommerceActive' => $this->is_enabled() ]; | ||
} | ||
|
||
/** | ||
* Returns this object represented by a key value structure that is compliant with the script data array. | ||
* | ||
* @return array<string,bool> Returns the legacy key and if the feature is enabled. | ||
*/ | ||
public function to_legacy_array(): array { | ||
return [ 'isWooCommerceActive' => $this->is_enabled() ]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
tests/Unit/Editors/Framework/Integrations/WooCommerce_Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
// @phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- This namespace should reflect the namespace of the original class. | ||
namespace Yoast\WP\SEO\Tests\Unit\Editors\Framework\Integrations; | ||
|
||
use Mockery; | ||
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional; | ||
use Yoast\WP\SEO\Editors\Framework\Integrations\WooCommerce; | ||
use Yoast\WP\SEO\Tests\Unit\TestCase; | ||
|
||
/** | ||
* Class WooCommerce_Test | ||
* | ||
* @group editors | ||
* | ||
* @covers \Yoast\WP\SEO\Editors\Framework\Integrations\WooCommerce | ||
*/ | ||
final class WooCommerce_Test extends TestCase { | ||
|
||
/** | ||
* Holds the WooCommerce conditional mock. | ||
* | ||
* @var Mockery\MockInterface|WooCommerce_Conditional | ||
*/ | ||
protected $woocommerce_conditional; | ||
|
||
/** | ||
* The WooCommerce feature. | ||
* | ||
* @var WooCommerce | ||
*/ | ||
private $instance; | ||
|
||
/** | ||
* Set up the test. | ||
* | ||
* @return void | ||
*/ | ||
protected function set_up() { | ||
parent::set_up(); | ||
$this->woocommerce_conditional = Mockery::mock( WooCommerce_Conditional::class ); | ||
$this->instance = new WooCommerce( $this->woocommerce_conditional ); | ||
} | ||
|
||
/** | ||
* Tests the is_enabled method. | ||
* | ||
* @dataProvider data_provider_is_enabled | ||
* | ||
* @param bool $woocommerce_enabled If the woocommerce plugin is enabled. | ||
* @param bool $expected The expected outcome. | ||
* | ||
* @return void | ||
*/ | ||
public function test_is_enabled( | ||
bool $woocommerce_enabled, | ||
bool $expected | ||
) { | ||
|
||
$this->woocommerce_conditional | ||
->expects( 'is_met' ) | ||
->times( 3 ) | ||
->andReturn( $woocommerce_enabled ); | ||
|
||
$this->assertSame( $expected, $this->instance->is_enabled() ); | ||
$this->assertSame( [ 'isWooCommerceActive' => $this->instance->is_enabled() ], $this->instance->to_legacy_array() ); | ||
} | ||
|
||
/** | ||
* Tests the to_array method. | ||
* | ||
* @dataProvider data_provider_is_enabled | ||
* | ||
* @param bool $woocommerce_enabled If the woocommerce plugin is enabled. | ||
* @param bool $expected The expected outcome. | ||
* | ||
* @return void | ||
*/ | ||
public function test_to_array( | ||
bool $woocommerce_enabled, | ||
bool $expected | ||
) { | ||
|
||
$this->woocommerce_conditional | ||
->expects( 'is_met' ) | ||
->times( 3 ) | ||
->andReturn( $woocommerce_enabled ); | ||
|
||
$this->assertSame( $expected, $this->instance->is_enabled() ); | ||
$this->assertSame( [ 'isWooCommerceActive' => $this->instance->is_enabled() ], $this->instance->to_array() ); | ||
} | ||
|
||
/** | ||
* Data provider for test_is_enabled. | ||
* | ||
* @return array<array<string|bool>> | ||
*/ | ||
public static function data_provider_is_enabled() { | ||
return [ | ||
'Enabled' => [ | ||
'woocommerce_enabled' => true, | ||
'expected' => true, | ||
], | ||
'Disabled' => [ | ||
'woocommerce_enabled' => false, | ||
'expected' => false, | ||
], | ||
]; | ||
} | ||
} |