Completed
Pull Request — master (#316)
by David
17:11 queued 14:25
created

VarnishTest::getBinary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
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\HttpDispatcher;
15
use FOS\HttpCache\ProxyClient\Varnish;
16
use FOS\HttpCache\Test\Proxy\VarnishProxy;
17
18
/**
19
 * Starts and clears the Varnish proxy between tests.
20
 *
21
 * You can define constants in your phpunit to control how this test behaves.
22
 *
23
 * Note that the WEB_SERVER_HOSTNAME must also match with what you have in your
24
 * .vcl file.
25
 *
26
 * To define constants in the phpunit file, use this syntax:
27
 * <php>
28
 *     <const name="VARNISH_FILE" value="./tests/Functional/Fixtures/varnish-3/fos.vcl" />
29
 * </php>
30
 *
31
 * VARNISH_FILE         Varnish configuration file (required if not passed to setUp)
32
 * VARNISH_BINARY       executable for Varnish. this can also be the full path
33
 *                      to the file if the binary is not automatically found
34
 *                      (default varnishd)
35
 * VARNISH_PORT         port Varnish listens on (default 6181)
36
 * VARNISH_MGMT_PORT    Varnish management port (default 6182)
37
 * VARNISH_CACHE_DIR    directory to use for cache
38
 *                      (default sys_get_temp_dir() + '/foshttpcache-varnish')
39
 * WEB_SERVER_HOSTNAME  hostname/IP your application can be reached at (required)
40
 *
41
 * If you want to test with Varnish 3, you need to define an environment
42
 * variable with the varnish version:
43
 * <php>
44
 *     <env name="VARNISH_VERSION" value="3" />
45
 * </php>
46
 */
47
trait VarnishTest
48
{
49
    /**
50
     * @var VarnishProxy
51
     */
52
    protected $proxy;
53
54
    /**
55
     * @var Varnish
56
     */
57
    protected $proxyClient;
58
59
    /**
60
     * Start Varnish and discard any cached content.
61
     */
62 23
    protected function setUp()
63
    {
64 23
        $this->getProxy()->clear();
65 23
    }
66
67
    /**
68
     * Stop Varnish.
69
     */
70 24
    protected function tearDown()
71
    {
72 24
        $this->getProxy()->stop();
73 24
    }
74
75
    /**
76
     * The default implementation looks at the constant VARNISH_FILE.
77
     *
78
     * @throws \Exception
79
     *
80
     * @return string the path to the varnish server configuration file to use with this test
81
     */
82 11
    protected function getConfigFile()
83
    {
84
        // @codeCoverageIgnoreStart
85
        if (!defined('VARNISH_FILE')) {
86
            throw new \Exception(
87
                'Specify the varnish configuration file path in phpunit.xml or override getConfigFile()'
88
            );
89
        }
90
        // @codeCoverageIgnoreEnd
91
92 11
        return VARNISH_FILE;
93
    }
94
95
    /**
96
     * Get Varnish binary.
97
     *
98
     * @return string
99
     */
100 23
    protected function getBinary()
101
    {
102 23
        return defined('VARNISH_BINARY') ? VARNISH_BINARY : null;
103
    }
104
105
    /**
106
     * Get Varnish port.
107
     *
108
     * @return int
109
     */
110 23
    protected function getCachingProxyPort()
111
    {
112 23
        return defined('VARNISH_PORT') ? VARNISH_PORT : 6181;
113
    }
114
115
    /**
116
     * Get Varnish management port.
117
     *
118
     * @return int
119
     */
120 23
    protected function getVarnishMgmtPort()
121
    {
122 23
        return defined('VARNISH_MGMT_PORT') ? VARNISH_MGMT_PORT : null;
123
    }
124
125
    /**
126
     * Get Varnish cache directory.
127
     *
128
     * @return string
129
     */
130 23
    protected function getCacheDir()
131
    {
132 23
        return defined('VARNISH_CACHE_DIR') ? VARNISH_CACHE_DIR : null;
133
    }
134
135
    /**
136
     * Defaults to 4.
137
     *
138
     * @return int
139
     */
140 13
    protected function getVarnishVersion()
141
    {
142 13
        return getenv('VARNISH_VERSION') ?: '4.0';
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 24
    protected function getProxy()
149
    {
150 24
        if (null === $this->proxy) {
151 24
            $this->proxy = new VarnishProxy($this->getConfigFile());
152 24
            if ($this->getBinary()) {
153 1
                $this->proxy->setBinary($this->getBinary());
154 1
            }
155
156 24
            if ($this->getCachingProxyPort()) {
157 24
                $this->proxy->setPort($this->getCachingProxyPort());
158 24
            }
159
160 24
            if ($this->getVarnishMgmtPort()) {
161 1
                $this->proxy->setManagementPort($this->getVarnishMgmtPort());
162 1
            }
163
164 24
            if ($this->getCacheDir()) {
165 1
                $this->proxy->setCacheDir($this->getCacheDir());
166 1
            }
167 24
        }
168
169 24
        return $this->proxy;
170
    }
171
172
    /**
173
     * Get Varnish proxy client.
174
     *
175
     * @return Varnish
176
     */
177 10 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...
178
    {
179 10
        if (null === $this->proxyClient) {
180 10
            $httpDispatcher = new HttpDispatcher(
181 10
                ['http://127.0.0.1:'.$this->getCachingProxyPort()],
182 10
                $this->getHostName().':'.$this->getCachingProxyPort()
183 10
            );
184 10
            $this->proxyClient = new Varnish($httpDispatcher);
185 10
        }
186
187 10
        return $this->proxyClient;
188
    }
189
190
    /**
191
     * Get the hostname where your application can be reached.
192
     *
193
     * @throws \Exception
194
     *
195
     * @return string
196
     */
197 23
    protected function getHostName()
198
    {
199
        // @codeCoverageIgnoreStart
200
        if (!defined('WEB_SERVER_HOSTNAME')) {
201
            throw new \Exception(
202
                'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml'
203
            );
204
        }
205
        // @codeCoverageIgnoreEnd
206
207 23
        return WEB_SERVER_HOSTNAME;
208
    }
209
}
210