Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/WooCommerce/Config/WooCommerceConfig.php

This file was deleted.

5 changes: 2 additions & 3 deletions src/WooCommerce/Method/CartMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Aztec\WPBrowser\WooCommerce\Method;

use Aztec\WPBrowser\WooCommerce\Config\WooCommerceConfig;
use Aztec\WPBrowser\WooCommerce\PageObject\CartPageObject;
use Aztec\WPBrowser\WooCommerce\PageObject\PageObjectProvider;
use Codeception\Util\Locator;
Expand All @@ -14,7 +13,7 @@ trait CartMethods
{
abstract protected function wpWebDriver(): WPWebDriver;

abstract protected function wooCommerceConfig(): WooCommerceConfig;
abstract protected function cartPageSlug(): string;

abstract protected function pageObjectProvider(): PageObjectProvider;

Expand All @@ -33,7 +32,7 @@ abstract protected function selector(mixed $value): string;
*/
public function amOnCartPage(): void
{
$this->wpWebDriver()->amOnPage($this->wooCommerceConfig()->cartPageSlug());
$this->wpWebDriver()->amOnPage($this->cartPageSlug());
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/WooCommerce/Method/CheckoutMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@

namespace Aztec\WPBrowser\WooCommerce\Method;

use Aztec\WPBrowser\WooCommerce\Config\WooCommerceConfig;
use Aztec\WPBrowser\WooCommerce\PageObject\CheckoutPageObject;
use Aztec\WPBrowser\WooCommerce\PageObject\PageObjectProvider;
use lucatume\WPBrowser\Module\WPDb;
use lucatume\WPBrowser\Module\WPWebDriver;

trait CheckoutMethods
{
abstract protected function wpWebDriver(): WPWebDriver;
abstract protected function wpDb(): WPDb;
abstract protected function wooCommerceConfig(): WooCommerceConfig;
abstract protected function checkoutPageSlug(): string;
abstract protected function pageObjectProvider(): PageObjectProvider;
abstract protected function selector(mixed $value): string;

Expand All @@ -33,7 +30,7 @@ abstract protected function selector(mixed $value): string;
*/
public function amOnCheckoutPage(): void
{
$this->wpWebDriver()->amOnPage($this->wooCommerceConfig()->checkoutPageSlug());
$this->wpWebDriver()->amOnPage($this->checkoutPageSlug());
$this->wpWebDriver()->waitForElement('.wc-block-checkout');
}

Expand Down
5 changes: 2 additions & 3 deletions src/WooCommerce/Method/CustomerBrowserMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace Aztec\WPBrowser\WooCommerce\Method;

use Aztec\WPBrowser\WooCommerce\Config\WooCommerceConfig;
use lucatume\WPBrowser\Module\WPWebDriver;

trait CustomerBrowserMethods
{
abstract protected function wpWebDriver(): WPWebDriver;

abstract protected function wooCommerceConfig(): WooCommerceConfig;
abstract protected function myAccountPageSlug(): string;

/**
* Navigate to the WooCommerce My Account page.
Expand All @@ -26,6 +25,6 @@ abstract protected function wooCommerceConfig(): WooCommerceConfig;
*/
public function amOnMyAccountPage(): void
{
$this->wpWebDriver()->amOnPage($this->wooCommerceConfig()->myAccountPageSlug());
$this->wpWebDriver()->amOnPage($this->myAccountPageSlug());
}
}
10 changes: 9 additions & 1 deletion src/WooCommerce/Module/WooCommerceDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Aztec\WPBrowser\WooCommerce\SubscriptionStorage\SubscriptionStorageInterface;
use Codeception\Exception\ModuleException;
use Codeception\Module;
use lucatume\WPBrowser\Module\WPDb;

class WooCommerceDb extends Module
{
Expand All @@ -28,7 +29,6 @@ class WooCommerceDb extends Module
use OrderMethods;
use ProductMethods;
use SubscriptionMethods;
use WooCommerceModuleSupport;

public function _initialize(): void
{
Expand All @@ -40,6 +40,14 @@ public function _initialize(): void
}
}

protected function wpDb(): WPDb
{
$module = $this->getModule('WPDb');
assert($module instanceof WPDb);

return $module;
}

protected function isHposEnabled(): bool
{
return HposState::isEnabled($this->wpDb());
Expand Down
54 changes: 0 additions & 54 deletions src/WooCommerce/Module/WooCommerceModuleSupport.php

This file was deleted.

49 changes: 40 additions & 9 deletions src/WooCommerce/Module/WooCommerceWebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class WooCommerceWebDriver extends Module
use CheckoutMethods;
use CustomerBrowserMethods;
use OrderBrowserMethods;
use WooCommerceModuleSupport;

/** @var array<string, mixed> */
protected array $config = [
Expand All @@ -44,14 +43,6 @@ public function _initialize(): void
'WooCommerceWebDriver requires the WPWebDriver module to be enabled in the same suite.',
);
}

if (! $this->hasModule('WPDb')) {
throw new ModuleException(
$this,
'WooCommerceWebDriver requires the WPDb module to be enabled in the same suite '
. '(it reads product and cart data directly from the database).',
);
}
}

protected function wpWebDriver(): WPWebDriver
Expand All @@ -72,4 +63,44 @@ protected function pageObjectProvider(): PageObjectProvider

return $this->pageObjectProvider;
}

protected function cartPageSlug(): string
{
return $this->pageSlugConfig('cartPageSlug');
}

protected function checkoutPageSlug(): string
{
return $this->pageSlugConfig('checkoutPageSlug');
}

protected function myAccountPageSlug(): string
{
return $this->pageSlugConfig('myAccountPageSlug');
}

private function pageSlugConfig(string $key): string
{
$value = $this->_getConfig($key);

if (!is_string($value)) {
throw new ModuleException($this, "Config key \"{$key}\" must be a string slug (e.g. \"/cart\").");
}

return $value;
}

/**
* Narrow a page-object selector constant to a string.
*
* Page objects expose their selectors as untyped class constants because
* the package targets PHP 8.0+, where typed class constants are not
* available. Reading such a constant through a (non-final, overridable)
* page-object instance therefore widens to mixed under static analysis.
* Selectors are always strings, so this safely narrows the value.
*/
protected function selector(mixed $value): string
{
return is_string($value) ? $value : '';
}
}
29 changes: 0 additions & 29 deletions tests/unit/WooCommerce/Config/WooCommerceConfigTest.php

