Completed
Pull Request — master (#365)
by Alessandro
03:17
created

WebServerListener::addRiskyTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 3
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 3
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
crap 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 38 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 PHPUnit\Framework\AssertionFailedError;
15
use PHPUnit\Framework\Test;
16
use PHPUnit\Framework\TestListener;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, FOS\HttpCache\Test\TestListener.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use PHPUnit\Framework\TestSuite;
18
use PHPUnit\Framework\Warning;
19
20
if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
21
    class_alias('FOS\HttpCache\Test\Legacy\WebServerListener', 'FOS\HttpCache\Test\WebServerListener');
22
// Using an early return instead of a else does not work when using the PHPUnit phar due to some weird PHP behavior (the class
23
// gets defined without executing the code before it and so the definition is not properly conditional)
24
} else {
25
    /**
26
     * A PHPUnit test listener that starts and stops the PHP built-in web server.
27
     *
28
     * This listener is configured with a couple of constants from the phpunit.xml
29
     * file. To define constants in the phpunit file, use this syntax:
30
     * <php>
31
     *     <const name="WEB_SERVER_HOSTNAME" value="localhost" />
32
     * </php>
33
     *
34
     * WEB_SERVER_HOSTNAME host name of the web server (required)
35
     * WEB_SERVER_PORT     port to listen on (required)
36
     * WEB_SERVER_DOCROOT  path to the document root for the server (required)
37
     */
38 View Code Duplication
    class WebServerListener implements TestListener
0 ignored issues
show
Duplication introduced by
This class 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...
39
    {
40
        /** @var WebServerListenerTrait */
41
        private $trait;
42
43
        public function __construct()
44
        {
45
            $this->trait = new WebServerListenerTrait();
46
        }
47
48
        /**
49
         * Make sure the PHP built-in web server is running for tests with group
50
         * 'webserver'.
51
         */
52
        public function startTestSuite(TestSuite $suite)
53
        {
54
            $this->trait->startTestSuite($suite);
55
        }
56
57
        /**
58
         *  We don't need these.
59
         */
60
        public function endTestSuite(TestSuite $suite)
61
        {
62
        }
63
64
        public function addError(Test $test, \Exception $e, $time)
65
        {
66
        }
67
68
        public function addFailure(Test $test, AssertionFailedError $e, $time)
69
        {
70
        }
71
72
        public function addIncompleteTest(Test $test, \Exception $e, $time)
73
        {
74
        }
75
76
        public function addSkippedTest(Test $test, \Exception $e, $time)
77
        {
78
        }
79
80
        public function startTest(Test $test)
81
        {
82
        }
83
84
        public function endTest(Test $test, $time)
85
        {
86
        }
87
88
        public function addRiskyTest(Test $test, \Exception $e, $time)
89
        {
90
        }
91
92
        public function addWarning(Test $test, Warning $e, $time)
93
        {
94
        }
95
96
        /**
97
         * Get web server hostname.
98
         *
99
         * @throws \Exception
100
         *
101
         * @return string
102
         */
103
        protected function getHostName()
104
        {
105
            return $this->trait->getHostName();
106
        }
107
108
        /**
109
         * Get web server port.
110
         *
111
         * @throws \Exception
112
         *
113
         * @return int
114
         */
115
        protected function getPort()
116
        {
117
            return $this->trait->getPort();
118
        }
119
120
        /**
121
         * Get web server port.
122
         *
123
         * @throws \Exception
124
         *
125
         * @return int
126
         */
127
        protected function getDocRoot()
128
        {
129
            return $this->trait->getDocRoot();
130
        }
131
132
        /**
133
         * Start PHP built-in web server.
134
         *
135
         * @return int PID
136
         */
137
        protected function startPhpWebServer()
138
        {
139
            return $this->trait->startPhpWebServer();
140
        }
141
142
        /**
143
         * Wait for caching proxy to be started up and reachable.
144
         *
145
         * @param string $ip
146
         * @param int    $port
147
         * @param int    $timeout Timeout in milliseconds
148
         *
149
         * @throws \RuntimeException If proxy is not reachable within timeout
150
         */
151
        protected function waitFor($ip, $port, $timeout)
152
        {
153
            $this->trait->waitFor($ip, $port, $timeout);
154
        }
155
    }
156
}
157