Skip to content

Commit

Permalink
Merge pull request #702 from godaddy-wordpress/chore/add-new-traits
Browse files Browse the repository at this point in the history
Add `CanGetNewInstanceTrait`
  • Loading branch information
nmolham-godaddy authored Aug 26, 2024
2 parents 604225c + 6f266df commit 7145b7e
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 3 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/CanGetNewInstanceTrait.php';
require_once PLUGIN_ROOT_DIR.'/woocommerce/Traits/IsSingletonTrait.php';

WP_Mock::setUsePatchwork(true);
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/Traits/CanGetNewInstanceTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace SkyVerge\WooCommerce\PluginFramework\v5_13_1\Tests\Unit\Traits;

use SkyVerge\WooCommerce\PluginFramework\v5_13_1\Tests\TestCase;
use SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits\CanGetNewInstanceTrait;

class CanGetNewInstanceTraitTest extends TestCase
{
/**
* Tests that it can get new instance with arguments.
*
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits\CanGetNewInstanceTrait::getNewInstance()
*/
public function testItCanGetNewInstanceWithArgs() : void
{
$class = $this->getTestClass();
$newInstance = $class::getNewInstance('value1', 'value2');

$this->assertSame('value1', $newInstance->arg1);
$this->assertSame('value2', $newInstance->arg2);
$this->assertInstanceOf(get_class($class), $newInstance);
}

/**
* Tests that it can get new instance without arguments.
*
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_13_1\Traits\CanGetNewInstanceTrait::getNewInstance()
*/
public function testItCanGetNewInstanceWithoutArgs() : void
{
$class = $this->getTestClass();
$newInstance = $class::getNewInstance();

$this->assertNull($newInstance->arg1);
$this->assertNull($newInstance->arg2);
$this->assertInstanceOf(get_class($class), $newInstance);
}

/**
* Anonymous Class for Testing Trait.
*
* @see testItCanGetNewInstance
*/
private function getTestClass()
{
return new class {
use CanGetNewInstanceTrait;

public ?string $arg1 = null;
public ?string $arg2 = null;

public function __construct(?string $arg1 = null, ?string $arg2 = null)
{
$this->arg1 = $arg1;
$this->arg2 = $arg2;
}
};
}
}
48 changes: 48 additions & 0 deletions woocommerce/Traits/CanGetNewInstanceTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\\CanGetNewInstanceTrait')) {
return;
}

/**
* A trait that allows a given class/object to get a new instance of itself.
* For singletons {@see CanGetNewInstanceTrait} instead.
*/
trait CanGetNewInstanceTrait
{
/**
* Creates and returns a new instance of the calling class.
*
* @return static
*/
public static function getNewInstance(...$args)
{
return new static(...$args);
}
}
4 changes: 2 additions & 2 deletions woocommerce/Traits/IsSingletonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public static function isLoaded() : bool
*
* @return static
*/
public static function getInstance()
public static function getInstance(...$args)
{
return static::$instance ??= new static(...func_get_args());
return static::$instance ??= new static(...$args);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion woocommerce/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
*** 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
* Feature - Add a trait to get a singular instance of a class: `IsSingletonTrait`
* Feature - Add a trait to get an instance (non-singular) of a class: `CanGetNewInstanceTrait`

2024.08.21 - version 5.13.0
* Feature - Add a trait for enum-like classes
Expand Down
4 changes: 4 additions & 0 deletions woocommerce/class-sv-wc-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ private function includes() {
// common exception class
require_once( $framework_path . '/class-sv-wc-plugin-exception.php' );

// traits
require_once( $framework_path . '/Traits/CanGetNewInstanceTrait.php' );
require_once( $framework_path . '/Traits/IsSingletonTrait.php' );

// addresses
require_once( $framework_path . '/Addresses/Address.php' );
require_once( $framework_path . '/Addresses/Customer_Address.php' );
Expand Down

0 comments on commit 7145b7e

Please sign in to comment.