Skip to content

Commit c8c6dae

Browse files
committed
Update tests
1 parent 0fd324e commit c8c6dae

File tree

2 files changed

+401
-128
lines changed

2 files changed

+401
-128
lines changed

tests/Pest.php

Lines changed: 128 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,130 @@
11
<?php declare(strict_types=1);
22

3-
/*
4-
|--------------------------------------------------------------------------
5-
| Expectations
6-
|--------------------------------------------------------------------------
7-
|
8-
| When you're writing tests, you often need to check that values meet certain conditions. The
9-
| "expect()" function gives you access to a set of "expectations" methods that you can use
10-
| to assert different things. Of course, you may extend the Expectation API at any time.
11-
|
12-
*/
13-
14-
expect()->extend('toBeOne', function () {
15-
return $this->toBe(1);
16-
});
3+
use Bugo\Sass\CompilerInterface;
4+
use Bugo\Sass\Compiler;
5+
use Mockery\Mock;
6+
use Mockery\MockInterface;
7+
use org\bovigo\vfs\vfsStream;
8+
use Symfony\Component\Process\Process;
9+
10+
function setupCompilerPaths(CompilerInterface $compiler): void
11+
{
12+
$reflection = new ReflectionClass($compiler);
13+
14+
try {
15+
$reflection->getProperty('nodePath')->setValue($compiler, 'node');
16+
$reflection->getProperty('bridgePath')->setValue($compiler, __DIR__ . '/../../bin/bridge.js');
17+
} catch (ReflectionException) {}
18+
}
19+
20+
function mockProcess(string $expectedCss, ?array $expectedSourceMap = null): Mock|(MockInterface&Process)
21+
{
22+
$mockProcess = Mockery::mock(Process::class);
23+
$mockProcess->shouldReceive('setInput')->andReturnSelf();
24+
$mockProcess->shouldReceive('run')->andReturn(0);
25+
$mockProcess->shouldReceive('getOutput')->andReturn(json_encode(['css' => $expectedCss, 'sourceMap' => $expectedSourceMap]));
26+
$mockProcess->shouldReceive('getErrorOutput')->andReturn('');
27+
28+
return $mockProcess;
29+
}
30+
31+
function mockCompiler(): Mock|(MockInterface&Compiler)
32+
{
33+
$mockCompiler = Mockery::mock(Compiler::class)->makePartial();
34+
$mockCompiler->shouldAllowMockingProtectedMethods();
35+
36+
setupCompilerPaths($mockCompiler);
37+
38+
return $mockCompiler;
39+
}
40+
41+
function assertCssEquals(string $actual, string $expected): void
42+
{
43+
expect($actual)->toBe($expected);
44+
}
45+
46+
function setupVfs(array $files = []): string
47+
{
48+
$root = vfsStream::setup();
49+
50+
foreach ($files as $filename => $content) {
51+
vfsStream::newFile($filename)->at($root)->setContent($content);
52+
}
53+
54+
return vfsStream::url('root');
55+
}
56+
57+
function generateLargeScss(int $size): string
58+
{
59+
$largeVariable = str_repeat('a', $size);
60+
return '$large: "' . $largeVariable . '"; body::after { content: $large; }';
61+
}
62+
63+
function mockProcessForGenerator(string $scss, string $expectedCss, ?array $expectedSourceMap = null, bool $isStreamed = false, array $chunks = []): Process
64+
{
65+
$mockProcess = Mockery::mock(Process::class);
66+
67+
if (trim($scss) !== '') {
68+
$mockProcess->shouldReceive('setInput')->once()->andReturnSelf();
69+
$mockProcess->shouldReceive('run')->once()->andReturn(0);
70+
$mockProcess->shouldReceive('getOutput')->once()->andReturn(json_encode([
71+
'css' => $expectedCss,
72+
'sourceMap' => $expectedSourceMap,
73+
'isStreamed' => $isStreamed,
74+
'chunks' => $chunks ?: [$expectedCss]
75+
]));
76+
}
77+
78+
$mockProcess->shouldReceive('getErrorOutput')->andReturn('');
79+
80+
return $mockProcess;
81+
}
82+
83+
function compileAndAssertGenerator(string $scss, string $expectedResult, array $options = [], ?array $expectedSourceMap = null, bool $isStreamed = false, array $chunks = []): void
84+
{
85+
$mockProcess = mockProcessForGenerator($scss, $expectedResult, $expectedSourceMap, $isStreamed, $chunks);
86+
87+
$compiler = mockCompiler();
88+
$compiler->shouldReceive('checkEnvironment')->andReturnNull();
89+
$compiler->shouldReceive('findNode')->andReturn('node');
90+
91+
if (trim($scss) !== '') {
92+
$compiler->shouldReceive('createProcess')->andReturn($mockProcess);
93+
}
94+
95+
$generator = $compiler->compileStringAsGenerator($scss, $options);
96+
expect($generator)->toBeInstanceOf(Generator::class);
97+
98+
$result = '';
99+
$hasSourceMap = false;
100+
foreach ($generator as $chunk) {
101+
if (str_contains($chunk, 'sourceMappingURL')) {
102+
$hasSourceMap = true;
103+
} else {
104+
$result .= $chunk;
105+
}
106+
}
107+
108+
expect($result)->toBe($expectedResult);
109+
if ($expectedSourceMap !== null) {
110+
expect($hasSourceMap)->toBeTrue();
111+
}
112+
}
113+
114+
function assertFindNodeReturnsPath(bool $isWindows): void
115+
{
116+
$mockProcess = Mockery::mock(Process::class);
117+
$mockProcess->shouldReceive('run')->once()->andReturn(0);
118+
$mockProcess->shouldReceive('isSuccessful')->once()->andReturn(true);
119+
120+
$compiler = Mockery::mock(Compiler::class)->makePartial();
121+
$compiler->shouldAllowMockingProtectedMethods();
122+
$compiler->shouldReceive('isWindows')->andReturn($isWindows);
123+
$compiler->shouldReceive('createProcess')->andReturn($mockProcess);
124+
125+
$node = (function() {
126+
return $this->findNode();
127+
})->call($compiler);
128+
129+
expect($node)->toBeString();
130+
}

0 commit comments

Comments
 (0)