diff --git a/includes/MslsBlock.php b/includes/MslsBlock.php index 70fdc57f..c019cb2f 100644 --- a/includes/MslsBlock.php +++ b/includes/MslsBlock.php @@ -4,7 +4,6 @@ class MslsBlock { - protected MslsOptions $options; public function __construct( MslsOptions $options ) { diff --git a/tests/phpunit/Component/Icon/TestIconPng.php b/tests/phpunit/Component/Icon/TestIconPng.php index 5255bc42..19a0e32b 100644 --- a/tests/phpunit/Component/Icon/TestIconPng.php +++ b/tests/phpunit/Component/Icon/TestIconPng.php @@ -6,7 +6,7 @@ use lloc\Msls\Component\Icon\IconPng; use lloc\MslsTests\MslsUnitTestCase; -class TestIconPng extends MslsUnitTestCase { +final class TestIconPng extends MslsUnitTestCase { public function test_get(): void { Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 4 ) . '/' ); diff --git a/tests/phpunit/Component/Icon/TestIconSvg.php b/tests/phpunit/Component/Icon/TestIconSvg.php index 3c6f3224..03c71dd8 100644 --- a/tests/phpunit/Component/Icon/TestIconSvg.php +++ b/tests/phpunit/Component/Icon/TestIconSvg.php @@ -6,7 +6,7 @@ use lloc\Msls\Component\Icon\IconSvg; use lloc\MslsTests\MslsUnitTestCase; -class TestIconSvg extends MslsUnitTestCase { +final class TestIconSvg extends MslsUnitTestCase { public function test_get(): void { Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 4 ) . '/' ); diff --git a/tests/phpunit/ContentImport/Importers/Attachments/TestLinking.php b/tests/phpunit/ContentImport/Importers/Attachments/TestLinking.php index 2fa777b6..95d03c9b 100644 --- a/tests/phpunit/ContentImport/Importers/Attachments/TestLinking.php +++ b/tests/phpunit/ContentImport/Importers/Attachments/TestLinking.php @@ -7,7 +7,7 @@ use lloc\Msls\ContentImport\Importers\Attachments\Linking; use lloc\MslsTests\MslsUnitTestCase; -class TestLinking extends MslsUnitTestCase { +final class TestLinking extends MslsUnitTestCase { public function testImport(): void { $coordinates = \Mockery::mock( ImportCoordinates::class ); diff --git a/tests/phpunit/ContentImport/Importers/PostFields/TestDuplicating.php b/tests/phpunit/ContentImport/Importers/PostFields/TestDuplicating.php index 056513a7..600c11e2 100644 --- a/tests/phpunit/ContentImport/Importers/PostFields/TestDuplicating.php +++ b/tests/phpunit/ContentImport/Importers/PostFields/TestDuplicating.php @@ -7,7 +7,7 @@ use lloc\Msls\ContentImport\Importers\PostFields\Duplicating; use lloc\MslsTests\MslsUnitTestCase; -class TestDuplicating extends MslsUnitTestCase { +final class TestDuplicating extends MslsUnitTestCase { public function testImport(): void { Functions\expect( 'wp_insert_post' )->once(); diff --git a/tests/phpunit/ContentImport/Importers/PostMeta/TestDuplicating.php b/tests/phpunit/ContentImport/Importers/PostMeta/TestDuplicating.php index 98a98929..6500d39f 100644 --- a/tests/phpunit/ContentImport/Importers/PostMeta/TestDuplicating.php +++ b/tests/phpunit/ContentImport/Importers/PostMeta/TestDuplicating.php @@ -9,7 +9,7 @@ use function Brain\Monkey\Functions; -class TestDuplicating extends MslsUnitTestCase { +final class TestDuplicating extends MslsUnitTestCase { public function testImport(): void { Functions\expect( 'switch_to_blog' )->twice(); diff --git a/tests/phpunit/ContentImport/Importers/TestAttachmentsImporters.php b/tests/phpunit/ContentImport/Importers/TestAttachmentsImporters.php index 3af28ae1..5dd35c50 100644 --- a/tests/phpunit/ContentImport/Importers/TestAttachmentsImporters.php +++ b/tests/phpunit/ContentImport/Importers/TestAttachmentsImporters.php @@ -5,7 +5,7 @@ use lloc\Msls\ContentImport\Importers\AttachmentsImporters; use lloc\MslsTests\MslsUnitTestCase; -class TestAttachmentsImporters extends MslsUnitTestCase { +final class TestAttachmentsImporters extends MslsUnitTestCase { public function testDetails(): void { diff --git a/tests/phpunit/ContentImport/Importers/TestBaseImporter.php b/tests/phpunit/ContentImport/Importers/TestBaseImporter.php index 2acc5a6b..fbf79787 100644 --- a/tests/phpunit/ContentImport/Importers/TestBaseImporter.php +++ b/tests/phpunit/ContentImport/Importers/TestBaseImporter.php @@ -8,34 +8,44 @@ use lloc\Msls\ContentImport\Relations; use lloc\MslsTests\MslsUnitTestCase; -class TestBaseImporter extends MslsUnitTestCase { - - public function setUp(): void { - parent::setUp(); +final class TestBaseImporter extends MslsUnitTestCase { + private function BaseImporterFactory(): BaseImporter { $import_coordinates = \Mockery::mock( ImportCoordinates::class ); - $this->test = new BaseImporter( $import_coordinates ); + + return new BaseImporter( $import_coordinates ); } public function testImport(): void { - $this->assertEquals( array(), $this->test->import( array() ) ); + $test = $this->BaseImporterFactory(); + + $this->assertEquals( array(), $test->import( array() ) ); } public function testSetImportCoordinates(): void { $import_coordinates = \Mockery::mock( ImportCoordinates::class ); $this->expectNotToPerformAssertions(); - $this->test->set_import_coordinates( $import_coordinates ); + + $test = $this->BaseImporterFactory(); + + $test->set_import_coordinates( $import_coordinates ); } public function testGetLogger(): void { - $this->assertInstanceOf( ImportLogger::class, $this->test->get_logger() ); + $test = $this->BaseImporterFactory(); + + $this->assertInstanceOf( ImportLogger::class, $test->get_logger() ); } public function testGetRelations(): void { - $this->assertInstanceOf( Relations::class, $this->test->get_relations() ); + $test = $this->BaseImporterFactory(); + + $this->assertInstanceOf( Relations::class, $test->get_relations() ); } public function testInfo(): void { - $this->assertInstanceOf( \stdClass::class, $this->test->info() ); + $test = $this->BaseImporterFactory(); + + $this->assertInstanceOf( \stdClass::class, $test->info() ); } } diff --git a/tests/phpunit/ContentImport/Importers/TestImportersBaseFactory.php b/tests/phpunit/ContentImport/Importers/TestImportersBaseFactory.php index 92fbab14..f932615d 100644 --- a/tests/phpunit/ContentImport/Importers/TestImportersBaseFactory.php +++ b/tests/phpunit/ContentImport/Importers/TestImportersBaseFactory.php @@ -7,7 +7,7 @@ use lloc\MslsTests\MslsUnitTestCase; use Mockery\Mock; -class TestImportersBaseFactory extends MslsUnitTestCase { +final class TestImportersBaseFactory extends MslsUnitTestCase { public function testMake(): void { $coordinates = \Mockery::mock( ImportCoordinates::class ); diff --git a/tests/phpunit/ContentImport/Importers/TestMap.php b/tests/phpunit/ContentImport/Importers/TestMap.php index 93db70e7..545d439d 100644 --- a/tests/phpunit/ContentImport/Importers/TestMap.php +++ b/tests/phpunit/ContentImport/Importers/TestMap.php @@ -6,7 +6,7 @@ use lloc\Msls\ContentImport\Importers\Map; use lloc\MslsTests\MslsUnitTestCase; -class TestMap extends MslsUnitTestCase { +final class TestMap extends MslsUnitTestCase { public function testMake(): void { $coordinates = \Mockery::mock( ImportCoordinates::class ); diff --git a/tests/phpunit/ContentImport/Importers/TestPostFieldsImporters.php b/tests/phpunit/ContentImport/Importers/TestPostFieldsImporters.php index fa3dcb86..c761e0d2 100644 --- a/tests/phpunit/ContentImport/Importers/TestPostFieldsImporters.php +++ b/tests/phpunit/ContentImport/Importers/TestPostFieldsImporters.php @@ -8,7 +8,7 @@ use lloc\Msls\ContentImport\Importers\PostFieldsImporters; use lloc\MslsTests\MslsUnitTestCase; -class TestPostFieldsImporters extends MslsUnitTestCase { +final class TestPostFieldsImporters extends MslsUnitTestCase { public function testMake(): void { $importer = \Mockery::mock( Duplicating::class ); diff --git a/tests/phpunit/ContentImport/Importers/TestPostMetaImporters.php b/tests/phpunit/ContentImport/Importers/TestPostMetaImporters.php index 94cbb1fb..1b79f484 100644 --- a/tests/phpunit/ContentImport/Importers/TestPostMetaImporters.php +++ b/tests/phpunit/ContentImport/Importers/TestPostMetaImporters.php @@ -5,7 +5,7 @@ use lloc\Msls\ContentImport\Importers\PostMetaImporters; use lloc\MslsTests\MslsUnitTestCase; -class TestPostMetaImporters extends MslsUnitTestCase { +final class TestPostMetaImporters extends MslsUnitTestCase { public function testDetails(): void { $test = new PostMetaImporters(); diff --git a/tests/phpunit/ContentImport/Importers/TestPostThumbnailImporters.php b/tests/phpunit/ContentImport/Importers/TestPostThumbnailImporters.php index 42cabf56..9fc8f433 100644 --- a/tests/phpunit/ContentImport/Importers/TestPostThumbnailImporters.php +++ b/tests/phpunit/ContentImport/Importers/TestPostThumbnailImporters.php @@ -5,7 +5,7 @@ use lloc\Msls\ContentImport\Importers\PostThumbnailImporters; use lloc\MslsTests\MslsUnitTestCase; -class TestPostThumbnailImporters extends MslsUnitTestCase { +final class TestPostThumbnailImporters extends MslsUnitTestCase { public function testDetails(): void { $test = new PostThumbnailImporters(); diff --git a/tests/phpunit/ContentImport/Importers/TestTermsImporters.php b/tests/phpunit/ContentImport/Importers/TestTermsImporters.php index e0b9a21a..495d5820 100644 --- a/tests/phpunit/ContentImport/Importers/TestTermsImporters.php +++ b/tests/phpunit/ContentImport/Importers/TestTermsImporters.php @@ -5,7 +5,7 @@ use lloc\Msls\ContentImport\Importers\TermsImporters; use lloc\MslsTests\MslsUnitTestCase; -class TestTermsImporters extends MslsUnitTestCase { +final class TestTermsImporters extends MslsUnitTestCase { public function testDetails(): void { $test = new TermsImporters(); diff --git a/tests/phpunit/ContentImport/LogWriters/TestAdminNoticeLogger.php b/tests/phpunit/ContentImport/LogWriters/TestAdminNoticeLogger.php index d7665f02..038f3e4e 100644 --- a/tests/phpunit/ContentImport/LogWriters/TestAdminNoticeLogger.php +++ b/tests/phpunit/ContentImport/LogWriters/TestAdminNoticeLogger.php @@ -7,16 +7,12 @@ use lloc\Msls\ContentImport\LogWriters\AdminNoticeLogger; use lloc\MslsTests\MslsUnitTestCase; -class TestAdminNoticeLogger extends MslsUnitTestCase { - - public function setUp(): void { - parent::setUp(); - - $this->test = new AdminNoticeLogger(); - } +final class TestAdminNoticeLogger extends MslsUnitTestCase { public function testGetTransient(): void { - $this->assertEquals( 'msls_last_import_log', $this->test->get_transient() ); + $test = new AdminNoticeLogger(); + + $this->assertEquals( 'msls_last_import_log', $test->get_transient() ); } public function testWrite(): void { @@ -29,10 +25,12 @@ public function testWrite(): void { $coordinates->dest_blog_id = 2; $coordinates->dest_post_id = 13; - $this->test->set_import_coordinates( $coordinates ); + $test = new AdminNoticeLogger(); + + $test->set_import_coordinates( $coordinates ); $data = array( 'info', array( 'foo' ) ); - $this->test->write( $data ); + $test->write( $data ); $this->expectNotToPerformAssertions(); } diff --git a/tests/phpunit/ContentImport/TestAttachmentPathFinder.php b/tests/phpunit/ContentImport/TestAttachmentPathFinder.php index 5c5daa25..91c09036 100644 --- a/tests/phpunit/ContentImport/TestAttachmentPathFinder.php +++ b/tests/phpunit/ContentImport/TestAttachmentPathFinder.php @@ -6,13 +6,7 @@ use lloc\MslsTests\MslsUnitTestCase; use Brain\Monkey\Functions; -class TestAttachmentPathFinder extends MslsUnitTestCase { - - public function setUp(): void { - parent::setUp(); - - $this->test = new AttachmentPathFinder(); - } +final class TestAttachmentPathFinder extends MslsUnitTestCase { public static function dataprovider_filter_srcset(): array { $image_src = 'http://example.com/image.jpg'; @@ -42,7 +36,9 @@ public function test_filter_srcset( $source, $imageSrc, $attachmentId, $expected Functions\expect( 'delete_post_meta' )->times( $time_dpm ); Functions\expect( 'get_blog_post' )->times( $times_gbp )->andReturn( $blog_post ); - $this->assertEquals( $expected, $this->test->filter_srcset( $source, null, $imageSrc, null, $attachmentId ) ); + $test = new AttachmentPathFinder(); + + $this->assertEquals( $expected, $test->filter_srcset( $source, null, $imageSrc, null, $attachmentId ) ); } public static function dataprovider_filter_attachement_url(): array { @@ -71,6 +67,8 @@ public function test_filter_attachment_url( string $image_src, $source_post, int Functions\expect( 'delete_post_meta' )->zeroOrMoreTimes(); Functions\expect( 'get_blog_post' )->zeroOrMoreTimes()->andReturn( $source_post ); - $this->assertEquals( $source_post->guid, $this->test->filter_attachment_url( $image_src, $attachment_id ) ); + $test = new AttachmentPathFinder(); + + $this->assertEquals( $source_post->guid, $test->filter_attachment_url( $image_src, $attachment_id ) ); } } diff --git a/tests/phpunit/ContentImport/TestContentImporter.php b/tests/phpunit/ContentImport/TestContentImporter.php index 81cab160..a94d2430 100644 --- a/tests/phpunit/ContentImport/TestContentImporter.php +++ b/tests/phpunit/ContentImport/TestContentImporter.php @@ -9,36 +9,41 @@ use lloc\MslsTests\MslsUnitTestCase; use Brain\Monkey\Actions; -class TestContentImporter extends MslsUnitTestCase { - - - public function setUp(): void { - parent::setUp(); +final class TestContentImporter extends MslsUnitTestCase { + private function ContentImporterFactory(): ContentImporter { $main = \Mockery::mock( MslsMain::class ); $main->shouldReceive( 'verify_nonce' )->andReturnTrue(); - $this->test = new ContentImporter( $main ); + return new ContentImporter( $main ); } public function test_logger(): void { - $this->test->set_logger( \Mockery::mock( ImportLogger::class ) ); + $test = $this->ContentImporterFactory(); + + $test->set_logger( \Mockery::mock( ImportLogger::class ) ); - $this->assertInstanceOf( ImportLogger::class, $this->test->get_logger() ); + $this->assertInstanceOf( ImportLogger::class, $test->get_logger() ); } public function test_relations(): void { - $this->test->set_relations( \Mockery::mock( Relations::class ) ); + $test = $this->ContentImporterFactory(); - $this->assertInstanceOf( Relations::class, $this->test->get_relations() ); + $test->set_relations( \Mockery::mock( Relations::class ) ); + + $this->assertInstanceOf( Relations::class, $test->get_relations() ); } public function test_handle_import(): void { - $this->assertEquals( array(), $this->test->handle_import() ); + $test = $this->ContentImporterFactory(); + + $this->assertEquals( array(), $test->handle_import() ); } public function test_parse_sources_no_post(): void { - $this->assertFalse( $this->test->parse_sources() ); + $test = $this->ContentImporterFactory(); + + $this->assertFalse( $test->parse_sources() ); } public function test_handle_false(): void { @@ -46,7 +51,7 @@ public function test_handle_false(): void { Actions\expectAdded( 'msls_main_save' )->once(); - $this->test->handle( false ); + $this->ContentImporterFactory()->handle( false ); } public function test_handle_true(): void { @@ -54,6 +59,6 @@ public function test_handle_true(): void { Actions\expectRemoved( 'msls_main_save' )->once(); - $this->test->handle( true ); + $this->ContentImporterFactory()->handle( true ); } } diff --git a/tests/phpunit/ContentImport/TestImportCoordinates.php b/tests/phpunit/ContentImport/TestImportCoordinates.php index 79430cd1..44b56fb9 100644 --- a/tests/phpunit/ContentImport/TestImportCoordinates.php +++ b/tests/phpunit/ContentImport/TestImportCoordinates.php @@ -6,25 +6,25 @@ use lloc\MslsTests\MslsUnitTestCase; use Brain\Monkey\Functions; -class TestImportCoordinates extends MslsUnitTestCase { +final class TestImportCoordinates extends MslsUnitTestCase { + public function ImportCoordinatesFactory(): ImportCoordinates { + $test = new ImportCoordinates(); - public function setUp(): void { - parent::setUp(); + $test->source_blog_id = 1; + $test->source_post_id = 42; + $test->dest_blog_id = 2; + $test->dest_post_id = 13; + $test->source_post = \Mockery::mock( \WP_Post::class ); + $test->source_lang = 'de_DE'; + $test->dest_lang = 'it_IT'; - $this->test = new ImportCoordinates(); - - $this->test->source_blog_id = 1; - $this->test->source_post_id = 42; - $this->test->dest_blog_id = 2; - $this->test->dest_post_id = 13; - $this->test->source_post = \Mockery::mock( \WP_Post::class ); - $this->test->source_lang = 'de_DE'; - $this->test->dest_lang = 'it_IT'; + return $test; } public static function providerValidate(): array { $post = \Mockery::mock( \WP_Post::class ); + return array( array( null, null, null, null, null, false ), array( $post, null, null, null, null, false ), @@ -42,9 +42,10 @@ public function testValidate( $post_a, $post_b, $source_post, $lang_a, $lang_b, Functions\expect( 'get_blog_post' )->andReturn( $post_a, $post_b ); Functions\expect( 'get_blog_option' )->andReturn( $lang_a, $lang_b ); - $this->test->source_post = $source_post; + $test = $this->ImportCoordinatesFactory(); + $test->source_post = $source_post; - $this->assertEquals( $expected, $this->test->validate() ); + $this->assertEquals( $expected, $test->validate() ); } public function testParseImportersFromPost(): void { @@ -61,16 +62,20 @@ public function testParseImportersFromPost(): void { ->with( INPUT_GET, ImportCoordinates::IMPORTERS_GLOBAL_KEY, FILTER_FORCE_ARRAY ) ->andReturn( array( 'pagesType' => 'pagesSlug' ) ); - $this->assertNull( $this->test->get_importer_for( 'pagesType' ) ); + $test = $this->ImportCoordinatesFactory(); - $this->test->parse_importers_from_request(); + $this->assertNull( $test->get_importer_for( 'pagesType' ) ); - $this->assertEquals( 'pagesSlug', $this->test->get_importer_for( 'pagesType' ) ); + $test->parse_importers_from_request(); + + $this->assertEquals( 'pagesSlug', $test->get_importer_for( 'pagesType' ) ); } public function testSetImporterFor(): void { - $this->test->set_importer_for( 'postsType', 'postsSlug' ); + $test = $this->ImportCoordinatesFactory(); + + $test->set_importer_for( 'postsType', 'postsSlug' ); - $this->assertEquals( 'postsSlug', $this->test->get_importer_for( 'postsType' ) ); + $this->assertEquals( 'postsSlug', $test->get_importer_for( 'postsType' ) ); } } diff --git a/tests/phpunit/ContentImport/TestImportLogger.php b/tests/phpunit/ContentImport/TestImportLogger.php index bf36a840..0707fa7b 100644 --- a/tests/phpunit/ContentImport/TestImportLogger.php +++ b/tests/phpunit/ContentImport/TestImportLogger.php @@ -6,15 +6,7 @@ use lloc\Msls\ContentImport\ImportLogger; use lloc\MslsTests\MslsUnitTestCase; -class TestImportLogger extends MslsUnitTestCase { - - public function setUp(): void { - parent::setUp(); - - $coordinates = \Mockery::mock( ImportCoordinates::class ); - - $this->test = new ImportLogger( $coordinates ); - } +final class TestImportLogger extends MslsUnitTestCase { public static function provider_get_data(): array { return array( @@ -28,6 +20,9 @@ public static function provider_get_data(): array { * @dataProvider provider_get_data */ public function test_get_data( $key ): void { - $this->assertArrayHasKey( $key, $this->test->get_data() ); + $coordinates = \Mockery::mock( ImportCoordinates::class ); + $test = new ImportLogger( $coordinates ); + + $this->assertArrayHasKey( $key, $test->get_data() ); } } diff --git a/tests/phpunit/ContentImport/TestMetaBox.php b/tests/phpunit/ContentImport/TestMetaBox.php index e7f79684..b9a45d35 100644 --- a/tests/phpunit/ContentImport/TestMetaBox.php +++ b/tests/phpunit/ContentImport/TestMetaBox.php @@ -4,17 +4,29 @@ use lloc\Msls\ContentImport\MetaBox; use lloc\MslsTests\MslsUnitTestCase; +use Brain\Monkey\Functions; -class TestMetaBox extends MslsUnitTestCase { +final class TestMetaBox extends MslsUnitTestCase { + public function test_render(): void { + $post = \Mockery::mock( 'WP_Post' ); + $post->ID = 1; - public function setUp(): void { - parent::setUp(); + Functions\expect( 'get_post' )->once()->andReturn( $post ); + Functions\expect( 'get_option' )->twice()->andReturn( array() ); + Functions\expect( 'get_available_languages' )->once()->andReturn( array() ); + Functions\expect( 'get_current_blog_id' )->once()->andReturn( 1 ); + Functions\expect( 'get_blog_option' )->once()->andReturn( 'de_DE' ); + Functions\expect( 'msls_blog_collection' )->once()->andReturn( array() ); - $this->test = new MetaBox(); + $expected = '

No translated versions linked to this post: import content functionality is disabled.

'; + + ( new MetaBox() )->render(); + + $this->expectOutputString( $expected ); } public function test_print_modal_html(): void { - $this->assertEquals( '', $this->test->print_modal_html() ); + $this->assertEquals( '', ( new MetaBox() )->print_modal_html() ); } } diff --git a/tests/phpunit/ContentImport/TestRelations.php b/tests/phpunit/ContentImport/TestRelations.php index 0ccc81d9..fe5989ae 100644 --- a/tests/phpunit/ContentImport/TestRelations.php +++ b/tests/phpunit/ContentImport/TestRelations.php @@ -6,17 +6,12 @@ use lloc\Msls\ContentImport\Relations; use lloc\MslsTests\MslsUnitTestCase; -class TestRelations extends MslsUnitTestCase { - - public function setUp(): void { - parent::setUp(); +final class TestRelations extends MslsUnitTestCase { + public function test_get_data(): void { $coordinates = \Mockery::mock( ImportCoordinates::class ); + $test = new Relations( $coordinates ); - $this->test = new Relations( $coordinates ); - } - - public function test_get_data(): void { - $this->assertIsArray( $this->test->get_data() ); + $this->assertIsArray( $test->get_data() ); } } diff --git a/tests/phpunit/ContentImport/TestService.php b/tests/phpunit/ContentImport/TestService.php index 76f49daf..01af4c50 100644 --- a/tests/phpunit/ContentImport/TestService.php +++ b/tests/phpunit/ContentImport/TestService.php @@ -7,7 +7,7 @@ use lloc\MslsTests\MslsUnitTestCase; use Brain\Monkey\Functions; -class TestService extends MslsUnitTestCase { +final class TestService extends MslsUnitTestCase { public function test_register_not_active_false(): void { $options = \Mockery::mock( MslsOptions::class ); diff --git a/tests/phpunit/Map/TestHrefLang.php b/tests/phpunit/Map/TestHrefLang.php index 17e8f1e6..c16ac528 100644 --- a/tests/phpunit/Map/TestHrefLang.php +++ b/tests/phpunit/Map/TestHrefLang.php @@ -9,11 +9,9 @@ use lloc\Msls\MslsBlogCollection; use lloc\MslsTests\MslsUnitTestCase; -class TestHrefLang extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestHrefLang extends MslsUnitTestCase { + private function HrefLangFactory(): HrefLang { $map = array( 'de_DE' => 'de', 'de_DE_formal' => 'de', @@ -26,12 +24,8 @@ protected function setUp(): void { foreach ( $map as $locale => $alpha2 ) { $blog = \Mockery::mock( MslsBlog::class ); - $blog->shouldReceive( - array( - 'get_alpha2' => $alpha2, - 'get_language' => $locale, - ) - ); + $blog->shouldReceive( 'get_alpha2' )->andReturn( $alpha2 ); + $blog->shouldReceive( 'get_language' )->andReturn( $locale ); $blogs[] = $blog; } @@ -39,23 +33,27 @@ protected function setUp(): void { $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get_objects' )->andReturn( $blogs ); - $this->test = new HrefLang( $collection ); + return new HrefLang( $collection ); } public function test_get(): void { - $this->assertEquals( 'de-DE', $this->test->get( 'de_DE' ) ); - $this->assertEquals( 'de-DE', $this->test->get( 'de_DE_formal' ) ); - $this->assertEquals( 'fr', $this->test->get( 'fr_FR' ) ); - $this->assertEquals( 'es', $this->test->get( 'es_ES' ) ); - $this->assertEquals( 'cat', $this->test->get( 'cat' ) ); - $this->assertEquals( 'en-GB', $this->test->get( 'en_GB' ) ); - $this->assertEquals( 'en-US', $this->test->get( 'en_US' ) ); + $test = $this->HrefLangFactory(); + + $this->assertEquals( 'de-DE', $test->get( 'de_DE' ) ); + $this->assertEquals( 'de-DE', $test->get( 'de_DE_formal' ) ); + $this->assertEquals( 'fr', $test->get( 'fr_FR' ) ); + $this->assertEquals( 'es', $test->get( 'es_ES' ) ); + $this->assertEquals( 'cat', $test->get( 'cat' ) ); + $this->assertEquals( 'en-GB', $test->get( 'en_GB' ) ); + $this->assertEquals( 'en-US', $test->get( 'en_US' ) ); } public function test_get_has_filter(): void { Functions\when( 'has_filter' )->justReturn( true ); Filters\expectApplied( 'msls_head_hreflang' )->once()->with( 'en_US' )->andReturn( 'en-US' ); - $this->assertEquals( 'en-US', $this->test->get( 'en_US' ) ); + $test = $this->HrefLangFactory(); + + $this->assertEquals( 'en-US', $test->get( 'en_US' ) ); } } diff --git a/tests/phpunit/MslsUnitTestCase.php b/tests/phpunit/MslsUnitTestCase.php index 984a92d1..8f5cbba0 100644 --- a/tests/phpunit/MslsUnitTestCase.php +++ b/tests/phpunit/MslsUnitTestCase.php @@ -8,13 +8,6 @@ class MslsUnitTestCase extends TestCase { - /** - * Instance of the class to test - * - * @var object $test - */ - protected object $test; - protected function setUp(): void { parent::setUp(); Monkey\setUp(); diff --git a/tests/phpunit/Query/TestTranslatedPostsQuery.php b/tests/phpunit/Query/TestTranslatedPostsQuery.php index 969265c1..3eb17eac 100644 --- a/tests/phpunit/Query/TestTranslatedPostsQuery.php +++ b/tests/phpunit/Query/TestTranslatedPostsQuery.php @@ -6,7 +6,7 @@ use lloc\Msls\Query\TranslatedPostIdQuery; use lloc\MslsTests\MslsUnitTestCase; -class TestTranslatedPostsQuery extends MslsUnitTestCase { +final class TestTranslatedPostsQuery extends MslsUnitTestCase { public function test_invoke_empty_string(): void { $sql_cache = \Mockery::mock( MslsSqlCacher::class ); diff --git a/tests/phpunit/TestMslsAdmin.php b/tests/phpunit/TestMslsAdmin.php index 2322589d..9bc436c8 100644 --- a/tests/phpunit/TestMslsAdmin.php +++ b/tests/phpunit/TestMslsAdmin.php @@ -8,13 +8,9 @@ use lloc\Msls\MslsBlogCollection; use lloc\Msls\MslsOptions; -class TestMslsAdmin extends MslsUnitTestCase { +final class TestMslsAdmin extends MslsUnitTestCase { - /** - * @param array $users - * @return MslsAdmin - */ - public function get_sut( array $users = array() ): MslsAdmin { + private function MslsAdminFactory( array $users = array() ): MslsAdmin { Functions\when( 'get_option' )->justReturn( array() ); Functions\when( 'update_option' )->justReturn( true ); Functions\when( 'get_current_blog_id' )->justReturn( 1 ); @@ -88,23 +84,22 @@ public function test_has_problems( array $languages, bool $is_empty, string $reg } public function test_subsubsub(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); - $this->assertEquals( - '', - $obj->subsubsub() - ); + $expected = ''; + + $this->assertEquals( $expected, $obj->subsubsub() ); } public function test_blog_language(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputRegex( '/^' @@ -113,7 +108,7 @@ public function test_display(): void { } public function test_admin_display(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( '' @@ -131,7 +126,7 @@ public function test_reference_user(): void { ); } - $obj = $this->get_sut( $users ); + $obj = $this->MslsAdminFactory( $users ); set_error_handler( static function ( $errno, $errstr ) { @@ -150,14 +145,14 @@ static function ( $errno, $errstr ) { } public function test_reference_user_over_max(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputRegex( '/^ ' @@ -166,7 +161,7 @@ public function test_activate_autocomplete(): void { } public function test_sort_by_description(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( ' ' @@ -176,7 +171,7 @@ public function test_sort_by_description(): void { public function test_exclude_current_blog(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( ' ' @@ -185,7 +180,7 @@ public function test_exclude_current_blog(): void { } function test_only_with_translation(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( ' ' @@ -194,7 +189,7 @@ function test_only_with_translation(): void { } function test_output_current_blog(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( ' ' @@ -203,7 +198,7 @@ function test_output_current_blog(): void { } function test_description(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( '' @@ -212,7 +207,7 @@ function test_description(): void { } function test_before_output(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( '' @@ -221,7 +216,7 @@ function test_before_output(): void { } function test_after_output(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( '' @@ -230,7 +225,7 @@ function test_after_output(): void { } function test_before_item(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( '' @@ -239,7 +234,7 @@ function test_before_item(): void { } function test_after_item(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( '' @@ -248,7 +243,7 @@ function test_after_item(): void { } function test_rewrite_tizio(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $post_type = \Mockery::mock( \WP_Post_Type::class ); $post_type->rewrite = false; @@ -262,7 +257,7 @@ function test_rewrite_tizio(): void { } function test_rewrite_pinko(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $post_type = \Mockery::mock( \WP_Post_Type::class ); $post_type->rewrite = true; @@ -276,7 +271,7 @@ function test_rewrite_pinko(): void { } function test_rewrite_pallino(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $post_type = \Mockery::mock( \WP_Post_Type::class ); $post_type->rewrite = array( 'slug' => 'pallino_slug' ); @@ -290,7 +285,7 @@ function test_rewrite_pallino(): void { } function test_content_filter(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputString( ' ' @@ -299,14 +294,14 @@ function test_content_filter(): void { } function test_content_priority(): void { - $obj = $this->get_sut(); + $obj = $this->MslsAdminFactory(); $this->expectOutputRegex( '/^' ); - $this->test->add_filter(); + $test = $this->MslsCustomFilterFactory(); + $test->add_filter(); } public function test_add_no_selected_blog(): void { @@ -86,6 +89,7 @@ public function test_add_no_selected_blog(): void { $this->expectOutputString( '' ); - $this->test->add_filter(); + $test = $this->MslsCustomFilterFactory(); + $test->add_filter(); } } diff --git a/tests/phpunit/TestMslsGetSet.php b/tests/phpunit/TestMslsGetSet.php index 721064a3..036e6ed7 100644 --- a/tests/phpunit/TestMslsGetSet.php +++ b/tests/phpunit/TestMslsGetSet.php @@ -4,7 +4,7 @@ use lloc\Msls\MslsGetSet; -class TestMslsGetSet extends MslsUnitTestCase { +final class TestMslsGetSet extends MslsUnitTestCase { public function test_unset(): void { $obj = new MslsGetSet(); diff --git a/tests/phpunit/TestMslsJson.php b/tests/phpunit/TestMslsJson.php index 6eb08d99..64b1480e 100644 --- a/tests/phpunit/TestMslsJson.php +++ b/tests/phpunit/TestMslsJson.php @@ -4,7 +4,7 @@ use lloc\Msls\MslsJson; -class TestMslsJson extends MslsUnitTestCase { +final class TestMslsJson extends MslsUnitTestCase { public function test_get(): void { $obj = ( new MslsJson() )->add( null, 'Test 3' )->add( '2', 'Test 2' )->add( 1, 'Test 1' ); diff --git a/tests/phpunit/TestMslsLanguageArray.php b/tests/phpunit/TestMslsLanguageArray.php index ba281a6e..7d43f670 100644 --- a/tests/phpunit/TestMslsLanguageArray.php +++ b/tests/phpunit/TestMslsLanguageArray.php @@ -4,9 +4,9 @@ use lloc\Msls\MslsLanguageArray; -class TestMslsLanguageArray extends MslsUnitTestCase { +final class TestMslsLanguageArray extends MslsUnitTestCase { - public function get_test(): MslsLanguageArray { + private function MslsLanguageArrayFactory(): MslsLanguageArray { $arr = array( 'fr_FR' => 0, // not ok, value 0 is not ok as blog_id 'it' => 1, @@ -18,14 +18,14 @@ public function get_test(): MslsLanguageArray { } public function test_get_val(): void { - $obj = $this->get_test(); + $obj = $this->MslsLanguageArrayFactory(); $this->assertEquals( 1, $obj->get_val( 'it' ) ); $this->assertEquals( 0, $obj->get_val( 'fr_FR' ) ); } public function test_get_arr(): void { - $obj = $this->get_test(); + $obj = $this->MslsLanguageArrayFactory(); $this->assertEquals( array( @@ -38,7 +38,7 @@ public function test_get_arr(): void { } public function test_set(): void { - $obj = $this->get_test(); + $obj = $this->MslsLanguageArrayFactory(); $this->assertEquals( array( diff --git a/tests/phpunit/TestMslsLink.php b/tests/phpunit/TestMslsLink.php index 984cae66..59bf1d97 100644 --- a/tests/phpunit/TestMslsLink.php +++ b/tests/phpunit/TestMslsLink.php @@ -7,7 +7,7 @@ use Hoa\Iterator\Filter; use lloc\Msls\MslsLink; -class TestMslsLink extends MslsUnitTestCase { +final class TestMslsLink extends MslsUnitTestCase { public function test_create(): void { Functions\expect( 'has_filter' )->once()->with( 'msls_link_create' )->andReturn( true ); diff --git a/tests/phpunit/TestMslsLinkImageOnly.php b/tests/phpunit/TestMslsLinkImageOnly.php index 154777da..2ae4c23a 100644 --- a/tests/phpunit/TestMslsLinkImageOnly.php +++ b/tests/phpunit/TestMslsLinkImageOnly.php @@ -5,7 +5,7 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsLinkImageOnly; -class TestMslsLinkImageOnly extends MslsUnitTestCase { +final class TestMslsLinkImageOnly extends MslsUnitTestCase { public function test_get_description_method(): void { $this->assertIsSTring( MslsLinkImageOnly::get_description() ); diff --git a/tests/phpunit/TestMslsLinkTextImage.php b/tests/phpunit/TestMslsLinkTextImage.php index 7719abf1..1f6170ad 100644 --- a/tests/phpunit/TestMslsLinkTextImage.php +++ b/tests/phpunit/TestMslsLinkTextImage.php @@ -5,7 +5,7 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsLinkTextImage; -class TestMslsLinkTextImage extends MslsUnitTestCase { +final class TestMslsLinkTextImage extends MslsUnitTestCase { public function test_get_description_method(): void { $this->assertIsSTring( MslsLinkTextImage::get_description() ); diff --git a/tests/phpunit/TestMslsLinkTextOnly.php b/tests/phpunit/TestMslsLinkTextOnly.php index 72757b11..c35c32c6 100644 --- a/tests/phpunit/TestMslsLinkTextOnly.php +++ b/tests/phpunit/TestMslsLinkTextOnly.php @@ -5,7 +5,7 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsLinkTextOnly; -class TestMslsLinkTextOnly extends MslsUnitTestCase { +final class TestMslsLinkTextOnly extends MslsUnitTestCase { public function test_get_description_method(): void { $this->assertIsSTring( MslsLinkTextOnly::get_description() ); diff --git a/tests/phpunit/TestMslsMain.php b/tests/phpunit/TestMslsMain.php index 33830f81..d9116c3f 100644 --- a/tests/phpunit/TestMslsMain.php +++ b/tests/phpunit/TestMslsMain.php @@ -8,11 +8,9 @@ use lloc\Msls\MslsMain; use lloc\Msls\MslsOptions; -class TestMslsMain extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsMain extends MslsUnitTestCase { + private function MslsMainFactory(): MslsMain { $options = \Mockery::mock( MslsOptions::class ); $blog = \Mockery::mock( MslsBlog::class ); @@ -21,11 +19,13 @@ protected function setUp(): void { $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get_current_blog' )->andReturn( $blog ); - $this->test = new MslsMain( $options, $collection ); + return new MslsMain( $options, $collection ); } public function test_get_input_array_empty(): void { - $this->assertEquals( array( 'de_DE' => 0 ), $this->test->get_input_array( 0 ) ); + $test = $this->MslsMainFactory(); + + $this->assertEquals( array( 'de_DE' => 0 ), $test->get_input_array( 0 ) ); } public function test_get_input_array(): void { @@ -36,24 +36,32 @@ public function test_get_input_array(): void { ) ); - $this->assertEquals( array( 'de_DE' => 2 ), $this->test->get_input_array( 1 ) ); + $test = $this->MslsMainFactory(); + + $this->assertEquals( array( 'de_DE' => 2 ), $test->get_input_array( 1 ) ); } public function test_is_autosave(): void { Functions\when( 'wp_is_post_revision' )->justReturn( true ); - $this->assertIsBool( $this->test->is_autosave( 0 ) ); + $test = $this->MslsMainFactory(); + + $this->assertIsBool( $test->is_autosave( 0 ) ); } public function test_verify_nonce(): void { - $this->assertFalse( $this->test->verify_nonce() ); + $test = $this->MslsMainFactory(); + + $this->assertFalse( $test->verify_nonce() ); } public function test_debugger_string(): void { $capture = tmpfile(); $backup = ini_set( 'error_log', stream_get_meta_data( $capture )['uri'] ); - $this->test->debugger( 'Test' ); + $test = $this->MslsMainFactory(); + + $test->debugger( 'Test' ); $this->assertStringContainsString( 'MSLS Debug: Test', stream_get_contents( $capture ) ); @@ -64,7 +72,9 @@ public function test_debugger_object(): void { $capture = tmpfile(); $backup = ini_set( 'error_log', stream_get_meta_data( $capture )['uri'] ); - $this->test->debugger( (object) array( 'test' => 'msls' ) ); + $test = $this->MslsMainFactory(); + + $test->debugger( (object) array( 'test' => 'msls' ) ); $this->assertStringContainsString( '[test] => msls', stream_get_contents( $capture ) ); diff --git a/tests/phpunit/TestMslsMetaBox.php b/tests/phpunit/TestMslsMetaBox.php index b14306e7..78397c08 100644 --- a/tests/phpunit/TestMslsMetaBox.php +++ b/tests/phpunit/TestMslsMetaBox.php @@ -14,11 +14,9 @@ use lloc\Msls\MslsOptionsPost; use lloc\Msls\MslsPostType; -class TestMslsMetaBox extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsMetaBox extends MslsUnitTestCase { + private function MslsMetaBoxFactory(): MslsMetaBox { Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 2 ) . '/' ); $blog = \Mockery::mock( MslsBlog::class ); @@ -33,7 +31,7 @@ protected function setUp(): void { $collection->shouldReceive( 'get_current_blog' )->andReturn( $blog ); $collection->shouldReceive( 'get_blog_id' )->andReturn( 1 ); - $this->test = new MslsMetaBox( $options, $collection ); + return new MslsMetaBox( $options, $collection ); } public function test_init(): void { @@ -91,14 +89,18 @@ public function test_render_option_selected(): void { Functions\expect( 'selected' )->once()->andReturn( 'selected="selected"' ); Functions\expect( 'get_the_title' )->once()->andReturn( 'Test' ); - $this->assertEquals( '', $this->test->render_option( 1, 1 ) ); + $test = $this->MslsMetaBoxFactory(); + + $this->assertEquals( '', $test->render_option( 1, 1 ) ); } public function test_render_option_not_selected(): void { Functions\expect( 'selected' )->once()->andReturn( '' ); Functions\expect( 'get_the_title' )->once()->andReturn( 'Test' ); - $this->assertEquals( '', $this->test->render_option( 1, 2 ) ); + $test = $this->MslsMetaBoxFactory(); + + $this->assertEquals( '', $test->render_option( 1, 2 ) ); } public function test_render_options(): void { @@ -110,7 +112,9 @@ public function test_render_options(): void { Functions\expect( 'selected' )->once()->andReturn( 'selected="selected"' ); Functions\expect( 'get_the_title' )->once()->andReturn( 'A random title' ); - $this->assertEquals( '', $this->test->render_options( 'post', 42 ) ); + $test = $this->MslsMetaBoxFactory(); + + $this->assertEquals( '', $test->render_options( 'post', 42 ) ); } public static function add_data_provider(): array { @@ -136,7 +140,8 @@ public function test_add( $post_type, $content_import, $autocomplete ) { Functions\expect( 'msls_post_type' )->once()->andReturn( $post_type ); $this->expectNotToPerformAssertions(); - $this->test->add(); + + $this->MslsMetaBoxFactory()->add(); } public function test_render_select_not_hierarchical(): void { @@ -168,7 +173,7 @@ public function test_render_select_not_hierarchical(): void { $expected = ''; $this->expectOutputString( $expected ); - $this->test->render_select(); + $this->MslsMetaBoxFactory()->render_select(); } public function test_render_select_hierarchical(): void { @@ -198,7 +203,7 @@ public function test_render_select_hierarchical(): void { $expected = ''; $this->expectOutputString( $expected ); - $this->test->render_select(); + $this->MslsMetaBoxFactory()->render_select(); } public static function render_input_provider(): array { @@ -235,7 +240,7 @@ public function test_render_input( $option, $the_title_times, $current_blog_id_t $this->expectOutputString( $expected ); - $this->test->render_input(); + $this->MslsMetaBoxFactory()->render_input(); } public function test_render_select_only_one_blog(): void { @@ -244,12 +249,12 @@ public function test_render_select_only_one_blog(): void { $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get' )->andReturn( array() ); - $this->test = new MslsMetaBox( $options, $collection ); + $test = new MslsMetaBox( $options, $collection ); $expected = '

You should define at least another blog in a different language in order to have some benefit from this plugin!

'; $this->expectOutputString( $expected ); - $this->test->render_select(); + $test->render_select(); } public function test_render_input_only_one_blog(): void { @@ -258,19 +263,20 @@ public function test_render_input_only_one_blog(): void { $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get' )->andReturn( array() ); - $this->test = new MslsMetaBox( $options, $collection ); + $test = new MslsMetaBox( $options, $collection ); $expected = '

You should define at least another blog in a different language in order to have some benefit from this plugin!

'; $this->expectOutputString( $expected ); - $this->test->render_input(); + $test->render_input(); } public function test_set_no_request(): void { Functions\expect( 'wp_is_post_revision' )->once()->andReturn( false ); $this->expectNotToPerformAssertions(); - $this->test->set( 13 ); + + $this->MslsMetaBoxFactory()->set( 13 ); } public function test_set_with_request_current_user_cannot(): void { @@ -280,7 +286,7 @@ public function test_set_with_request_current_user_cannot(): void { Functions\expect( 'current_user_can' )->once()->andReturnFalse(); $this->expectNotToPerformAssertions(); - $this->test->set( 13 ); + $this->MslsMetaBoxFactory()->set( 13 ); } public function test_set_with_request(): void { @@ -294,7 +300,7 @@ public function test_set_with_request(): void { Functions\expect( 'restore_current_blog' )->once(); $this->expectNotToPerformAssertions(); - $this->test->set( 13 ); + $this->MslsMetaBoxFactory()->set( 13 ); } public function test_maybe_set_linked_post() { @@ -309,8 +315,10 @@ public function test_maybe_set_linked_post() { Functions\expect( 'switch_to_blog' )->once(); Functions\expect( 'get_option' )->once()->andReturn( array() ); + $test = $this->MslsMetaBoxFactory(); + $mydata = new MslsOptionsPost(); - $mydata = $this->test->maybe_set_linked_post( $mydata ); + $mydata = $test->maybe_set_linked_post( $mydata ); $this->assertEquals( 42, $mydata->de_DE ); } @@ -325,8 +333,10 @@ public function test_maybe_set_linked_post_with_no_post() { Functions\expect( 'switch_to_blog' )->once(); Functions\expect( 'get_option' )->once()->andReturn( array() ); + $test = $this->MslsMetaBoxFactory(); + $mydata = new MslsOptionsPost(); - $mydata = $this->test->maybe_set_linked_post( $mydata ); + $mydata = $test->maybe_set_linked_post( $mydata ); $this->assertNull( $mydata->de_DE ); } @@ -337,7 +347,7 @@ function test_maybe_set_linked_post_with_no_blog_id() { $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get_blog_id' )->andReturn( null ); - $this->test = new MslsMetaBox( $options, $collection ); + $test = new MslsMetaBox( $options, $collection ); Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue(); Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue(); @@ -346,7 +356,7 @@ function test_maybe_set_linked_post_with_no_blog_id() { Functions\expect( 'get_option' )->once()->andReturn( array() ); $mydata = new MslsOptionsPost(); - $mydata = $this->test->maybe_set_linked_post( $mydata ); + $mydata = $test->maybe_set_linked_post( $mydata ); $this->assertNull( $mydata->de_DE ); } @@ -356,7 +366,7 @@ function test_maybe_set_linked_post_with_mydata_already_set() { $collection = \Mockery::mock( MslsBlogCollection::class ); - $this->test = new MslsMetaBox( $options, $collection ); + $test = new MslsMetaBox( $options, $collection ); Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue(); Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue(); @@ -365,7 +375,7 @@ function test_maybe_set_linked_post_with_mydata_already_set() { $mydata = new MslsOptionsPost(); $mydata->de_DE = 42; - $mydata = $this->test->maybe_set_linked_post( $mydata ); + $mydata = $test->maybe_set_linked_post( $mydata ); $this->assertEquals( 42, $mydata->de_DE ); } diff --git a/tests/phpunit/TestMslsOptions.php b/tests/phpunit/TestMslsOptions.php index a09c8fe7..d7d24269 100644 --- a/tests/phpunit/TestMslsOptions.php +++ b/tests/phpunit/TestMslsOptions.php @@ -7,9 +7,9 @@ use lloc\Msls\MslsOptions; use lloc\Msls\MslsPostType; -class TestMslsOptions extends MslsUnitTestCase { +final class TestMslsOptions extends MslsUnitTestCase { - public function get_test(): MslsOptions { + private function MslsOptionsFactory(): MslsOptions { Functions\when( 'home_url' )->justReturn( 'https://lloc.de' ); Functions\when( 'get_option' )->justReturn( array() ); Functions\when( 'update_option' )->justReturn( true ); @@ -48,7 +48,7 @@ public function test_create(): void { } public function test_get_arg(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertNull( $obj->get_arg( 0 ) ); $this->assertIsSTring( $obj->get_arg( 0, '' ) ); @@ -65,7 +65,7 @@ public function test_save(): void { Functions\expect( 'delete_option' )->once()->with( 'msls' ); Functions\expect( 'add_option' )->once()->with( 'msls', $arr, '', true ); - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->expectNotToPerformAssertions(); $obj->save( $arr ); @@ -94,44 +94,44 @@ public static function set_provider(): array { * @dataProvider set_provider */ function test_set( $expected, $input ): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertEquals( $expected, $obj->set( $input ) ); } function test_get_permalink(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertIsSTring( $obj->get_permalink( 'de_DE' ) ); } function test_get_postlink(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertIsSTring( $obj->get_postlink( 'de_DE' ) ); $this->assertEquals( '', $obj->get_postlink( 'de_DE' ) ); } function test_get_current_link(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertIsSTring( $obj->get_current_link() ); } function test_is_excluded(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertIsBool( $obj->is_excluded() ); } function test_is_content_filter(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertIsBool( $obj->is_content_filter() ); } function test_get_order(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertIsSTring( $obj->get_order() ); } @@ -139,7 +139,7 @@ function test_get_order(): void { function test_get_url(): void { Functions\when( 'plugins_url' )->justReturn( 'https://msls.co/wp-content/plugins' ); - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertIsSTring( $obj->get_url( '/dev/test' ) ); } @@ -149,7 +149,7 @@ function test_get_flag_url(): void { Functions\when( 'plugins_url' )->justReturn( 'https://msls.co/wp-content/plugins' ); Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 2 ) . '/' ); - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertIsSTring( $obj->get_flag_url( 'de_DE' ) ); } @@ -166,7 +166,7 @@ function ( $code ) { } ); - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $expected = array( 'en_US' => 'American English', @@ -177,13 +177,13 @@ function ( $code ) { } public function test_get_icon_type_standard(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertEquals( MslsAdminIcon::TYPE_FLAG, $obj->get_icon_type() ); } public function test_get_icon_type_admin_display(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $obj->set( array( 'admin_display' => MslsAdminIcon::TYPE_LABEL ) ); $this->assertEquals( MslsAdminIcon::TYPE_LABEL, $obj->get_icon_type() ); @@ -233,12 +233,12 @@ function ( $url = '' ) { } public function test_get_slug(): void { - $obj = $this->get_test(); + $obj = $this->MslsOptionsFactory(); $this->assertEquals( '', $obj->get_slug( 'post' ) ); } public function test_get_option_name(): void { - $this->assertSame( 'msls', $this->get_test()->get_option_name() ); + $this->assertSame( 'msls', $this->MslsOptionsFactory()->get_option_name() ); } } diff --git a/tests/phpunit/TestMslsOptionsPost.php b/tests/phpunit/TestMslsOptionsPost.php index 32b71d8e..1683e513 100644 --- a/tests/phpunit/TestMslsOptionsPost.php +++ b/tests/phpunit/TestMslsOptionsPost.php @@ -6,24 +6,26 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsOptionsPost; -class TestMslsOptionsPost extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsOptionsPost extends MslsUnitTestCase { + private function MslsOptionsPostFactory(): MslsOptionsPost { Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) ); - $this->test = new MslsOptionsPost( 42 ); + return new MslsOptionsPost( 42 ); } public function test_get_postlink_not_has_value(): void { - $this->assertEquals( '', $this->test->get_postlink( 'es_ES' ) ); + $test = $this->MslsOptionsPostFactory(); + + $this->assertEquals( '', $test->get_postlink( 'es_ES' ) ); } public function test_get_postlink_post_is_null(): void { Functions\expect( 'get_post' )->once()->andReturnNull(); - $this->assertEquals( '', $this->test->get_postlink( 'de_DE' ) ); + $test = $this->MslsOptionsPostFactory(); + + $this->assertEquals( '', $test->get_postlink( 'de_DE' ) ); } public function test_get_postlink_post_is_draft(): void { @@ -32,7 +34,9 @@ public function test_get_postlink_post_is_draft(): void { Functions\expect( 'get_post' )->once()->andReturn( $post ); - $this->assertEquals( '', $this->test->get_postlink( 'de_DE' ) ); + $test = $this->MslsOptionsPostFactory(); + + $this->assertEquals( '', $test->get_postlink( 'de_DE' ) ); } public function test_get_postlink_post_is_published(): void { @@ -44,18 +48,24 @@ public function test_get_postlink_post_is_published(): void { Functions\expect( 'get_post_type_object' )->once()->andReturn( (object) array( 'rewrite' => array( 'with_front' => true ) ) ); Functions\expect( 'get_permalink' )->once()->andReturn( 'https://example.de/a-post' ); - Filters\expectApplied( 'check_url' )->once()->with( 'https://example.de/a-post', $this->test ); + $test = $this->MslsOptionsPostFactory(); - $this->assertEquals( 'https://example.de/a-post', $this->test->get_postlink( 'de_DE' ) ); + Filters\expectApplied( 'check_url' )->once()->with( 'https://example.de/a-post', $test ); + + $this->assertEquals( 'https://example.de/a-post', $test->get_postlink( 'de_DE' ) ); } public function test_get_current_link(): void { Functions\expect( 'get_permalink' )->once()->andReturn( 'https://msls.co/a-post' ); - $this->assertEquals( 'https://msls.co/a-post', $this->test->get_current_link() ); + $test = $this->MslsOptionsPostFactory(); + + $this->assertEquals( 'https://msls.co/a-post', $test->get_current_link() ); } public function test_get_option_name(): void { - $this->assertSame( 'msls_42', $this->test->get_option_name() ); + $test = $this->MslsOptionsPostFactory(); + + $this->assertSame( 'msls_42', $test->get_option_name() ); } } diff --git a/tests/phpunit/TestMslsOptionsQuery.php b/tests/phpunit/TestMslsOptionsQuery.php index 539e1551..40cffc30 100644 --- a/tests/phpunit/TestMslsOptionsQuery.php +++ b/tests/phpunit/TestMslsOptionsQuery.php @@ -11,7 +11,7 @@ use lloc\Msls\MslsOptionsQueryYear; use lloc\Msls\MslsSqlCacher; -class TestMslsOptionsQuery extends MslsUnitTestCase { +final class TestMslsOptionsQuery extends MslsUnitTestCase { protected function setUp(): void { parent::setUp(); diff --git a/tests/phpunit/TestMslsOptionsQueryAuthor.php b/tests/phpunit/TestMslsOptionsQueryAuthor.php index 7c724562..c34068e3 100644 --- a/tests/phpunit/TestMslsOptionsQueryAuthor.php +++ b/tests/phpunit/TestMslsOptionsQueryAuthor.php @@ -6,9 +6,9 @@ use lloc\Msls\MslsOptionsQueryAuthor; use lloc\Msls\MslsSqlCacher; -class TestMslsOptionsQueryAuthor extends MslsUnitTestCase { +final class TestMslsOptionsQueryAuthor extends MslsUnitTestCase { - protected function get_test( int $author_id ): MslsOptionsQueryAuthor { + private function MslsOptionsQueryAuthorFactory( int $author_id ): MslsOptionsQueryAuthor { Functions\expect( 'get_option' )->once()->andReturn( array() ); Functions\expect( 'get_queried_object_id' )->once()->andReturn( $author_id ); @@ -20,16 +20,16 @@ protected function get_test( int $author_id ): MslsOptionsQueryAuthor { } public function test_has_value_true(): void { - $this->assertTrue( $this->get_test( 17 )->has_value( 'de_DE' ) ); + $this->assertTrue( $this->MslsOptionsQueryAuthorFactory( 17 )->has_value( 'de_DE' ) ); } public function test_has_value_false(): void { - $this->assertFalse( $this->get_test( 0 )->has_value( 'de_DE' ) ); + $this->assertFalse( $this->MslsOptionsQueryAuthorFactory( 0 )->has_value( 'de_DE' ) ); } public function test_get_current_link_method(): void { Functions\expect( 'get_author_posts_url' )->once()->andReturn( 'https://msls.co/queried-author' ); - $this->assertEquals( 'https://msls.co/queried-author', $this->get_test( 42 )->get_current_link() ); + $this->assertEquals( 'https://msls.co/queried-author', $this->MslsOptionsQueryAuthorFactory( 42 )->get_current_link() ); } } diff --git a/tests/phpunit/TestMslsOptionsQueryDay.php b/tests/phpunit/TestMslsOptionsQueryDay.php index 232e37ca..8363f82d 100644 --- a/tests/phpunit/TestMslsOptionsQueryDay.php +++ b/tests/phpunit/TestMslsOptionsQueryDay.php @@ -6,9 +6,9 @@ use lloc\Msls\MslsOptionsQueryDay; use lloc\Msls\MslsSqlCacher; -class TestMslsOptionsQueryDay extends MslsUnitTestCase { +final class TestMslsOptionsQueryDay extends MslsUnitTestCase { - public function get_test( int $year, int $monthnum, int $day ): MslsOptionsQueryDay { + private function MslsOptionsQueryDayFactory( int $year, int $monthnum, int $day ): MslsOptionsQueryDay { parent::setUp(); Functions\expect( 'get_option' )->once()->andReturn( array() ); @@ -22,16 +22,16 @@ public function get_test( int $year, int $monthnum, int $day ): MslsOptionsQuery } public function test_has_value_true(): void { - $this->assertTrue( $this->get_test( 1998, 12, 31 )->has_value( 'de_DE' ) ); + $this->assertTrue( $this->MslsOptionsQueryDayFactory( 1998, 12, 31 )->has_value( 'de_DE' ) ); } public function test_has_value(): void { - $this->assertFalse( $this->get_test( 0, 0, 0 )->has_value( 'de_DE' ) ); + $this->assertFalse( $this->MslsOptionsQueryDayFactory( 0, 0, 0 )->has_value( 'de_DE' ) ); } public function test_get_current_link(): void { Functions\expect( 'get_day_link' )->once()->andReturn( 'https://msls.co/queried-day' ); - $this->assertEquals( 'https://msls.co/queried-day', $this->get_test( 2015, 07, 02 )->get_current_link() ); + $this->assertEquals( 'https://msls.co/queried-day', $this->MslsOptionsQueryDayFactory( 2015, 07, 02 )->get_current_link() ); } } diff --git a/tests/phpunit/TestMslsOptionsQueryMonth.php b/tests/phpunit/TestMslsOptionsQueryMonth.php index 434a16bd..0124ebe2 100644 --- a/tests/phpunit/TestMslsOptionsQueryMonth.php +++ b/tests/phpunit/TestMslsOptionsQueryMonth.php @@ -6,11 +6,9 @@ use lloc\Msls\MslsOptionsQueryMonth; use lloc\Msls\MslsSqlCacher; -class TestMslsOptionsQueryMonth extends MslsUnitTestCase { - - public function get_test( int $year, int $monthnum ): MslsOptionsQueryMonth { - parent::setUp(); +final class TestMslsOptionsQueryMonth extends MslsUnitTestCase { + private function MslsOptionsQueryMonthFactory( int $year, int $monthnum ): MslsOptionsQueryMonth { Functions\expect( 'get_option' )->once()->andReturn( array() ); Functions\expect( 'get_query_var' )->times( 2 )->andReturn( $year, $monthnum ); @@ -22,16 +20,16 @@ public function get_test( int $year, int $monthnum ): MslsOptionsQueryMonth { } public function test_has_value_true(): void { - $this->assertTrue( $this->get_test( 1998, 12 )->has_value( 'de_DE' ) ); + $this->assertTrue( $this->MslsOptionsQueryMonthFactory( 1998, 12 )->has_value( 'de_DE' ) ); } public function test_has_value_false(): void { - $this->assertFalse( $this->get_test( 0, 0 )->has_value( 'de_DE' ) ); + $this->assertFalse( $this->MslsOptionsQueryMonthFactory( 0, 0 )->has_value( 'de_DE' ) ); } public function test_get_current_link(): void { Functions\expect( 'get_month_link' )->once()->andReturn( 'https://msls.co/queried-month' ); - $this->assertEquals( 'https://msls.co/queried-month', $this->get_test( 2015, 7 )->get_current_link() ); + $this->assertEquals( 'https://msls.co/queried-month', $this->MslsOptionsQueryMonthFactory( 2015, 7 )->get_current_link() ); } } diff --git a/tests/phpunit/TestMslsOptionsQueryPostType.php b/tests/phpunit/TestMslsOptionsQueryPostType.php index 2f32ed4e..89c93914 100644 --- a/tests/phpunit/TestMslsOptionsQueryPostType.php +++ b/tests/phpunit/TestMslsOptionsQueryPostType.php @@ -6,11 +6,9 @@ use lloc\Msls\MslsOptionsQueryPostType; use lloc\Msls\MslsSqlCacher; -class TestMslsOptionsQueryPostType extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsOptionsQueryPostType extends MslsUnitTestCase { + private function MslsOptionsQueryPostTypeFactory(): MslsOptionsQueryPostType { Functions\expect( 'get_option' )->once()->andReturn( array() ); Functions\expect( 'get_query_var' )->once()->andReturn( 'queried-posttype' ); @@ -18,7 +16,7 @@ protected function setUp(): void { $sql_cacher->shouldReceive( 'prepare' )->never(); $sql_cacher->shouldReceive( 'get_var' )->never(); - $this->test = new MslsOptionsQueryPostType( $sql_cacher ); + return new MslsOptionsQueryPostType( $sql_cacher ); } public function test_has_value_existing(): void { @@ -30,19 +28,26 @@ public function test_has_value_existing(): void { ), ) ); - $this->assertTrue( $this->test->has_value( 'de_DE' ) ); + + $test = $this->MslsOptionsQueryPostTypeFactory(); + + $this->assertTrue( $test->has_value( 'de_DE' ) ); } public function test_has_value_not_existing(): void { $post_type = \Mockery::mock( '\WP_Post_Type' ); Functions\expect( 'get_post_type_object' )->once()->andReturn( $post_type ); - $this->assertTrue( $this->test->has_value( 'it_IT' ) ); + $test = $this->MslsOptionsQueryPostTypeFactory(); + + $this->assertTrue( $test->has_value( 'it_IT' ) ); } public function test_get_current_link(): void { Functions\expect( 'get_post_type_archive_link' )->once()->andReturn( 'https://msls.co/queried-posttype' ); - $this->assertEquals( 'https://msls.co/queried-posttype', $this->test->get_current_link() ); + $test = $this->MslsOptionsQueryPostTypeFactory(); + + $this->assertEquals( 'https://msls.co/queried-posttype', $test->get_current_link() ); } } diff --git a/tests/phpunit/TestMslsOptionsQueryYear.php b/tests/phpunit/TestMslsOptionsQueryYear.php index 0156255b..d941bbde 100644 --- a/tests/phpunit/TestMslsOptionsQueryYear.php +++ b/tests/phpunit/TestMslsOptionsQueryYear.php @@ -6,11 +6,9 @@ use lloc\Msls\MslsOptionsQueryYear; use lloc\Msls\MslsSqlCacher; -class TestMslsOptionsQueryYear extends MslsUnitTestCase { - - protected function get_test( int $year ): MslsOptionsQueryYear { - parent::setUp(); +final class TestMslsOptionsQueryYear extends MslsUnitTestCase { + private function MslsOptionsQueryYearFactory( int $year ): MslsOptionsQueryYear { Functions\expect( 'get_option' )->once()->andReturn( array() ); Functions\expect( 'get_query_var' )->once()->andReturn( $year ); @@ -22,16 +20,18 @@ protected function get_test( int $year ): MslsOptionsQueryYear { } public function test_has_value_true(): void { - $this->assertTrue( $this->get_test( 1998 )->has_value( 'de_DE' ) ); + $this->assertTrue( $this->MslsOptionsQueryYearFactory( 1998 )->has_value( 'de_DE' ) ); } public function test_has_value_false(): void { - $this->assertFalse( $this->get_test( 0 )->has_value( 'de_DE' ) ); + $this->assertFalse( $this->MslsOptionsQueryYearFactory( 0 )->has_value( 'de_DE' ) ); } public function test_get_current_link_method(): void { Functions\expect( 'get_year_link' )->once()->andReturn( 'https://msls.co/queried-year' ); - $this->assertEquals( 'https://msls.co/queried-year', $this->get_test( 2015 )->get_current_link() ); + $test = $this->MslsOptionsQueryYearFactory( 2015 ); + + $this->assertEquals( 'https://msls.co/queried-year', $test->get_current_link() ); } } diff --git a/tests/phpunit/TestMslsOptionsTax.php b/tests/phpunit/TestMslsOptionsTax.php index 88673c57..09afcefe 100644 --- a/tests/phpunit/TestMslsOptionsTax.php +++ b/tests/phpunit/TestMslsOptionsTax.php @@ -7,20 +7,19 @@ use lloc\Msls\MslsOptionsTaxTerm; use lloc\Msls\MslsOptionsTaxTermCategory; -class TestMslsOptionsTax extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsOptionsTax extends MslsUnitTestCase { + private function MslsOptionsTaxFactory(): MslsOptionsTax { Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array( 'de_DE' => 42 ) ); - $this->test = new MslsOptionsTax( 0 ); + return new MslsOptionsTax( 0 ); } public function test_create_category(): void { Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 ); Functions\expect( 'is_admin' )->once()->andReturnFalse(); Functions\expect( 'is_category' )->once()->andReturnTrue(); + Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array( 'de_DE' => 42 ) ); $this->assertInstanceOf( MslsOptionsTaxTermCategory::class, MslsOptionsTax::create() ); } @@ -30,6 +29,7 @@ public function test_create_post_tag(): void { Functions\expect( 'is_admin' )->once()->andReturnFalse(); Functions\expect( 'is_category' )->once()->andReturnFalse(); Functions\expect( 'is_tag' )->once()->andReturnTrue(); + Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array( 'de_DE' => 42 ) ); $this->assertInstanceOf( MslsOptionsTaxTerm::class, MslsOptionsTax::create() ); } @@ -37,7 +37,9 @@ public function test_create_post_tag(): void { public function test_get_tax_query(): void { Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); - $this->assertEquals( '', $this->test->get_tax_query() ); + $test = $this->MslsOptionsTaxFactory(); + + $this->assertEquals( '', $test->get_tax_query() ); } public function test_get_tax_query_woo(): void { @@ -54,7 +56,9 @@ public function test_get_tax_query_woo(): void { Functions\expect( 'is_woocommerce' )->once()->andReturn( true ); - $this->assertEquals( $expected, $this->test->get_tax_query( array() ) ); + $test = $this->MslsOptionsTaxFactory(); + + $this->assertEquals( $expected, $test->get_tax_query() ); } public function test_get_tax_query_set(): void { @@ -71,23 +75,31 @@ public function test_get_tax_query_set(): void { Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); - $this->assertEquals( $expected, $this->test->get_tax_query( array() ) ); + $test = $this->MslsOptionsTaxFactory(); + + $this->assertEquals( $expected, $test->get_tax_query( array() ) ); } public function test_get_postlink(): void { Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); - $this->assertEquals( '', $this->test->get_postlink( 'de_DE' ) ); + $test = $this->MslsOptionsTaxFactory(); + + $this->assertEquals( '', $test->get_postlink( 'de_DE' ) ); } public function test_get_postlink_empty(): void { Functions\expect( 'is_woocommerce' )->never(); - $this->assertEquals( '', $this->test->get_postlink( 'it_IT' ) ); + $test = $this->MslsOptionsTaxFactory(); + + $this->assertEquals( '', $test->get_postlink( 'it_IT' ) ); } public function test_get_current_link(): void { - $this->assertIsString( $this->test->get_current_link() ); + $test = $this->MslsOptionsTaxFactory(); + + $this->assertIsString( $test->get_current_link() ); } public function test_get_term_link(): void { @@ -106,7 +118,9 @@ public function test_get_term_link(): void { Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); Functions\expect( 'get_term_link' )->once()->andReturn( $expected ); - $this->assertEquals( $expected, $this->test->get_term_link( 42 ) ); + $test = $this->MslsOptionsTaxFactory(); + + $this->assertEquals( $expected, $test->get_term_link( 42 ) ); } public function test_get_term_link_wp_error(): void { @@ -125,12 +139,16 @@ public function test_get_term_link_wp_error(): void { Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); Functions\expect( 'get_term_link' )->once()->andReturn( $wp_error ); - $this->assertEquals( '', $this->test->get_term_link( 42 ) ); + $test = $this->MslsOptionsTaxFactory(); + + $this->assertEquals( '', $test->get_term_link( 42 ) ); } public function test_get_term_link_empty(): void { Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); - $this->assertEquals( '', $this->test->get_term_link( 42 ) ); + $test = $this->MslsOptionsTaxFactory(); + + $this->assertEquals( '', $test->get_term_link( 42 ) ); } } diff --git a/tests/phpunit/TestMslsOptionsTaxTerm.php b/tests/phpunit/TestMslsOptionsTaxTerm.php index b64a6660..d4bb2594 100644 --- a/tests/phpunit/TestMslsOptionsTaxTerm.php +++ b/tests/phpunit/TestMslsOptionsTaxTerm.php @@ -5,25 +5,28 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsOptionsTaxTerm; -class TestMslsOptionsTaxTerm extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsOptionsTaxTerm extends MslsUnitTestCase { + private function MslsOptionsTaxTermFactory(): MslsOptionsTaxTerm { Functions\expect( 'get_option' )->once()->with( 'msls_term_42' )->andReturn( array( 'de_DE' => 42 ) ); - $this->test = new MslsOptionsTaxTerm( 42 ); + return new MslsOptionsTaxTerm( 42 ); } + public function test_get_postlink_empty(): void { - $this->assertEquals( '', $this->test->get_postlink( '' ) ); + $test = $this->MslsOptionsTaxTermFactory(); + + $this->assertEquals( '', $test->get_postlink( '' ) ); } public function test_check_url_empty(): void { $options = \Mockery::mock( MslsOptionsTaxTerm::class ); - $this->assertEquals( '', $this->test->check_base( null, $options ) ); - $this->assertEquals( '', $this->test->check_base( '', $options ) ); - $this->assertEquals( '', $this->test->check_base( false, $options ) ); + $test = $this->MslsOptionsTaxTermFactory(); + + $this->assertEquals( '', $test->check_base( null, $options ) ); + $this->assertEquals( '', $test->check_base( '', $options ) ); + $this->assertEquals( '', $test->check_base( false, $options ) ); } public function test_check_url(): void { @@ -36,7 +39,10 @@ public function test_check_url(): void { $options->shouldReceive( 'get_tax_query' )->andReturn( '' ); $expected = 'https://example.de/tag/keyword'; - $this->assertEquals( $expected, $this->test->check_base( 'https://example.de/schlagwort/keyword', $options ) ); + + $test = $this->MslsOptionsTaxTermFactory(); + + $this->assertEquals( $expected, $test->check_base( 'https://example.de/schlagwort/keyword', $options ) ); } public function test_check_url_permastruct_false(): void { @@ -49,10 +55,15 @@ public function test_check_url_permastruct_false(): void { $options->shouldReceive( 'get_tax_query' )->andReturn( '' ); $expected = 'https://example.de/schlagwort/keyword'; - $this->assertEquals( $expected, $this->test->check_base( $expected, $options ) ); + + $test = $this->MslsOptionsTaxTermFactory(); + + $this->assertEquals( $expected, $test->check_base( $expected, $options ) ); } public function test_get_option_name(): void { - $this->assertSame( 'msls_term_42', $this->test->get_option_name() ); + $test = $this->MslsOptionsTaxTermFactory(); + + $this->assertSame( 'msls_term_42', $test->get_option_name() ); } } diff --git a/tests/phpunit/TestMslsOptionsTaxTermCategory.php b/tests/phpunit/TestMslsOptionsTaxTermCategory.php index bd01af82..cb2bb6c3 100644 --- a/tests/phpunit/TestMslsOptionsTaxTermCategory.php +++ b/tests/phpunit/TestMslsOptionsTaxTermCategory.php @@ -5,7 +5,7 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsOptionsTaxTermCategory; -class TestMslsOptionsTaxTermCategory extends MslsUnitTestCase { +final class TestMslsOptionsTaxTermCategory extends MslsUnitTestCase { public function test_object(): void { Functions\expect( 'get_option' )->once()->andReturn( array() ); diff --git a/tests/phpunit/TestMslsOutput.php b/tests/phpunit/TestMslsOutput.php index 0ddaa7f3..5ddcbef5 100644 --- a/tests/phpunit/TestMslsOutput.php +++ b/tests/phpunit/TestMslsOutput.php @@ -6,16 +6,13 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsBlog; use lloc\Msls\MslsBlogCollection; -use lloc\Msls\MslsContentFilter; use lloc\Msls\MslsOptions; use lloc\Msls\MslsOptionsPost; use lloc\Msls\MslsOutput; -class TestMslsOutput extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsOutput extends MslsUnitTestCase { + private function MslsOutputFactory(): MslsOutput { $options = \Mockery::mock( MslsOptions::class ); $collection = \Mockery::mock( MslsBlogCollection::class ); @@ -23,11 +20,13 @@ protected function setUp(): void { $collection->shouldReceive( 'get_current_blog' )->andReturn( 1 ); $collection->shouldReceive( 'get_filtered' )->andReturn( array() ); - $this->test = new MslsOutput( $options, $collection ); + return new MslsOutput( $options, $collection ); } public function test_get_method(): void { - $this->assertEquals( array(), $this->test->get( 0 ) ); + $test = $this->MslsOutputFactory(); + + $this->assertEquals( array(), $test->get( 0 ) ); } public function test_get_alternate_links_two_url(): void { @@ -71,7 +70,10 @@ public function test_get_alternate_links_two_url(): void { $expected = '' . PHP_EOL . ''; - $this->assertEquals( $expected, $this->test->get_alternate_links() ); + + $test = $this->MslsOutputFactory(); + + $this->assertEquals( $expected, $test->get_alternate_links() ); } public function test_get_alternate_links_null_url(): void { @@ -101,7 +103,9 @@ public function test_get_alternate_links_null_url(): void { Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 ); Functions\expect( 'get_option' )->once()->andReturn( array() ); - $this->assertEquals( '', $this->test->get_alternate_links() ); + $test = $this->MslsOutputFactory(); + + $this->assertEquals( '', $test->get_alternate_links() ); } public function test_get_alternate_links_one_url(): void { @@ -134,14 +138,21 @@ public function test_get_alternate_links_one_url(): void { Filters\expectApplied( 'mlsl_output_get_alternate_links_default' )->once(); - $this->assertEquals( '', $this->test->get_alternate_links() ); + $expected = ''; + + $test = $this->MslsOutputFactory(); + + $this->assertEquals( $expected, $test->get_alternate_links() ); } public function test___toString_no_translation(): void { $expected = 'Example'; Filters\expectApplied( 'msls_output_no_translation_found' )->once()->andReturn( $expected ); - $this->assertEquals( $expected, strval( $this->test ) ); + + $test = $this->MslsOutputFactory(); + + $this->assertEquals( $expected, strval( $test ) ); } public function test___toString_output(): void { @@ -172,9 +183,11 @@ public function test___toString_output(): void { Functions\expect( 'restore_current_blog' )->once(); Functions\expect( 'home_url' )->once()->andReturnFirstArg(); + $expected = 'de_DE Deutsch'; + $test = new MslsOutput( $options, $collection ); - $this->assertEquals( 'de_DE Deutsch', strval( $test ) ); + $this->assertEquals( $expected, strval( $test ) ); } public function test___toString_current_blog(): void { @@ -272,18 +285,24 @@ public function test_get_not_fulfilled(): void { } public function test_get_tags(): void { - $this->assertIsArray( $this->test->get_tags() ); + $test = $this->MslsOutputFactory(); + + $this->assertIsArray( $test->get_tags() ); } public function test_set_tags(): void { Functions\expect( 'wp_parse_args' )->once()->andReturn( array() ); - $this->assertInstanceOf( MslsOutput::class, $this->test->set_tags() ); + $test = $this->MslsOutputFactory(); + + $this->assertInstanceOf( MslsOutput::class, $test->set_tags() ); } public function test_is_requirements_not_fulfilled_with_null(): void { - $this->assertFalse( $this->test->is_requirements_not_fulfilled( null, false, 'de_DE' ) ); - $this->assertTrue( $this->test->is_requirements_not_fulfilled( null, true, 'de_DE' ) ); + $test = $this->MslsOutputFactory(); + + $this->assertFalse( $test->is_requirements_not_fulfilled( null, false, 'de_DE' ) ); + $this->assertTrue( $test->is_requirements_not_fulfilled( null, true, 'de_DE' ) ); } public function test_is_requirements_not_fulfilled_with_mslsoptions(): void { @@ -291,8 +310,10 @@ public function test_is_requirements_not_fulfilled_with_mslsoptions(): void { $mydata = new MslsOptions(); - $this->assertFalse( $this->test->is_requirements_not_fulfilled( $mydata, false, 'de_DE' ) ); - $this->assertFalse( $this->test->is_requirements_not_fulfilled( $mydata, true, 'de_DE' ) ); + $test = $this->MslsOutputFactory(); + + $this->assertFalse( $test->is_requirements_not_fulfilled( $mydata, false, 'de_DE' ) ); + $this->assertFalse( $test->is_requirements_not_fulfilled( $mydata, true, 'de_DE' ) ); } public function test_is_requirements_not_fulfilled_with_mslsoptionspost(): void { @@ -300,8 +321,10 @@ public function test_is_requirements_not_fulfilled_with_mslsoptionspost(): void $mydata = new MslsOptionsPost(); - $this->assertFalse( $this->test->is_requirements_not_fulfilled( $mydata, false, 'de_DE' ) ); - $this->assertTrue( $this->test->is_requirements_not_fulfilled( $mydata, true, 'de_DE' ) ); + $test = $this->MslsOutputFactory(); + + $this->assertFalse( $test->is_requirements_not_fulfilled( $mydata, false, 'de_DE' ) ); + $this->assertTrue( $test->is_requirements_not_fulfilled( $mydata, true, 'de_DE' ) ); } public function test_init(): void { diff --git a/tests/phpunit/TestMslsPlugin.php b/tests/phpunit/TestMslsPlugin.php index 3b000676..fb3c2a1f 100644 --- a/tests/phpunit/TestMslsPlugin.php +++ b/tests/phpunit/TestMslsPlugin.php @@ -8,7 +8,7 @@ use lloc\Msls\MslsOutput; use lloc\Msls\MslsPlugin; -class TestMslsPlugin extends MslsUnitTestCase { +final class TestMslsPlugin extends MslsUnitTestCase { function test_admin_menu_without_autocomplete(): void { Functions\expect( 'is_admin_bar_showing' )->once()->andReturnTrue(); diff --git a/tests/phpunit/TestMslsPostTag.php b/tests/phpunit/TestMslsPostTag.php index b638885f..0900a2d9 100644 --- a/tests/phpunit/TestMslsPostTag.php +++ b/tests/phpunit/TestMslsPostTag.php @@ -11,11 +11,9 @@ use lloc\Msls\MslsPostTag; use lloc\Msls\MslsTaxonomy; -class TestMslsPostTag extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsPostTag extends MslsUnitTestCase { + private function MslsPostTagFacory(): MslsPostTag { Functions\when( 'get_option' )->justReturn( array( 'de_DE' => 42 ) ); Functions\expect( 'is_admin' )->andReturn( true ); Functions\expect( 'get_post_types' )->andReturn( array( 'post', 'page' ) ); @@ -39,7 +37,8 @@ function ( $language ) { return $language === 'de_DE' ? 1 : null; } ); - $this->test = new MslsPostTag( $options, $collection ); + + return new MslsPostTag( $options, $collection ); } public function test_init(): void { @@ -128,11 +127,13 @@ public function test_edit_input(): void { '; + $test = $this->MslsPostTagFacory(); + $this->expectOutputString( $output ); - $this->test->edit_input( $term, 'test' ); + $test->edit_input( $term, 'test' ); // second call should not output anything - $this->test->edit_input( $term, 'test' ); + $test->edit_input( $term, 'test' ); } public function test_add_input(): void { @@ -163,10 +164,11 @@ public function test_add_input(): void { $this->expectOutputString( $output ); - $this->test->add_input( 'test' ); + $test = $this->MslsPostTagFacory(); + $test->add_input( 'test' ); // second call should not output anything - $this->test->add_input( 'test' ); + $test->add_input( 'test' ); } public function test_the_input_no_blogs(): void { @@ -189,7 +191,7 @@ public function test_set() { Functions\expect( 'restore_current_blog' )->twice(); $this->expectNotToPerformAssertions(); - $this->test->set( 42 ); + $this->MslsPostTagFacory()->set( 42 ); } public function test_maybe_set_linked_term_origin_lang(): void { @@ -199,7 +201,8 @@ public function test_maybe_set_linked_term_origin_lang(): void { Functions\expect( 'filter_has_var' )->twice()->andReturnTrue(); Functions\expect( 'filter_input' )->once()->andReturn( 'de_DE' ); - $result = $this->test->maybe_set_linked_term( $mydata ); + $test = $this->MslsPostTagFacory(); + $result = $test->maybe_set_linked_term( $mydata ); $this->assertSame( $mydata, $result ); } @@ -211,7 +214,8 @@ public function test_maybe_set_linked_term_blog_id_null(): void { Functions\expect( 'filter_has_var' )->twice()->andReturnTrue(); Functions\expect( 'filter_input' )->twice()->andReturn( 'fr_FR', 13 ); - $result = $this->test->maybe_set_linked_term( $mydata ); + $test = $this->MslsPostTagFacory(); + $result = $test->maybe_set_linked_term( $mydata ); $this->assertSame( $mydata, $result ); } @@ -226,7 +230,8 @@ public function test_maybe_set_linked_term_origin_term_wrong(): void { Functions\expect( 'get_term' )->once()->andReturn( (object) array() ); Functions\expect( 'restore_current_blog' )->once(); - $result = $this->test->maybe_set_linked_term( $mydata ); + $test = $this->MslsPostTagFacory(); + $result = $test->maybe_set_linked_term( $mydata ); $this->assertSame( $mydata, $result ); } @@ -244,7 +249,8 @@ public function test_maybe_set_linked_term_happy_path(): void { Functions\expect( 'get_term' )->once()->andReturn( $term ); Functions\expect( 'restore_current_blog' )->once(); - $result = $this->test->maybe_set_linked_term( $mydata ); + $test = $this->MslsPostTagFacory(); + $result = $test->maybe_set_linked_term( $mydata ); $expected = array( $term->en_US, 13 ); $actual = array( $result->en_US, $result->de_DE ); diff --git a/tests/phpunit/TestMslsPostTagClassic.php b/tests/phpunit/TestMslsPostTagClassic.php index ed8f53cd..94117095 100644 --- a/tests/phpunit/TestMslsPostTagClassic.php +++ b/tests/phpunit/TestMslsPostTagClassic.php @@ -9,11 +9,9 @@ use lloc\Msls\MslsOptions; use lloc\Msls\MslsPostTagClassic; -class TestMslsPostTagClassic extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsPostTagClassic extends MslsUnitTestCase { + private function MslsPostTagClassicFactory(): MslsPostTagClassic { Functions\expect( 'get_option' )->andReturn( array( 'de_DE' => 42, @@ -36,7 +34,7 @@ protected function setUp(): void { $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get' )->andReturn( $blogs ); - $this->test = new MslsPostTagClassic( $options, $collection ); + return new MslsPostTagClassic( $options, $collection ); } /** @@ -106,10 +104,11 @@ public function test_edit_input(): void { $tag = \Mockery::mock( \WP_Term::class ); - $this->test->edit_input( $tag, 'test' ); + $test = $this->MslsPostTagClassicFactory(); + $test->edit_input( $tag, 'test' ); // second call should not output anything - $this->test->edit_input( $tag, 'test' ); + $test->edit_input( $tag, 'test' ); } public function test_add_input(): void { @@ -154,10 +153,11 @@ public function test_add_input(): void { $this->expectOutputString( $output ); - $this->test->add_input( 'test' ); + $test = $this->MslsPostTagClassicFactory(); + $test->add_input( 'test' ); // second call should not output anything - $this->test->add_input( 'test' ); + $test->add_input( 'test' ); } public function test_the_input_no_blogs(): void { diff --git a/tests/phpunit/TestMslsPostType.php b/tests/phpunit/TestMslsPostType.php index 74a6c1fc..d70ec805 100644 --- a/tests/phpunit/TestMslsPostType.php +++ b/tests/phpunit/TestMslsPostType.php @@ -5,22 +5,24 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsPostType; -class TestMslsPostType extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); +final class TestMslsPostType extends MslsUnitTestCase { + private function MslsPostTypeFactory(): MslsPostType { Functions\when( 'get_post_types' )->justReturn( array() ); Functions\when( 'get_post_type' )->justReturn( array() ); - $this->test = new MslsPostType(); + return new MslsPostType(); } public function test_is_post_type(): void { - $this->assertTrue( $this->test->is_post_type() ); + $test = $this->MslsPostTypeFactory(); + + $this->assertTrue( $test->is_post_type() ); } public function test_acl_request(): void { - $this->assertEquals( '', $this->test->acl_request() ); + $test = $this->MslsPostTypeFactory(); + + $this->assertEquals( '', $test->acl_request() ); } } diff --git a/tests/phpunit/TestMslsRegistry.php b/tests/phpunit/TestMslsRegistry.php index 65e13bd8..0e61df69 100644 --- a/tests/phpunit/TestMslsRegistry.php +++ b/tests/phpunit/TestMslsRegistry.php @@ -4,7 +4,7 @@ use lloc\Msls\MslsRegistry; -class TestMslsRegistry extends MslsUnitTestCase { +final class TestMslsRegistry extends MslsUnitTestCase { public function test_set_method(): void { $obj = new MslsRegistry(); diff --git a/tests/phpunit/TestMslsRequest.php b/tests/phpunit/TestMslsRequest.php index 67905894..1c980a4c 100644 --- a/tests/phpunit/TestMslsRequest.php +++ b/tests/phpunit/TestMslsRequest.php @@ -6,7 +6,7 @@ use lloc\Msls\MslsFields; use lloc\Msls\MslsRequest; -class TestMslsRequest extends MslsUnitTestCase { +final class TestMslsRequest extends MslsUnitTestCase { public function test_isset_ok(): void { Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturn( 13 ); diff --git a/tests/phpunit/TestMslsShortCode.php b/tests/phpunit/TestMslsShortCode.php index 49401f8f..a20ed124 100644 --- a/tests/phpunit/TestMslsShortCode.php +++ b/tests/phpunit/TestMslsShortCode.php @@ -6,7 +6,7 @@ use lloc\Msls\MslsOptions; use lloc\Msls\MslsShortCode; -class TestMslsShortCode extends MslsUnitTestCase { +final class TestMslsShortCode extends MslsUnitTestCase { public function test_init(): void { Functions\expect( 'add_shortcode' )->once()->with( 'sc_msls_widget', array( MslsShortCode::class, 'render_widget' ) ); diff --git a/tests/phpunit/TestMslsSqlCacher.php b/tests/phpunit/TestMslsSqlCacher.php index 18f9bed9..e176ccb8 100644 --- a/tests/phpunit/TestMslsSqlCacher.php +++ b/tests/phpunit/TestMslsSqlCacher.php @@ -5,18 +5,7 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsSqlCacher; -class TestMslsSqlCacher extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); - - $wpdb = \Mockery::mock( \WPDB::class ); - - $wpdb->shouldReceive( 'prepare' )->andReturn( '' ); - $wpdb->shouldReceive( 'get_results' )->andReturn( array() ); - - $this->test = new MslsSqlCacher( $wpdb, 'MslsSqlCacherTest' ); - } +final class TestMslsSqlCacher extends MslsUnitTestCase { public function test_create(): void { global $wpdb; @@ -30,16 +19,25 @@ public function test_create(): void { } public function test_set_params_method(): void { + global $wpdb; + + $wpdb = \Mockery::mock( \WPDB::class ); + + $wpdb->shouldReceive( 'prepare' )->andReturn( '' ); + $wpdb->shouldReceive( 'get_results' )->andReturn( array() ); + Functions\when( 'wp_cache_get' )->justReturn( false ); Functions\when( 'wp_cache_set' )->justReturn( true ); - $sql = $this->test->prepare( - "SELECT blog_id FROM {$this->test->blogs} WHERE blog_id != %d AND site_id = %d", - $this->test->blogid, - $this->test->siteid + $test = new MslsSqlCacher( $wpdb, 'MslsSqlCacherTest' ); + + $sql = $test->prepare( + "SELECT blog_id FROM {$test->blogs} WHERE blog_id != %d AND site_id = %d", + $test->blogid, + $test->siteid ); $this->assertIsSTring( $sql ); - $this->assertIsArray( $this->test->get_results( $sql ) ); + $this->assertIsArray( $test->get_results( $sql ) ); } } diff --git a/tests/phpunit/TestMslsTaxonomy.php b/tests/phpunit/TestMslsTaxonomy.php index 3eb0cffd..cad65149 100644 --- a/tests/phpunit/TestMslsTaxonomy.php +++ b/tests/phpunit/TestMslsTaxonomy.php @@ -7,16 +7,9 @@ use lloc\Msls\MslsOptions; use lloc\Msls\MslsTaxonomy; -class TestMslsTaxonomy extends MslsUnitTestCase { - - /** - * @param bool $exluded - * - * @return MslsTaxonomy - */ - public function get_test( bool $exluded = false ): MslsTaxonomy { - parent::setUp(); +final class TestMslsTaxonomy extends MslsUnitTestCase { + private function MslsTaxonomyFactory( bool $exluded = false ): MslsTaxonomy { $options = \Mockery::mock( MslsOptions::class ); $options->shouldReceive( 'is_excluded' )->andReturn( $exluded ); @@ -37,34 +30,34 @@ public function test_acl_request_included(): void { Functions\when( 'current_user_can' )->justReturn( true ); Functions\expect( 'get_query_var' )->twice()->with( 'taxonomy' )->andReturn( 'category' ); - $this->assertEquals( 'category', $this->get_test()->acl_request() ); + $this->assertEquals( 'category', $this->MslsTaxonomyFactory()->acl_request() ); } public function test_acl_request_excluded(): void { Functions\expect( 'get_query_var' )->once()->with( 'taxonomy' )->andReturn( 'category' ); - $this->assertEquals( '', $this->get_test( true )->acl_request() ); + $this->assertEquals( '', $this->MslsTaxonomyFactory( true )->acl_request() ); } public function test_get_post_type(): void { Functions\expect( 'get_query_var' )->once()->with( 'taxonomy' )->andReturn( 'category' ); - $this->assertEquals( '', $this->get_test()->get_post_type() ); + $this->assertEquals( '', $this->MslsTaxonomyFactory()->get_post_type() ); } public function test_is_post_type(): void { Functions\expect( 'get_query_var' )->once()->with( 'taxonomy' )->andReturn( 'category' ); - $this->assertFalse( $this->get_test()->is_post_type() ); + $this->assertFalse( $this->MslsTaxonomyFactory()->is_post_type() ); } public function test_is_taxonomy(): void { Functions\expect( 'get_query_var' )->once()->with( 'taxonomy' )->andReturn( 'category' ); - $this->assertTrue( $this->get_test()->is_taxonomy() ); + $this->assertTrue( $this->MslsTaxonomyFactory()->is_taxonomy() ); } public function test_get_request_empty(): void { Functions\expect( 'get_query_var' )->twice()->with( 'taxonomy' )->andReturn( 'category' ); Functions\expect( 'filter_has_var' )->twice()->with( INPUT_GET, MslsFields::FIELD_TAXONOMY )->andReturn( false ); - $this->assertEquals( 'category', $this->get_test()->get_request() ); + $this->assertEquals( 'category', $this->MslsTaxonomyFactory()->get_request() ); } public function test_get_request_not_empty(): void { @@ -73,6 +66,6 @@ public function test_get_request_not_empty(): void { Functions\expect( 'filter_has_var' )->twice()->with( INPUT_GET, MslsFields::FIELD_TAXONOMY )->andReturn( true ); Functions\expect( 'filter_input' )->twice()->with( INPUT_GET, MslsFields::FIELD_TAXONOMY, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( $taxonomy ); - $this->assertEquals( $taxonomy, $this->get_test()->get_request() ); + $this->assertEquals( $taxonomy, $this->MslsTaxonomyFactory()->get_request() ); } } diff --git a/tests/phpunit/TestMslsWidget.php b/tests/phpunit/TestMslsWidget.php index 281c4023..bd271e58 100644 --- a/tests/phpunit/TestMslsWidget.php +++ b/tests/phpunit/TestMslsWidget.php @@ -8,13 +8,7 @@ use lloc\Msls\MslsOutput; use lloc\Msls\MslsWidget; -class TestMslsWidget extends MslsUnitTestCase { - - protected function setUp(): void { - parent::setUp(); - - $this->test = new MslsWidget(); - } +final class TestMslsWidget extends MslsUnitTestCase { public function test_init(): void { $options = \Mockery::mock( MslsOptions::class ); @@ -25,7 +19,7 @@ public function test_init(): void { $this->expectNotToPerformAssertions(); - $this->test->init(); + MslsWidget::init(); } public function test_widget(): void { @@ -48,20 +42,25 @@ public function test_widget(): void { Functions\expect( 'msls_output' )->once()->andReturn( MslsOutput::create() ); $this->expectOutputString( '

Test

No available translations found
' ); - $this->test->widget( array(), array( 'title' => 'Test' ) ); + ( new MslsWidget() )->widget( array(), array( 'title' => 'Test' ) ); } - public function test_update(): void { - Functions\expect( 'wp_strip_all_tags' )->twice()->andReturnFirstArg(); - - $result = $this->test->update( array(), array() ); - $this->assertEquals( array(), $result ); + public static function update_provider(): array { + return array( + array( array(), array(), array(), 0 ), + array( array( 'title' => 'abc' ), array(), array( 'title' => 'abc' ), 1 ), + array( array( 'title' => 'xyz' ), array( 'title' => 'abc' ), array( 'title' => 'xyz' ), 1 ), + ); + } - $result = $this->test->update( array( 'title' => 'abc' ), array() ); - $this->assertEquals( array( 'title' => 'abc' ), $result ); + /** + * @dataProvider update_provider + */ + public function test_update( array $new_instance, array $old_instance, array $expected, int $times ): void { + Functions\expect( 'wp_strip_all_tags' )->times( $times )->andReturnFirstArg(); - $result = $this->test->update( array( 'title' => 'xyz' ), array( 'title' => 'abc' ) ); - $this->assertEquals( array( 'title' => 'xyz' ), $result ); + $result = ( new MslsWidget() )->update( $new_instance, $old_instance ); + $this->assertEquals( $expected, $result ); } public function test_form(): void { @@ -69,7 +68,7 @@ public function test_form(): void { $this->expectOutputString( $expected ); - $result = $this->test->form( array() ); + $result = ( new MslsWidget() )->form( array() ); $this->assertEquals( $expected, $result ); } }