Skip to content

Commit de96af6

Browse files
committed
Unit tests added
1 parent b23adfc commit de96af6

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

tests/TestMslsOptionsQuery.php

+58-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,67 @@
55
use Brain\Monkey\Functions;
66

77
use lloc\Msls\MslsOptionsQuery;
8+
use lloc\Msls\MslsOptionsQueryAuthor;
9+
use lloc\Msls\MslsOptionsQueryDay;
10+
use lloc\Msls\MslsOptionsQueryMonth;
11+
use lloc\Msls\MslsOptionsQueryPostType;
12+
use lloc\Msls\MslsOptionsQueryYear;
813

914
/**
1015
* TestMslsOptionsQuery
1116
*/
1217
class TestMslsOptionsQuery extends MslsUnitTestCase {
1318

14-
public function test_create(): void {
19+
public function test_create_is_day(): void {
20+
Functions\expect( 'is_day' )->once()->andReturn( true );
21+
Functions\expect( 'get_query_var' )->times( 3 )->andReturnValues( array( 1969, 6, 26 ) );
22+
Functions\expect( 'get_option' )->once();
23+
24+
$this->assertInstanceOf( MslsOptionsQueryDay::class, MslsOptionsQuery::create() );
25+
}
26+
public function test_create_is_month(): void {
27+
Functions\expect( 'is_day' )->once()->andReturn( false );
28+
Functions\expect( 'is_month' )->once()->andReturn( true );
29+
Functions\expect( 'get_query_var' )->twice()->andReturnValues( array( 1969, 6 ) );
30+
Functions\expect( 'get_option' )->once();
31+
32+
$this->assertInstanceOf( MslsOptionsQueryMonth::class, MslsOptionsQuery::create() );
33+
}
34+
35+
public function test_create_is_year(): void {
36+
Functions\expect( 'is_day' )->once()->andReturn( false );
37+
Functions\expect( 'is_month' )->once()->andReturn( false );
38+
Functions\expect( 'is_year' )->once()->andReturn( true );
39+
Functions\expect( 'get_query_var' )->once()->andReturn( 1969 );
40+
Functions\expect( 'get_option' )->once();
41+
42+
$this->assertInstanceOf( MslsOptionsQueryYear::class, MslsOptionsQuery::create() );
43+
}
44+
45+
public function test_create_is_author(): void {
46+
Functions\expect( 'is_day' )->once()->andReturn( false );
47+
Functions\expect( 'is_month' )->once()->andReturn( false );
48+
Functions\expect( 'is_year' )->once()->andReturn( false );
49+
Functions\expect( 'is_author' )->once()->andReturn( true );
50+
Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 );
51+
Functions\expect( 'get_option' )->once();
52+
53+
$this->assertInstanceOf( MslsOptionsQueryAuthor::class, MslsOptionsQuery::create() );
54+
}
55+
56+
public function test_create_is_post_type_archive(): void {
57+
Functions\expect( 'is_day' )->once()->andReturn( false );
58+
Functions\expect( 'is_month' )->once()->andReturn( false );
59+
Functions\expect( 'is_year' )->once()->andReturn( false );
60+
Functions\expect( 'is_author' )->once()->andReturn( false );
61+
Functions\expect( 'is_post_type_archive' )->once()->andReturn( true );
62+
Functions\expect( 'get_query_var' )->once()->andReturn( 'book' );
63+
Functions\expect( 'get_option' )->once();
64+
65+
$this->assertInstanceOf( MslsOptionsQueryPostType::class, MslsOptionsQuery::create() );
66+
}
67+
68+
public function test_create_is_null(): void {
1569
Functions\expect( 'is_day' )->once()->andReturn( false );
1670
Functions\expect( 'is_month' )->once()->andReturn( false );
1771
Functions\expect( 'is_year' )->once()->andReturn( false );
@@ -24,16 +78,15 @@ public function test_create(): void {
2478
public function test_current_get_postlink(): void {
2579
$home_url = 'https://example.org/';
2680

27-
Functions\expect( 'get_option' )->once()->andReturn( [ 'de_DE' => 42 ] );
81+
Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) );
2882
Functions\expect( 'home_url' )->once()->andReturn( $home_url );
2983

3084
$this->assertEquals( $home_url, ( new MslsOptionsQuery() )->get_postlink( 'de_DE' ) );
3185
}
3286

3387
public function test_non_existent_get_postlink(): void {
34-
Functions\expect( 'get_option' )->once()->andReturn( [ 'de_DE' => 42 ] );
88+
Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) );
3589

3690
$this->assertEquals( '', ( new MslsOptionsQuery() )->get_postlink( 'fr_FR' ) );
3791
}
38-
39-
}
92+
}

tests/TestMslsOptionsQueryPostType.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,25 @@ class TestMslsOptionsQueryPostType extends MslsUnitTestCase {
1414
protected function setUp(): void {
1515
parent::setUp();
1616

17-
Functions\expect( 'get_option' )->once()->andReturn( [ 'de_DE' => 42 ] );
17+
Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) );
1818

1919
$this->test = new MslsOptionsQueryPostType();
2020
}
2121

22-
public function test_has_value(): void {
23-
$this->assertIsBool( $this->test->has_value( 'de_DE' ) );
22+
public function test_has_value_existing(): void {
23+
$this->assertTrue( $this->test->has_value( 'de_DE' ) );
24+
}
25+
26+
public function test_has_value_not_existing(): void {
27+
$post_type = \Mockery::mock( '\WP_Post_Type' );
28+
Functions\expect( 'get_post_type_object' )->once()->andReturn( $post_type );
29+
30+
$this->assertTrue( $this->test->has_value( 'it_IT' ) );
2431
}
2532

2633
public function test_get_current_link(): void {
2734
Functions\expect( 'get_post_type_archive_link' )->once()->andReturn( 'https://example.org/queried-posttype' );
2835

2936
$this->assertEquals( 'https://example.org/queried-posttype', $this->test->get_current_link() );
3037
}
31-
3238
}

0 commit comments

Comments
 (0)