-
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.
Merge pull request #702 from godaddy-wordpress/chore/add-new-traits
Add `CanGetNewInstanceTrait`
- Loading branch information
Showing
6 changed files
with
117 additions
and
3 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,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; | ||
} | ||
}; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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
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