Completed
Pull Request — master (#289)
by Gavin
16:24 queued 13:47
created

VarnishTest::getProxyClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

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