Test Failed
Branch master (4a30b9)
by John
02:00
created

PayloadTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 57.14 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 44
loc 77
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_url_query_payload_deprecated_key() 0 9 1
A test_url_query_payload_without_referer() 0 18 2
A test_url_query_payload_with_wp_referer() 22 22 2
A test_url_query_payload_with_server_referer() 22 22 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @package wpmautic\tests
4
 */
5
6
/**
7
 * Ensure that payload data are valid in the current context
8
 */
9
class PayloadTest extends WP_UnitTestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    public function test_url_query_payload_deprecated_key()
12
    {
13
        $payload = wpmautic_get_url_query();
14
15
        $this->assertArrayNotHasKey('url', $payload);
0 ignored issues
show
Bug introduced by
The method assertArrayNotHasKey() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
16
        $this->assertArrayNotHasKey('title', $payload);
0 ignored issues
show
Bug introduced by
The method assertArrayNotHasKey() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
17
        $this->assertArrayHasKey('page_url', $payload);
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
18
        $this->assertArrayHasKey('page_title', $payload);
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
19
    }
20
21
    public function test_url_query_payload_without_referer()
22
    {
23
        $base_url = 'http://example.org';
24
        $this->go_to( $base_url );
0 ignored issues
show
Bug introduced by
The method go_to() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
26
        $payload = wpmautic_get_url_query();
27
28
        $title = sprintf( "%s &#8211; %s", WP_TESTS_TITLE, get_option( 'blogdescription' ) );
29
30
        $this->assertEquals($payload['page_url'], $base_url);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
        $this->assertEquals($payload['page_title'], $title);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
        $this->assertEquals($payload['language'], get_locale());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
        if ( version_compare( get_bloginfo('version'), '4.5', '<' ) ) {
34
            $this->assertEquals($payload['referrer'], null);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
        } else {
36
            $this->assertEquals($payload['referrer'], $base_url);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
        }
38
    }
39
40 View Code Duplication
    public function test_url_query_payload_with_wp_referer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
test_url_query_payload_with_wp_referer uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
41
    {
42
        if ( version_compare( get_bloginfo('version'), '4.5', '<' ) ) {
43
            $this->markTestSkipped( 'wp_get_raw_referer function was introduced in WP 4.5' );
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
            return;
45
        }
46
47
        $base_url = 'http://example.org';
48
        $_REQUEST['_wp_http_referer'] = 'http://toto.org';
49
        $this->go_to( $base_url );
0 ignored issues
show
Bug introduced by
The method go_to() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
51
        $payload = wpmautic_get_url_query();
52
53
        $title = sprintf( "%s &#8211; %s", WP_TESTS_TITLE, get_option( 'blogdescription' ) );
54
55
        $this->assertEquals($payload['page_url'], $base_url);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        $this->assertEquals($payload['page_title'], $title);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        $this->assertEquals($payload['language'], get_locale());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
        $this->assertEquals($payload['referrer'], 'http://toto.org');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
60
        unset($_REQUEST['_wp_http_referer']);
61
    }
62
63 View Code Duplication
    public function test_url_query_payload_with_server_referer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
test_url_query_payload_with_server_referer uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
64
    {
65
        if ( version_compare( get_bloginfo('version'), '4.5', '<' ) ) {
66
            $this->markTestSkipped( 'wp_get_raw_referer function was introduced in WP 4.5' );
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            return;
68
        }
69
70
        $base_url = 'http://example.org';
71
        $_SERVER['HTTP_REFERER'] = 'http://toto.org';
72
        $this->go_to( $base_url );
0 ignored issues
show
Bug introduced by
The method go_to() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
74
        $payload = wpmautic_get_url_query();
75
76
        $title = sprintf( "%s &#8211; %s", WP_TESTS_TITLE, get_option( 'blogdescription' ) );
77
78
        $this->assertEquals($payload['page_url'], $base_url);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
        $this->assertEquals($payload['page_title'], $title);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
        $this->assertEquals($payload['language'], get_locale());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
        $this->assertEquals($payload['referrer'], 'http://toto.org');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<PayloadTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
83
        unset($_SERVER['HTTP_REFERER']);
84
    }
85
}
86