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

NginxTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 132
Duplicated Lines 11.36 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.22%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 15
loc 132
ccs 37
cts 45
cp 0.8222
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 4 1
A getConfigFile() 0 12 2
A getBinary() 0 4 2
A getCachingProxyPort() 0 4 2
A getCacheDir() 0 4 2
A getHostName() 0 10 2
A getProxy() 0 17 4
A getProxyClient() 15 15 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\Nginx;
16
use FOS\HttpCache\Test\Proxy\NginxProxy;
17
18
/**
19
 * Starts and clears the NGINX proxy between tests.
20
 *
21
 * You can define constants in your phpunit.xml to control how this test behaves.
22
 *
23
 * Note that the WEB_SERVER_HOSTNAME must also match with what you have in your
24
 * NGINX configuration file.
25
 *
26
 * To define constants in the phpunit file, use this syntax:
27
 * <php>
28
 *     <const name="NGINX_FILE" value="./tests/FOS/HttpCache/Tests/Functional/Fixtures/nginx/fos.conf" />
29
 * </php>
30
 *
31
 * NGINX_FILE          NGINX configuration file (required if not passed to setUp)
32
 * NGINX_BINARY        Executable for NGINX. This can also be the full path
33
 *                     to the file if the binary is not automatically found
34
 *                     (default nginx)
35
 * NGINX_PORT          Port NGINX listens to (default 8088)
36
 * NGINX_CACHE_PATH    directory to use for cache
37
 *                     Must match `proxy_cache_path` directive in
38
 *                     your configuration file.
39
 *                     (default sys_get_temp_dir() + '/foshttpcache-nginx')
40
 * WEB_SERVER_HOSTNAME hostname where your application can be reached (required)
41
 */
42
trait NginxTest
43
{
44
    /**
45
     * @var Nginx
46
     */
47
    protected $proxyClient;
48
49
    /**
50
     * @var NginxProxy
51
     */
52
    protected $proxy;
53
54 6
    protected function setUp()
55
    {
56 6
        $this->getProxy()->clear();
57 6
    }
58
59 6
    protected function tearDown()
60
    {
61 6
        $this->getProxy()->stop();
62 6
    }
63
64
    /**
65
     * The default implementation looks at the constant NGINX_FILE.
66
     *
67
     * @throws \Exception
68
     *
69
     * @return string the path to the NGINX server configuration file to use with this test
70
     */
71 6
    protected function getConfigFile()
72
    {
73 6
        if (!defined('NGINX_FILE')) {
74
            throw new \Exception(
75
                'Specify the NGINX'
76
                .' configuration file path in phpunit.xml or override getConfigFile()'
77
            );
78
        }
79
80
        // NGINX needs an absolute path
81 6
        return realpath(NGINX_FILE);
82
    }
83
84
    /**
85
     * Defaults to "nginx".
86
     *
87
     * @return string
88
     */
89 6
    protected function getBinary()
90
    {
91 6
        return defined('NGINX_BINARY') ? NGINX_BINARY : null;
92
    }
93
94
    /**
95
     * Defaults to 8088.
96
     *
97
     * @return int
98
     */
99 6
    protected function getCachingProxyPort()
100
    {
101 6
        return defined('NGINX_PORT') ? NGINX_PORT : 8088;
102
    }
103
104
    /**
105
     * Get NGINX cache directory.
106
     */
107 6
    protected function getCacheDir()
108
    {
109 6
        return defined('NGINX_CACHE_PATH') ? NGINX_CACHE_PATH : null;
110
    }
111
112
    /**
113
     * Get the hostname where your application can be reached.
114
     *
115
     * @throws \Exception
116
     *
117
     * @return string
118
     */
119 6
    protected function getHostName()
120
    {
121 6
        if (!defined('WEB_SERVER_HOSTNAME')) {
122
            throw new \Exception(
123
                'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml'
124
            );
125
        }
126
127 6
        return WEB_SERVER_HOSTNAME;
128
    }
129
130
    /**
131
     * @return NginxProxy
132
     */
133 6
    protected function getProxy()
134
    {
135 6
        if (null === $this->proxy) {
136 6
            $this->proxy = new NginxProxy($this->getConfigFile());
137 6
            $this->proxy->setPort($this->getCachingProxyPort());
138
139 6
            if ($this->getBinary()) {
140
                $this->proxy->setBinary($this->getBinary());
141
            }
142
143 6
            if ($this->getCacheDir()) {
144
                $this->proxy->setCacheDir($this->getCacheDir());
145
            }
146 6
        }
147
148 6
        return $this->proxy;
149
    }
150
151
    /**
152
     * Get proxy client.
153
     *
154
     * @param string $purgeLocation Optional purgeLocation
155
     *
156
     * @return Nginx
157
     */
158 6 View Code Duplication
    protected function getProxyClient($purgeLocation = '')
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...
159
    {
160 6
        if (null === $this->proxyClient) {
161 6
            $httpDispatcher = new HttpDispatcher(
162 6
                ['http://127.0.0.1:'.$this->getCachingProxyPort()],
163 6
                $this->getHostName().':'.$this->getCachingProxyPort()
164 6
            );
165
166 6
            $this->proxyClient = new Nginx($httpDispatcher, [
167 6
                'purge_location' => $purgeLocation,
168 6
            ]);
169 6
        }
170
171 6
        return $this->proxyClient;
172
    }
173
}
174