-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a Container Singleton, + unit tests
- Loading branch information
1 parent
5c62e6b
commit 8094289
Showing
6 changed files
with
235 additions
and
15 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
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,52 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace Custom_PTT; | ||
|
||
use DI\Container; | ||
use DI\ContainerBuilder; | ||
use Exception; | ||
|
||
/** | ||
* Container_Singleton class. | ||
* | ||
* This class is a placeholder for a singleton container instance. | ||
* | ||
* @package Custom_PTT | ||
* @since 0.2.1 | ||
*/ | ||
class Container_Singleton { | ||
/** | ||
* The Container instance. | ||
* | ||
* @var Container|null | ||
* | ||
* @since 0.2.1 | ||
*/ | ||
private static ?Container $container = null; | ||
|
||
/** | ||
* Prevent class from being instantiated. | ||
* @since 0.2.1 | ||
*/ | ||
private function __construct() {} | ||
|
||
/** | ||
* Get the Container instance. | ||
* | ||
* @return Container | ||
* @throws Exception | ||
* | ||
* @since 0.2.1 | ||
*/ | ||
public static function get_instance(): Container { | ||
if ( null === static::$container ) { | ||
$container_builder = new ContainerBuilder(); | ||
$container_builder->addDefinitions( __DIR__ . '/../config/di-config.php' ); | ||
static::$container = $container_builder->build(); | ||
} | ||
|
||
return static::$container; | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace Custom_PTT\Tests\Unit; | ||
|
||
use Custom_PTT\Container_Singleton; | ||
use DI\Container; | ||
use Exception; | ||
use ReflectionClass; | ||
use WP_UnitTestCase; | ||
|
||
/** | ||
* Class Container_Singleton_Test | ||
* | ||
* @package Custom_PTT\Tests\Unit | ||
* @since 0.2.1 | ||
*/ | ||
class Container_Singleton_Test extends WP_UnitTestCase { | ||
|
||
/** | ||
* Resets the container instance before each test, as it's created on plugin bootstrap | ||
* | ||
* @return void | ||
* | ||
* @since 0.2.1 | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$reflection = new ReflectionClass( Container_Singleton::class ); | ||
$container = $reflection->getProperty( 'container' ); | ||
$container->setAccessible( true ); | ||
$container->setValue( null, null ); | ||
} | ||
|
||
/** | ||
* Test that get_instance returns a Container instance. | ||
* | ||
* @throws Exception | ||
* | ||
* @since 0.2.1 | ||
*/ | ||
public function test_get_instance_returns_container_instance() { | ||
$instance = Container_Singleton::get_instance(); | ||
$this->assertInstanceOf( Container::class, $instance ); | ||
} | ||
|
||
/** | ||
* Test that get_instance always returns the same instance. | ||
* | ||
* @throws Exception | ||
* | ||
* @since 0.2.1 | ||
*/ | ||
public function test_get_instance_returns_same_instance() { | ||
$first_call_instance = Container_Singleton::get_instance(); | ||
$second_call_instance = Container_Singleton::get_instance(); | ||
|
||
$this->assertSame( $first_call_instance, $second_call_instance ); | ||
} | ||
|
||
/** | ||
* @return void | ||
* | ||
* @since 0.2.1 | ||
*/ | ||
public function tearDown(): void { | ||
parent::tearDown(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,21 +1,34 @@ | ||
<?php | ||
|
||
|
||
define( 'TESTS_PLUGIN_DIR', dirname( __DIR__, 2 ) ); | ||
$_test_root = getenv( 'WP_TESTS_DIR' ); | ||
|
||
if ( false === getenv( 'WP_TESTS_DIR' ) ) { | ||
exit( 'WP_TESTS_DIR environment variable is not set. Please set it to the root of your WordPress installation.' ); | ||
} | ||
|
||
define( 'TESTS_PLUGIN_DIR', dirname( __DIR__, 2 ) ); | ||
|
||
if ( ! file_exists( TESTS_PLUGIN_DIR . '/vendor/autoload.php' ) ) { | ||
exit( 'The vendor directory is missing. Please run composer install.' ); | ||
} | ||
|
||
if ( ! file_exists( $_test_root . '/includes/bootstrap.php' ) ) { | ||
exit( 'Could not load /includes/bootstrap.php' ); | ||
} | ||
|
||
require TESTS_PLUGIN_DIR . '/vendor/autoload.php'; | ||
|
||
$_test_root = getenv( 'WP_TESTS_DIR' ); | ||
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', dirname( __DIR__, 2 ) . '/vendor/yoast/phpunit-polyfills' ); | ||
|
||
if ( ! file_exists( $_test_root . '/includes/bootstrap.php' ) ) { | ||
exit( 'Could not load /includes/bootstrap.php' ); | ||
require_once $_test_root . '/includes/functions.php'; | ||
|
||
/** | ||
* Manually load the plugin being tested. | ||
*/ | ||
function _manually_load_plugin(): void { | ||
require dirname( __DIR__, 2 ) . '/custom-post-types-taxonomies.php'; | ||
} | ||
|
||
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); | ||
|
||
require $_test_root . '/includes/bootstrap.php'; |