From 604225c3c8fa816219f1420733cb270648c2bddd Mon Sep 17 00:00:00 2001 From: Nabeel Molham <72820458+nmolham-godaddy@users.noreply.github.com> Date: Fri, 23 Aug 2024 20:37:49 +1000 Subject: [PATCH] Add IsSingletonTrait (#700) * Define IsSingletonTrait * Add IsSingletonTrait methods * Unit test IsSingletonTrait * Add changelog entry * Check if trait exists rather than class --------- Co-authored-by: Ashley Gibson <99189195+agibson-godaddy@users.noreply.github.com> --- tests/bootstrap.php | 1 + tests/unit/Traits/IsSingletonTraitTest.php | 78 ++++++++++++++++++++++ woocommerce/Traits/IsSingletonTrait.php | 67 +++++++++++++++++++ woocommerce/changelog.txt | 1 + 4 files changed, 147 insertions(+) create mode 100644 tests/unit/Traits/IsSingletonTraitTest.php create mode 100644 woocommerce/Traits/IsSingletonTrait.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b846bcfaf..6cd225bdb 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,6 +9,7 @@ require_once PLUGIN_ROOT_DIR.'/woocommerce/class-sv-wc-plugin-exception.php'; require_once PLUGIN_ROOT_DIR.'/woocommerce/Enums/Traits/EnumTrait.php'; require_once PLUGIN_ROOT_DIR.'/woocommerce/Enums/PaymentFormContext.php'; +require_once PLUGIN_ROOT_DIR.'/woocommerce/Traits/IsSingletonTrait.php'; WP_Mock::setUsePatchwork(true); WP_Mock::bootstrap(); diff --git a/tests/unit/Traits/IsSingletonTraitTest.php b/tests/unit/Traits/IsSingletonTraitTest.php new file mode 100644 index 000000000..82f0781c2 --- /dev/null +++ b/tests/unit/Traits/IsSingletonTraitTest.php @@ -0,0 +1,78 @@ +singleton = new TestSingleton(); + + $this->setInaccessiblePropertyValue($this->singleton, 'instance', $this->singleton); + } + + /** + * Tests that it can determine whether an instance is loaded or not. + * + * @covers \SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits\IsSingletonTrait::isLoaded() + */ + public function testCanCheckIfIsLoaded() : void + { + self::assertTrue($this->singleton::isLoaded()); + + $this->singleton::reset(); + + self::assertFalse($this->singleton::isLoaded()); + } + + /** + * Tests that it can initialize and return an instance of self. + * + * @covers \SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits\IsSingletonTrait::getInstance() + */ + public function testCanGetInstance() : void + { + TestSingleton::reset(); + + $instance = TestSingleton::getInstance(); + + $this->assertInstanceOf(TestSingleton::class, $instance); + $this->assertSame($instance, TestSingleton::getInstance()); + } + + /** + * Tests that an instance can be reset. + * + * @covers \SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits\IsSingletonTrait::reset() + */ + public function testCanBeReset() : void + { + $this->singleton::reset(); + + $singleton = new ReflectionClass($this->singleton); + $instance = $singleton->getProperty('instance'); + $instance->setAccessible(true); + + self::assertNull($instance->getValue()); + } +} + +/** Dummy class for testing {@see IsSingletonTrait} */ +final class TestSingleton +{ + use IsSingletonTrait; +} diff --git a/woocommerce/Traits/IsSingletonTrait.php b/woocommerce/Traits/IsSingletonTrait.php new file mode 100644 index 000000000..4d4e93ed6 --- /dev/null +++ b/woocommerce/Traits/IsSingletonTrait.php @@ -0,0 +1,67 @@ +