/
VarnishProxyTest.php
98 lines (81 loc) · 2.75 KB
/
VarnishProxyTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\HttpCache\Tests\Unit\Test\Proxy;
use FOS\HttpCache\Test\Proxy\VarnishProxy;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;
class VarnishProxyTest extends TestCase
{
public function testInvalidConfigFileThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot find config file: nope.vcl');
new VarnishProxy('nope.vcl');
}
public static function allowInlineFlagProvider(): array
{
return [[true], [false]];
}
#[PHPUnit\DataProvider('allowInlineFlagProvider')]
public function testStart(bool $inlineC): void
{
$proxy = new VarnishProxyMock('config.vcl');
$proxy->setBinary('/usr/sbin/varnishd');
$proxy->setConfigDir('/my/varnish/dir');
$proxy->setIp('192.168.0.1');
$proxy->setManagementPort(1331);
$proxy->setCacheDir('/tmp/cache/dir');
$proxy->setAllowInlineC($inlineC);
$this->assertEquals($inlineC, $proxy->getAllowInlineC());
$proxy->start();
$this->assertEquals('/usr/sbin/varnishd', $proxy->command);
$vclPath = ((int) getenv('VARNISH_VERSION')) >= 5 ? 'vcl_path' : 'vcl_dir';
$arguments = [
'-a', '192.168.0.1:6181',
'-T', '192.168.0.1:1331',
'-f', 'config.vcl',
'-n', '/tmp/cache/dir',
'-p', $vclPath.'=/my/varnish/dir',
'-P', '/tmp/foshttpcache-varnish.pid',
];
if ($inlineC) {
$arguments[] = '-p';
$arguments[] = 'vcc_allow_inline_c=on';
}
$this->assertEquals($arguments, $proxy->arguments);
}
public function testWaitThrowsException(): void
{
$proxy = new VarnishProxyMock('config.vcl');
$proxy->wait = false;
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Caching proxy cannot be reached at 127.0.0.1:6181');
$proxy->start();
}
}
class VarnishProxyMock extends VarnishProxy
{
public string $command;
public array $arguments;
public bool $wait = true;
public function setConfigFile(string $configFile): void
{
$this->configFile = $configFile;
}
protected function runCommand(string $command, array $arguments): void
{
$this->command = $command;
$this->arguments = $arguments;
}
protected function wait($timeout, $callback): bool
{
return $this->wait;
}
}