This file was deleted.

16 changes: 1 addition & 15 deletions tests/unit/WooCommerce/Module/SiblingModuleCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,10 @@ public function testWooCommerceWebDriverThrowsWhenWpWebDriverIsMissing(): void
$module->_initialize();
}

public function testWooCommerceWebDriverThrowsWhenWpDbIsMissing(): void
public function testWooCommerceWebDriverInitializesWhenWpWebDriverIsPresent(): void
{
$module = $this->moduleWithSiblings(WooCommerceWebDriver::class, [
'WPWebDriver' => true,
'WPDb' => false,
]);

$this->expectException(ModuleException::class);
$this->expectExceptionMessage('WPDb');

$module->_initialize();
}

public function testWooCommerceWebDriverInitializesWhenBothSiblingsArePresent(): void
{
$module = $this->moduleWithSiblings(WooCommerceWebDriver::class, [
'WPWebDriver' => true,
'WPDb' => true,
]);

$this->expectNotToPerformAssertions();
Expand Down
45 changes: 25 additions & 20 deletions tests/unit/WooCommerce/Module/WooCommerceWebDriverPageSlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Aztec\WPBrowser\Tests\Unit\WooCommerce\Module;

use Aztec\WPBrowser\WooCommerce\Config\WooCommerceConfig;
use Aztec\WPBrowser\WooCommerce\Module\WooCommerceWebDriver;
use Codeception\Test\Unit;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -15,11 +14,11 @@ class WooCommerceWebDriverPageSlugTest extends Unit
{
public function testDefaultsToWooCommerceConventionSlugs(): void
{
$config = $this->wooCommerceConfigOf($this->module());
$module = $this->module();

$this->assertSame('/cart', $config->cartPageSlug());
$this->assertSame('/checkout', $config->checkoutPageSlug());
$this->assertSame('/my-account', $config->myAccountPageSlug());
$this->assertSame('/cart', $this->invoke($module, 'cartPageSlug'));
$this->assertSame('/checkout', $this->invoke($module, 'checkoutPageSlug'));
$this->assertSame('/my-account', $this->invoke($module, 'myAccountPageSlug'));
}

public function testEachSlugIsIndependentlyOverridable(): void
Expand All @@ -28,11 +27,28 @@ public function testEachSlugIsIndependentlyOverridable(): void
'checkoutPageSlug' => '/finalizar-compra',
]);

$config = $this->wooCommerceConfigOf($module);
$this->assertSame('/cart', $this->invoke($module, 'cartPageSlug'), 'untouched slug keeps its default');
$this->assertSame(
'/finalizar-compra',
$this->invoke($module, 'checkoutPageSlug'),
'overridden slug wins',
);
$this->assertSame(
'/my-account',
$this->invoke($module, 'myAccountPageSlug'),
'untouched slug keeps its default',
);
}

private function invoke(WooCommerceWebDriver $module, string $method): string
{
$reflection = new ReflectionMethod($module, $method);
$reflection->setAccessible(true);

$this->assertSame('/cart', $config->cartPageSlug(), 'untouched slug keeps its default');
$this->assertSame('/finalizar-compra', $config->checkoutPageSlug(), 'overridden slug wins');
$this->assertSame('/my-account', $config->myAccountPageSlug(), 'untouched slug keeps its default');
$value = $reflection->invoke($module);
$this->assertIsString($value);

return $value;
}

/**
Expand All @@ -56,15 +72,4 @@ private function module(array $overrides = []): WooCommerceWebDriver

return $module;
}

private function wooCommerceConfigOf(WooCommerceWebDriver $module): WooCommerceConfig
{
$method = new ReflectionMethod($module, 'wooCommerceConfig');
$method->setAccessible(true);

$config = $method->invoke($module);
assert($config instanceof WooCommerceConfig);

return $config;
}
}
Loading