SymfonyTest::getCachingProxyPort()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 2
cts 2
cp 1
crap 2
rs 10
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
use Toflar\Psr6HttpCacheStore\Psr6Store;
0 ignored issues
show
Bug introduced by
The type Toflar\Psr6HttpCacheStore\Psr6Store was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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