Completed
Pull Request — master (#257)
by David de
04:22
created

SymfonyTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 88.46%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 2
cbo 2
dl 0
loc 92
ccs 23
cts 26
cp 0.8846
rs 10

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() 0 14 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\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 5
    protected function getHostName()
80
    {
81 5
        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 5
        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 4
    protected function getProxyClient()
112
    {
113 4
        if (null === $this->proxyClient) {
114 4
            $this->proxyClient = new Symfony(
115 4
                ['http://127.0.0.1:' . $this->getCachingProxyPort()],
116
                [
117 4
                    'base_uri' => $this->getHostName() . ':' . $this->getCachingProxyPort(),
118
                    'purge_method' => 'NOTIFY'
119 4
                ]
120 4
            );
121 4
        }
122
123 4
        return $this->proxyClient;
124
    }
125
}
126