diff --git a/src/Pages/v9/BackOffice/Page.php b/src/Pages/v9/BackOffice/Page.php new file mode 100644 index 0000000..33b015c --- /dev/null +++ b/src/Pages/v9/BackOffice/Page.php @@ -0,0 +1,9 @@ +exctractVersions(Env::get('PRESTAFLOW_PS_VERSION', '8.1.0')); + // Not the environment alone: a suite may pin its version through + // $psVersion or onVersion(), which resolveVersion() lets win. + $this->resolveVersion(); $this->setLocale(Env::get('PRESTAFLOW_LOCALE', 'en')); } diff --git a/src/Traits/Version.php b/src/Traits/Version.php index 6d32b31..cfc5ba5 100644 --- a/src/Traits/Version.php +++ b/src/Traits/Version.php @@ -37,6 +37,12 @@ public function onVersion(string $version): self $this->psVersionOverride = $version; + // Once the globals are loaded, the version has already been resolved: + // resolve it again so the pages imported next use this one. + if (!empty($this->globals)) { + $this->resolveVersion(); + } + return $this; } diff --git a/tests/Unit/Pages/VersionBasePagesTest.php b/tests/Unit/Pages/VersionBasePagesTest.php new file mode 100644 index 0000000..422375e --- /dev/null +++ b/tests/Unit/Pages/VersionBasePagesTest.php @@ -0,0 +1,34 @@ +\FrontOffice\Page, and custom pages extend these + * classes: the v9 pair went missing in the Common-to-v9 migration, which broke + * both on PrestaShop 9 (VisualTestsSuite included). + */ +final class VersionBasePagesTest extends TestCase +{ + public static function areaBases(): array + { + $cases = []; + foreach (['v7', 'v8', 'v9'] as $version) { + $cases["$version FrontOffice"] = ["PrestaFlow\\Library\\Pages\\$version\\FrontOffice\\Page", CommonFrontOfficePage::class]; + $cases["$version BackOffice"] = ["PrestaFlow\\Library\\Pages\\$version\\BackOffice\\Page", CommonBackOfficePage::class]; + } + + return $cases; + } + + /** @dataProvider areaBases */ + public function test_each_version_has_its_area_base_page(string $fqcn, string $parent): void + { + $this->assertTrue(class_exists($fqcn), "$fqcn is missing"); + $this->assertTrue(is_subclass_of($fqcn, $parent), "$fqcn must extend $parent"); + } +} diff --git a/tests/Unit/Tests/SuiteVersionWiringTest.php b/tests/Unit/Tests/SuiteVersionWiringTest.php new file mode 100644 index 0000000..ea74462 --- /dev/null +++ b/tests/Unit/Tests/SuiteVersionWiringTest.php @@ -0,0 +1,85 @@ +savedEnv = $_ENV['PRESTAFLOW_PS_VERSION'] ?? null; + $_ENV['PRESTAFLOW_PS_VERSION'] = '9.0.0'; + putenv('PRESTAFLOW_PS_VERSION=9.0.0'); + } + + protected function tearDown(): void + { + if ($this->savedEnv === null) { + unset($_ENV['PRESTAFLOW_PS_VERSION']); + putenv('PRESTAFLOW_PS_VERSION'); + } else { + $_ENV['PRESTAFLOW_PS_VERSION'] = $this->savedEnv; + putenv('PRESTAFLOW_PS_VERSION=' . $this->savedEnv); + } + parent::tearDown(); + } + + public function test_the_environment_version_applies_without_an_override(): void + { + $suite = new class (loadGlobals: true, getBrowser: false) extends TestsSuite {}; + + $this->assertSame('9', $suite->getMajorVersion(namespace: true)); + $this->assertSame('9.0.0', $suite->getGlobals()['PS_VERSION']); + } + + public function test_the_ps_version_property_wins_over_the_environment(): void + { + $suite = new class (loadGlobals: true, getBrowser: false) extends TestsSuite { + protected $psVersion = '1.7.8.11'; + }; + + $this->assertSame('7', $suite->getMajorVersion(namespace: true)); + $this->assertSame('1.7.8.11', $suite->getGlobals()['PS_VERSION']); + } + + public function test_on_version_takes_effect_without_calling_resolve_version(): void + { + $suite = new class (loadGlobals: true, getBrowser: false) extends TestsSuite {}; + + $suite->onVersion('8.2.0'); + + $this->assertSame('8', $suite->getMajorVersion(namespace: true)); + $this->assertSame('8.2.0', $suite->getGlobals()['PS_VERSION']); + } + + public function test_on_version_in_init_decides_the_namespace_import_page_loads(): void + { + $suite = new class (loadGlobals: true, getBrowser: false) extends TestsSuite { + public function init() + { + $this->onVersion('8.2.0'); + $this->importPage('FrontOffice\Home'); + + return $this; + } + }; + + $suite->init(); + + $this->assertInstanceOf( + \PrestaFlow\Library\Pages\v8\FrontOffice\Home\Page::class, + $suite->pages['frontOfficeHomePage'] + ); + } +}