From 41f6b3ef095b0558556e61ed22f3c9261edc4704 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Mon, 30 Sep 2024 22:33:13 +0200 Subject: [PATCH] NEW tests --- .../class-test-endpoint-template.php | 28 +++++- .../endpoints/class-test-endpoint-type.php | 93 +++++++++++++++++++ 2 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 test/unit/php/includes/core/classes/endpoints/class-test-endpoint-type.php diff --git a/test/unit/php/includes/core/classes/endpoints/class-test-endpoint-template.php b/test/unit/php/includes/core/classes/endpoints/class-test-endpoint-template.php index 51c345824..37d4b95e3 100644 --- a/test/unit/php/includes/core/classes/endpoints/class-test-endpoint-template.php +++ b/test/unit/php/includes/core/classes/endpoints/class-test-endpoint-template.php @@ -27,20 +27,38 @@ class Test_Endpoint_Template extends Base { * @return void */ public function test___construct(): void { - $slug = 'custom-endpoint'; + $slug = 'endpoint-template'; $callback = function () { return array( 'file_name' => 'endpoint-template.php', 'dir_path' => '/path/to/theme', ); }; + // Create a mock for Endpoint. + $endpoint_template = new Endpoint_Template( $slug, $callback ); + + $this->assertIsString( Utility::get_hidden_property( $endpoint_template, 'plugin_template_dir' ) ); + $this->assertNotEmpty( Utility::get_hidden_property( $endpoint_template, 'plugin_template_dir' ) ); + $this->assertSame( + sprintf( + '%s/includes/templates/endpoints', + GATHERPRESS_CORE_PATH + ), + Utility::get_hidden_property( $endpoint_template, 'plugin_template_dir' ), + 'Failed to assert, plugin_template_dir is set to fallback directory.' + ); + $plugin_default = '/mock/plugin/templates'; - $template_default = '/default/template.php'; + // $template_default = '/default/template.php'; - // Create a mock for Endpoint_Template. - $instance = new Endpoint_Template( $slug, $callback, $plugin_default ); + // Create a mock for Endpoint. + $endpoint_template = new Endpoint_Template( $slug, $callback, $plugin_default ); - $this->assertInstanceOf( Endpoint_Template::class, $instance ); + $this->assertSame( + '/mock/plugin/templates', + Utility::get_hidden_property( $endpoint_template, 'plugin_template_dir' ), + 'Failed to assert, plugin_template_dir is set to test directory.' + ); } /** diff --git a/test/unit/php/includes/core/classes/endpoints/class-test-endpoint-type.php b/test/unit/php/includes/core/classes/endpoints/class-test-endpoint-type.php new file mode 100644 index 000000000..ccce9b9cb --- /dev/null +++ b/test/unit/php/includes/core/classes/endpoints/class-test-endpoint-type.php @@ -0,0 +1,93 @@ + 'endpoint-template.php', + 'dir_path' => '/path/to/theme', + ); + }; + + // Create a mock for Endpoint. + $endpoint_template = new Endpoint_Template( $slug, $callback ); + + $this->assertIsString( $endpoint_template->slug ); + $this->assertIsCallable( Utility::get_hidden_property( $endpoint_template, 'callback' ) ); + } + + /** + * Coverage for is_of_class method. + * + * @covers ::is_of_class + * + * @return void + */ + public function test_is_of_class(): void { + $instance = new Endpoint_Redirect( 'slug', function(){} ); + + $this->assertTrue( + Utility::invoke_hidden_method( $instance, 'is_of_class', array( 'GatherPress\Core\Endpoints\Endpoint_Redirect' ) ), + 'Failed to validate class in namespace.' + ); + + $this->assertFalse( + Utility::invoke_hidden_method( $instance, 'is_of_class', array( 'GatherPress\Core\Endpoints\Endpoint_Template' ) ), + 'Failed to validate non-used class in namespace.' + ); + } + + /** + * Coverage for is_in_class method. + * + * @covers ::is_in_class + * + * @return void + */ + public function test_is_in_class(): void { + $instance = new Endpoint_Redirect( 'slug', function(){} ); + + $this->assertTrue( + Utility::invoke_hidden_method( $instance, 'is_in_class', array( 'GatherPress\Core\Endpoints\Endpoint_Redirect' ) ), + 'Failed to validate class in namespace.' + ); + + $this->assertTrue( + Utility::invoke_hidden_method( $instance, 'is_in_class', array( 'GatherPress\Core\Endpoints\Endpoint_Template' ) ), + 'Failed to validate class in namespace.' + ); + + $this->assertFalse( + Utility::invoke_hidden_method( $instance, 'is_in_class', array( 'WP_Post' ) ), + 'Failed to validate class is not in namespace.' + ); + } +}