Completed
Pull Request — master (#312)
by David
06:00
created

SymfonyTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 3
dl 16
loc 94
ccs 25
cts 28
cp 0.8929
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A getCachingProxyPort() 0 8 2
A getHostName() 0 10 2
A getProxy() 0 8 2
A getProxyClient() 16 16 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
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\Test;
13
14
use FOS\HttpCache\ProxyClient\Http\HttpDispatcher;
15
use FOS\HttpCache\ProxyClient\Symfony;
16
use FOS\HttpCache\Test\Proxy\SymfonyProxy;
17
18
/**
19
 * Clears the Symfony HttpCache proxy between tests.
20
 *
21
 * The webserver with Symfony is to be started with the WebServerListener.
22
 *
23
 * You can define constants in your phpunit to control how this test behaves.
24
 *
25
 * To define constants in the phpunit file, use this syntax:
26
 * <php>
27
 *     <const name="WEB_SERVER_PORT" value="8080" />
28
 * </php>
29
 *
30
 * WEB_SERVER_PORT     port the PHP webserver listens on (required)
31
 * WEB_SERVER_HOSTNAME hostname where your application can be reached (required)
32
 *
33
 * Note that the SymfonyProxy also uses a SYMFONY_CACHE_DIR constant.
34
 */
35
trait SymfonyTest
36
{
37
    /**
38
     * @var Symfony
39
     */
40
    protected $proxyClient;
41
42
    /**
43
     * @var SymfonyProxy
44
     */
45
    protected $proxy;
46
47
    /**
48
     * Clear Symfony HttpCache.
49
     *
50
     * @throws \Exception
51
     */
52 5
    protected function setUp()
53
    {
54 5
        $this->getProxy()->clear();
55 5
    }
56
57
    /**
58
     * Get server port.
59
     *
60
     * @return int
61
     *
62
     * @throws \Exception
63
     */
64 5
    protected function getCachingProxyPort()
65
    {
66 5
        if (!defined('WEB_SERVER_PORT')) {
67
            throw new \Exception('Set WEB_SERVER_PORT in your phpunit.xml');
68
        }
69
70 5
        return WEB_SERVER_PORT;
71
    }
72
73
    /**
74
     * Get the hostname where your application can be reached.
75
     *
76
     * @throws \Exception
77
     *
78
     * @return string
79
     */
80 5
    protected function getHostName()
81
    {
82 5
        if (!defined('WEB_SERVER_HOSTNAME')) {
83
            throw new \Exception(
84
                'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml'
85
            );
86
        }
87
88 5
        return WEB_SERVER_HOSTNAME;
89
    }
90
91
    /**
92
     * @return SymfonyProxy
93
     */
94 5
    protected function getProxy()
95
    {
96 5
        if (null === $this->proxy) {
97 5
            $this->proxy = new SymfonyProxy();
98 5
        }
99
100 5
        return $this->proxy;
101
    }
102
103
    /**
104
     * Get Symfony proxy client.
105
     *
106
     * We use a non-default method for PURGE because the built-in PHP webserver
107
     * does not allow arbitrary HTTP methods.
108
     * https://github.com/php/php-src/blob/PHP-5.4.1/sapi/cli/php_http_parser.c#L78-L102
109
     *
110
     * @return Symfony
111
     */
112 5 View Code Duplication
    protected function getProxyClient()
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...
113
    {
114 5
        if (null === $this->proxyClient) {
115 5
            $httpDispatcher = new HttpDispatcher(
116 5
                ['http://127.0.0.1:'.$this->getCachingProxyPort()],
117 5
                $this->getHostName().':'.$this->getCachingProxyPort()
118 5
            );
119
120 5
            $this->proxyClient = new Symfony($httpDispatcher, [
121 5
                    'purge_method' => 'NOTIFY',
122
                ]
123 5
            );
124 5
        }
125
126 5
        return $this->proxyClient;
127
    }
128
}
129