Skip to content

Commit d298dbe

Browse files
authored
Increase code coverage (#22)
* Run tests with coverage * Add missing tests.
1 parent e98dd74 commit d298dbe

File tree

7 files changed

+229
-3
lines changed

7 files changed

+229
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+

bin/run-tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
phpunit --coverage-html target/test-coverage

test/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ function _manually_load_plugin() {
3232

3333
require 'class-basetestcase.php';
3434
require 'class-permalinksteps.php';
35+
require 'class-customposttypesteps.php';
3536
require 'class-permalinkasserter.php';
3637
require 'class-navigationasserter.php';

test/class-basetestcase.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class BaseTestCase extends WP_UnitTestCase {
1717
*/
1818
protected $permalink_steps;
1919

20+
/**
21+
* The CustomPostTypeSteps.
22+
*
23+
* @var CustomPostTypeSteps
24+
*/
25+
protected $custom_post_type_steps;
26+
2027
/**
2128
* The PermalinkAsserter.
2229
*
@@ -37,8 +44,9 @@ class BaseTestCase extends WP_UnitTestCase {
3744
public function setUp() {
3845
parent::setUp();
3946

40-
$this->permalink_steps = new PermalinkSteps( $this );
41-
$this->permalink_asserter = new PermalinkAsserter( $this );
42-
$this->navigation_asserter = new NavigationAsserter( $this );
47+
$this->permalink_steps = new PermalinkSteps( $this );
48+
$this->custom_post_type_steps = new CustomPostTypeSteps( $this );
49+
$this->permalink_asserter = new PermalinkAsserter( $this );
50+
$this->navigation_asserter = new NavigationAsserter( $this );
4351
}
4452
}

test/class-customposttypesteps.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Tests util file.
4+
*
5+
* @package WordPress_Custom_Fields_Permalink
6+
*/
7+
8+
/**
9+
* Class CustomPostTypeSteps contains utility methods to setup custom post types.
10+
*/
11+
class CustomPostTypeSteps {
12+
13+
/**
14+
* CustomPostTypeSteps constructor.
15+
*/
16+
public function __construct() {
17+
}
18+
19+
/**
20+
* Sets the given custom post type.
21+
*
22+
* @param string $post_type Post type.
23+
*
24+
* @return CustomPostTypeStepsBuilder
25+
*/
26+
public function given_custom_post_type( $post_type ) {
27+
return new CustomPostTypeStepsBuilder( $post_type );
28+
}
29+
}
30+
31+
/**
32+
* Class CustomPostTypeStepsBuiler contains utility methods to setup custom post types.
33+
*/
34+
class CustomPostTypeStepsBuilder {
35+
36+
/**
37+
* Custom post type name.
38+
*
39+
* @var string
40+
*/
41+
private $post_type = null;
42+
43+
/**
44+
* Custom post type rewrite slug.
45+
*
46+
* @var string
47+
*/
48+
private $rewrite_slug = null;
49+
50+
/**
51+
* PermalinkSteps constructor.
52+
*
53+
* @param string $post_type Post type.
54+
*/
55+
public function __construct( $post_type ) {
56+
$this->post_type = $post_type;
57+
}
58+
59+
/**
60+
* Sets the given permalink structure.
61+
*/
62+
public function end() {
63+
$this->init();
64+
}
65+
66+
/**
67+
* Initializes all.
68+
*/
69+
private function init() {
70+
$args = array(
71+
'label' => $this->post_type,
72+
'supports' => array( 'title', 'editor', 'custom-fields' ),
73+
'public' => true,
74+
);
75+
76+
if ( $this->rewrite_slug ) {
77+
$args['rewrite'] = array(
78+
'slug' => $this->rewrite_slug,
79+
);
80+
}
81+
82+
register_post_type( $this->post_type, $args );
83+
84+
flush_rewrite_rules();
85+
}
86+
87+
/**
88+
* Sets the given rewrite slug.
89+
*
90+
* @param string $rewrite_slug Rewrite slug.
91+
*
92+
* @return CustomPostTypeStepsBuilder
93+
*/
94+
public function with_rewrite_slug( $rewrite_slug ) {
95+
$this->rewrite_slug = $rewrite_slug;
96+
return $this;
97+
}
98+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Tests case.
4+
*
5+
* @package WordPress_Custom_Fields_Permalink
6+
*/
7+
8+
/**
9+
* Class CustomPostTypeWithMetaKey
10+
*/
11+
class CustomPostTypeWithMetaKey extends BaseTestCase {
12+
13+
/**
14+
* Test case.
15+
*/
16+
function test_generates_permalink_to_post_using_meta_key() {
17+
// given.
18+
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
19+
20+
$this->custom_post_type_steps->given_custom_post_type( 'custom_post_type' )
21+
->with_rewrite_slug( 'custom/%field_some_meta_key%' )
22+
->end();
23+
24+
$post_params = array(
25+
'post_title' => 'Some post title',
26+
'post_type' => 'custom_post_type',
27+
'meta_input' => array(
28+
'some_meta_key' => 'Some meta value',
29+
'some_other_meta_key' => 'Some other meta value',
30+
),
31+
);
32+
$created_post_id = $this->factory()->post->create( $post_params );
33+
34+
// when & then.
35+
$this->permalink_asserter->has_permalink( $created_post_id, '/custom/some-meta-value/some-post-title/' );
36+
}
37+
38+
/**
39+
* Test case.
40+
*/
41+
function test_go_to_post_using_meta_key_permalink_structure() {
42+
// given.
43+
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
44+
45+
$this->custom_post_type_steps->given_custom_post_type( 'custom_post_type' )
46+
->with_rewrite_slug( 'custom/%field_some_meta_key%' )
47+
->end();
48+
49+
$post_params = array(
50+
'post_title' => 'Some post title',
51+
'post_type' => 'custom_post_type',
52+
'meta_input' => array(
53+
'some_meta_key' => 'Some meta value',
54+
'some_other_meta_key' => 'Some other meta value',
55+
),
56+
);
57+
$created_post_id = $this->factory()->post->create( $post_params );
58+
59+
// when.
60+
$this->go_to( '/custom/some-meta-value/some-post-title/' );
61+
62+
// then.
63+
$this->navigation_asserter->then_displayed_post( $created_post_id );
64+
}
65+
66+
/**
67+
* Test case.
68+
*/
69+
function test_not_go_to_the_post_when_invalid_value_of_meta_key_part_in_url() {
70+
// given.
71+
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
72+
73+
$this->custom_post_type_steps->given_custom_post_type( 'custom_post_type' )
74+
->with_rewrite_slug( '%field_some_meta_key%' )
75+
->end();
76+
77+
$post_params = array(
78+
'post_title' => 'Some post title',
79+
'post_type' => 'custom_post_type',
80+
'meta_input' => array(
81+
'some_meta_key' => 'Some meta value',
82+
),
83+
);
84+
$created_post_id = $this->factory()->post->create( $post_params );
85+
86+
// when.
87+
$this->go_to( '/custom/some-different-meta-value/some-post-title/' );
88+
89+
// then.
90+
$this->navigation_asserter->then_not_displayed_post( $created_post_id );
91+
}
92+
}

test/suites/MetaKeyPermalinkStructure/PostWithMetaKey.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,26 @@ function test_go_to_post_using_meta_key_permalink_structure() {
5252
// then.
5353
$this->navigation_asserter->then_displayed_post( $created_post_id );
5454
}
55+
56+
/**
57+
* Test case.
58+
*/
59+
function test_not_go_to_the_post_when_invalid_value_of_meta_key_part_in_url() {
60+
// given.
61+
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
62+
63+
$post_params = array(
64+
'post_title' => 'Some post title',
65+
'meta_input' => array(
66+
'some_meta_key' => 'Some meta value',
67+
),
68+
);
69+
$created_post_id = $this->factory()->post->create( $post_params );
70+
71+
// when.
72+
$this->go_to( '/some-different-meta-value/some-post-title/' );
73+
74+
// then.
75+
$this->navigation_asserter->then_not_displayed_post( $created_post_id );
76+
}
5577
}

0 commit comments

Comments
 (0)