Skip to content

Commit

Permalink
Add IsSingletonTrait (#700)
Browse files Browse the repository at this point in the history
* 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
nmolham-godaddy and agibson-godaddy authored Aug 23, 2024
1 parent 7cf0efe commit 604225c
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
78 changes: 78 additions & 0 deletions tests/unit/Traits/IsSingletonTraitTest.php
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;
}
67 changes: 67 additions & 0 deletions woocommerce/Traits/IsSingletonTrait.php
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;
}
}
1 change: 1 addition & 0 deletions woocommerce/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** SkyVerge WooCommerce Plugin Framework Changelog ***

2024.nn.nn - version 5.13.1-dev.1
* Feature - Add a trait to get a singular instance of a class IsSingletonTrait

2024.08.21 - version 5.13.0
* Feature - Add a trait for enum-like classes
Expand Down

0 comments on commit 604225c

Please sign in to comment.