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

SymfonyTest::getCachingProxyPort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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