From 1276f7c3ecefbaf002501113b7234064700d83b2 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Mon, 7 Aug 2023 16:57:06 +0200 Subject: [PATCH 1/2] Test factory for BlogCollection --- tests/bootstrap.php | 27 +++++++++++++++++++++++++++ tests/test-mslscustomcolumn.php | 13 +++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 1682bd9f..a89f4264 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,6 +2,8 @@ namespace lloc\MslsTests; +use lloc\Msls\MslsBlog; +use lloc\Msls\MslsBlogCollection; use PHPUnit\Framework\TestCase; use Brain\Monkey; use Brain\Monkey\Functions; @@ -25,6 +27,31 @@ protected function setUp(): void { Functions\when( '__' )->returnArg(); } + /** + * Get a of the MslsBlogCollection class that contains some blogs + * + * @param $map + * + * @return void + */ + public function getBlogsCollection( $map = [ 'de_DE' => 'de', 'en_US' => 'en' ] ): MslsBlogCollection { + foreach ( $map as $locale => $alpha2 ) { + $blog = \Mockery::mock( MslsBlog::class ); + $blog->shouldReceive( [ + 'get_alpha2' => $alpha2, + 'get_language' => $locale, + ] ); + + $blogs[] = $blog; + } + + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get_objects' )->andReturn( $blogs ); + $collection->shouldReceive( 'get' )->andReturn( $blogs ); + + return $collection; + } + protected function tearDown(): void { restore_error_handler(); diff --git a/tests/test-mslscustomcolumn.php b/tests/test-mslscustomcolumn.php index 02b74a4b..a80e6dc4 100644 --- a/tests/test-mslscustomcolumn.php +++ b/tests/test-mslscustomcolumn.php @@ -5,10 +5,23 @@ use lloc\Msls\MslsCustomColumn; use lloc\Msls\MslsOptions; use lloc\Msls\MslsBlogCollection; +use Brain\Monkey\Functions; class WP_Test_MslsCustomColumn extends Msls_UnitTestCase { function test_th() { + Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://example.org/added-args' ); + Functions\expect( 'get_the_ID' )->twice()->andReturnValues( [ 1, 2 ] ); + Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 1 ) . '/' ); + + $options = \Mockery::mock( MslsOptions::class ); + $obj = new MslsCustomColumn( $options, $this->getBlogsCollection() ); + + $expected = [ 'mslscol' => 'de_DE en_US' ]; + $this->assertEquals( $expected, $obj->th( [] ) ); + } + + function test_th_empty() { $options = \Mockery::mock( MslsOptions::class ); $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get' )->once()->andReturn( [] ); From 6edfa3f6717db33a7cf6a1a478b82602eb1a9814 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Mon, 7 Aug 2023 17:07:24 +0200 Subject: [PATCH 2/2] Test factory for BlogCollection --- tests/test-mslscustomcolumn.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test-mslscustomcolumn.php b/tests/test-mslscustomcolumn.php index a80e6dc4..a1721841 100644 --- a/tests/test-mslscustomcolumn.php +++ b/tests/test-mslscustomcolumn.php @@ -14,10 +14,10 @@ function test_th() { Functions\expect( 'get_the_ID' )->twice()->andReturnValues( [ 1, 2 ] ); Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 1 ) . '/' ); - $options = \Mockery::mock( MslsOptions::class ); - $obj = new MslsCustomColumn( $options, $this->getBlogsCollection() ); - + $options = \Mockery::mock( MslsOptions::class ); + $obj = new MslsCustomColumn( $options, $this->getBlogsCollection() ); $expected = [ 'mslscol' => 'de_DE en_US' ]; + $this->assertEquals( $expected, $obj->th( [] ) ); }