Completed
Pull Request — master (#444)
by Yanick
12:02
created

LiteSpeedTest::getHostName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\LiteSpeed;
16
use FOS\HttpCache\Test\Proxy\LiteSpeedProxy;
17
18
// TODO: update docs in case anything is configurable
19
/**
20
 * Starts and clears the LiteSpeed proxy between tests.
21
 */
22
trait LiteSpeedTest
23
{
24
    /**
25
     * @var LiteSpeed
26
     */
27
    protected $proxyClient;
28
29
    /**
30
     * @var LiteSpeedProxy
31
     */
32
    protected $proxy;
33
34
    protected function setUp()
35
    {
36
        $this->getProxy()->clear();
37
    }
38
39
    protected function tearDown()
40
    {
41
        $this->getProxy()->stop();
42
    }
43
44
    /**
45
     * Defaults to "/usr/local/lsws/bin/lswsctrl".
46
     *
47
     * @return string
48
     */
49
    protected function getBinary()
50
    {
51
        return defined('LITESPEED_BINARY') ? LITESPEED_BINARY : null;
52
    }
53
54
    /**
55
     * Defaults to 80.
56
     *
57
     * @return int
58
     */
59
    protected function getCachingProxyPort()
60
    {
61
        return defined('LITESPEED_PORT') ? LITESPEED_PORT : 8088;
62
    }
63
64
    /**
65
     * Get the hostname where your application can be reached.
66
     *
67
     * @throws \Exception
68
     *
69
     * @return string
70
     */
71
    protected function getHostName()
72
    {
73
        // @codeCoverageIgnoreStart
74
        if (!defined('WEB_SERVER_HOSTNAME')) {
75
            throw new \Exception(
76
                'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml'
77
            );
78
        }
79
        // @codeCoverageIgnoreEnd
80
81
        return WEB_SERVER_HOSTNAME;
82
    }
83
84
    /**
85
     * @return LiteSpeedProxy
86
     */
87
    protected function getProxy()
88
    {
89
        if (null === $this->proxy) {
90
            $this->proxy = new LiteSpeedProxy();
91
            $this->proxy->setPort($this->getCachingProxyPort());
92
93
            if ($this->getBinary()) {
94
                $this->proxy->setBinary($this->getBinary());
95
            }
96
        }
97
98
        return $this->proxy;
99
    }
100
101
    /**
102
     * Get proxy client.
103
     *
104
     * @return LiteSpeed
105
     */
106
    protected function getProxyClient()
107
    {
108 View Code Duplication
        if (null === $this->proxyClient) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
109
            $httpDispatcher = new HttpDispatcher(
110
                ['http://'.$this->getHostName().':'.$this->getCachingProxyPort()]
111
            );
112
113
            $this->proxyClient = new LiteSpeed($httpDispatcher);
114
        }
115
116
        return $this->proxyClient;
117
    }
118
}
119