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

SymfonyTest::getProxyClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

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