diff --git a/.wp-env.json b/.wp-env.json index 1f957a7..1c12dd9 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -1,9 +1,10 @@ { - "core": null, - "phpVersion": "8.0", + "core": "WordPress/WordPress", + "phpVersion": "8.1", "plugins": [ "." ], "env": { "tests": { + "phpVersion": "8.1", "config": { "FS_METHOD": "direct", "WP_DEBUG": true, diff --git a/custom-post-types-taxonomies.php b/custom-post-types-taxonomies.php index ba7c929..c512737 100644 --- a/custom-post-types-taxonomies.php +++ b/custom-post-types-taxonomies.php @@ -23,6 +23,7 @@ declare( strict_types=1 ); +use Custom_PTT\Container_Singleton; use Custom_PTT\Plugin_Factory; if ( ! defined( 'ABSPATH' ) ) { @@ -65,11 +66,7 @@ * @throws Exception */ function bootstrap_plugin(): void { - - $container_builder = new DI\ContainerBuilder(); - $container_builder->addDefinitions( __DIR__ . '/config/di-config.php' ); - $container = $container_builder->build(); - + $container = Container_Singleton::get_instance(); Plugin_Factory::create( $container ) ->boot(); } diff --git a/includes/Container_Singleton.php b/includes/Container_Singleton.php new file mode 100644 index 0000000..30f6a96 --- /dev/null +++ b/includes/Container_Singleton.php @@ -0,0 +1,52 @@ +addDefinitions( __DIR__ . '/../config/di-config.php' ); + static::$container = $container_builder->build(); + } + + return static::$container; + } +} diff --git a/tests/unit/Container_Singleton_Test.php b/tests/unit/Container_Singleton_Test.php new file mode 100644 index 0000000..219fbd0 --- /dev/null +++ b/tests/unit/Container_Singleton_Test.php @@ -0,0 +1,71 @@ +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(); + } +} diff --git a/tests/unit/Plugin_Test.php b/tests/unit/Plugin_Test.php index dc223f5..56533cd 100644 --- a/tests/unit/Plugin_Test.php +++ b/tests/unit/Plugin_Test.php @@ -4,22 +4,108 @@ namespace Custom_PTT\Tests\Unit; +use Custom_PTT\Container_Singleton; use Custom_PTT\Plugin; +use Custom_PTT\Plugin_Factory; use DI\Container; +use DI\DependencyException; +use DI\NotFoundException; +use Exception; use WP_UnitTestCase; +/** + * Class Plugin_Test + * + * @package Custom_PTT\Tests\Unit + * @since 0.1.0-alpha + */ class Plugin_Test extends WP_UnitTestCase { - private Plugin $plugin; + /** + * Container instance. + * + * @var Container + * + * @since 0.2.1 + */ private Container $container; + /** + * Plugin instance. + * + * @var Plugin + * + * @since 0.2.1 + */ + private Plugin $plugin; + + /** + * Set up + * + * @return void + * + * @throws Exception + * @since 0.1.0-alpha + */ public function setUp(): void { parent::setUp(); - $this->container = $this->createMock( Container::class ); - $this->plugin = new Plugin( $this->container ); + $this->container = Container_Singleton::get_instance(); + $this->plugin = Plugin_Factory::create( $this->container ); + } + + /** + * Test plugin instance is created + * + * @return void + * + * @since 0.2.1 + */ + public function test_plugin_instance_is_created() { + $this->assertInstanceOf( Plugin::class, $this->plugin ); + } + + /** + * Test boot register activation and deactivation hooks + * + * @throws Exception + * + * @since 0.2.1 + */ + public function test_boot_method_registers_hooks() { + + $this->plugin->boot(); + + $file = plugin_basename( CUSTOM_PTT_FILE ); + + $this->assertEquals( 10, has_action( 'activate_' . $file, array( $this->plugin, 'on_activation' ) ) ); + $this->assertEquals( 10, has_action( 'deactivate_' . $file, array( $this->plugin, 'on_deactivation' ) ) ); + } + + /** + * Test all services are registered + * + * @since 0.2.1 + */ + public function test_register_services_method_registers_services() { + $services = $this->container->getKnownEntryNames(); + + if ( empty( $services ) ) { + $this->fail( 'No services found' ); + } + + foreach ( $services as $service ) { + $this->assertTrue( $this->container->has( $service ) ); + } } + /** + * Tear down + * + * @return void + * + * @since 0.1.0-alpha + */ public function tearDown(): void { parent::tearDown(); } diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 425f2c9..37ef392 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -1,21 +1,34 @@