-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathSystemTestCase.php
More file actions
118 lines (95 loc) · 3.39 KB
/
SystemTestCase.php
File metadata and controls
118 lines (95 loc) · 3.39 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/*
* This file is part of the PHPBench package
*
* (c) Daniel Leech <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PhpBench\Tests\System;
use Exception;
use DOMDocument;
use RuntimeException;
use DOMXPath;
use PhpBench\Dom\Document;
use PhpBench\Tests\IntegrationTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;
class SystemTestCase extends IntegrationTestCase
{
protected string $fname;
protected function setUp(): void
{
$this->fname = $this->workspace()->path('testfile');
$this->workspace()->reset();
$filesystem = new Filesystem();
$filesystem->mirror(__DIR__ . '/env', $this->workspace()->path('/env'));
$filesystem->mirror(__DIR__ . '/benchmarks', $this->workspace()->path('/benchmarks'));
$filesystem->mirror(__DIR__ . '/bootstrap', $this->workspace()->path('/bootstrap'));
}
public function getBenchResult(?string $benchmark = null, string $extraCmd = ''): Document
{
$benchmark = $benchmark ?: 'benchmarks/set4';
$process = $this->phpbench(
'run ' . $benchmark . ' --executor=debug --dump-file=' . $this->fname . ' ' . $extraCmd
);
if ($process->getExitCode() !== 0) {
throw new Exception('Could not generate test data:' . $process->getErrorOutput() . $process->getOutput());
}
$document = new Document();
$document->load($this->fname);
return $document;
}
/**
* @param list<string>|string $command
*/
public function phpbench(array|string $command, ?string $cwd = null): Process
{
$cwd = $this->workspace()->path($cwd);
$bin = __DIR__ . '/../../bin/phpbench';
if (is_array($command)) {
$process = new Process([$bin, ...$command], $cwd);
} else {
$bin .= ' --verbose --no-ansi';
$process = Process::fromShellCommandline($bin . ' ' . $command, $cwd);
}
$process->run();
return $process;
}
protected static function assertExitCode(int $expected, Process $process): void
{
$exitCode = $process->getExitCode();
if ($exitCode !== $expected) {
self::fail(sprintf(
'Expected exit code "%s" from "%s" but got "%s": STDOUT: %s ERR: %s',
$expected,
$process->getCommandLine(),
$exitCode,
$process->getOutput(),
$process->getErrorOutput()
));
}
self::assertEquals($expected, $exitCode);
}
protected function assertXPathCount(int $count, string $xmlString, string $query): void
{
$this->assertXPathExpression($count, $xmlString, sprintf('count(%s)', $query));
}
protected function assertXPathExpression(int $expected, string $xmlString, string $expression): void
{
$dom = new DOMDocument();
$result = @$dom->loadXML($xmlString);
if (false === $result) {
throw new RuntimeException(sprintf(
'Could not load XML "%s"',
$xmlString
));
}
$xpath = new DOMXPath($dom);
$dom->formatOutput = true;
$result = $xpath->evaluate($expression);
$this->assertEquals($expected, $result);
}
}