-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Define IsSingletonTrait * Add IsSingletonTrait methods * Unit test IsSingletonTrait * Add changelog entry * Check if trait exists rather than class --------- Co-authored-by: Ashley Gibson <[email protected]>
- Loading branch information
1 parent
7cf0efe
commit 604225c
Showing
4 changed files
with
147 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace SkyVerge\WooCommerce\PluginFramework\v5_13_1\Tests\Unit\Traits; | ||
|
||
use Exception; | ||
use ReflectionClass; | ||
use SkyVerge\WooCommerce\PluginFramework\v5_13_1\Tests\TestCase; | ||
use SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits\IsSingletonTrait; | ||
|
||
class IsSingletonTraitTest extends TestCase | ||
{ | ||
protected TestSingleton $singleton; | ||
|
||
/** | ||
* Runs a script for every test in this set. | ||
* | ||
* @throws Exception | ||
*/ | ||
public function setUp() : void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->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; | ||
} |
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,67 @@ | ||
<?php | ||
/** | ||
* WooCommerce Plugin Framework | ||
* | ||
* This source file is subject to the GNU General Public License v3.0 | ||
* that is bundled with this package in the file license.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://www.gnu.org/licenses/gpl-3.0.html | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so we can send you a copy immediately. | ||
* | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade the plugin to newer | ||
* versions in the future. If you wish to customize the plugin for your | ||
* needs please refer to http://www.skyverge.com | ||
* | ||
* @package SkyVerge/WooCommerce/Plugin/Classes | ||
* @author SkyVerge | ||
* @copyright Copyright (c) 2013-2024, SkyVerge, Inc. | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 | ||
*/ | ||
|
||
namespace SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits; | ||
|
||
defined('ABSPATH') or exit; | ||
|
||
if (trait_exists('\\SkyVerge\\WooCommerce\\PluginFramework\\v5_13_1\\Traits\\IsSingletonTrait')) { | ||
return; | ||
} | ||
|
||
trait IsSingletonTrait | ||
{ | ||
/** @var ?static holds the current singleton instance */ | ||
protected static $instance; | ||
|
||
/** | ||
* Determines if the current instance is loaded. | ||
* | ||
* @return bool | ||
*/ | ||
public static function isLoaded() : bool | ||
{ | ||
return (bool) static::$instance; | ||
} | ||
|
||
/** | ||
* Gets the singleton instance. | ||
* | ||
* @return static | ||
*/ | ||
public static function getInstance() | ||
{ | ||
return static::$instance ??= new static(...func_get_args()); | ||
} | ||
|
||
/** | ||
* Resets the singleton instance. | ||
* | ||
* @return void | ||
*/ | ||
public static function reset() : void | ||
{ | ||
static::$instance = null; | ||
} | ||
} |
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