Skip to content

Commit b0ce054

Browse files
committed
tests: increase test coverage
1 parent 2187935 commit b0ce054

12 files changed

Lines changed: 123 additions & 39 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ coverage.xml
1313
# Playwright
1414
node_modules/
1515
/tests/Browser/screenshots
16+
17+
# MacOS
18+
.DS_Store

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"test:type-coverage": "pest --type-coverage --exactly=100",
5050
"test:typos": "peck",
5151
"test:lint": "pint --test",
52-
"test:unit": "pest --parallel --coverage --exactly=76.6",
52+
"test:unit": "pest --parallel --coverage --exactly=79.7",
5353
"test:types": "phpstan",
5454
"test:refactor": "rector --dry-run",
5555
"test": [

phpunit.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,4 @@
3131
<directory suffix=".php">./src</directory>
3232
</include>
3333
</source>
34-
<php>
35-
<env name="PEST_BROWSER_SCREENSHOT_DIR" value="./tests/Browser/screenshots"/>
36-
</php>
3734
</phpunit>

src/Playwright/Frame.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,8 @@ public function selectOption(
519519

520520
/**
521521
* Evaluates a JavaScript expression in the frame context.
522-
*
523-
* @param mixed $arg
524522
*/
525-
public function evaluate(string $pageFunction, $arg = null): mixed
523+
public function evaluate(string $pageFunction, mixed $arg = null): mixed
526524
{
527525
$params = ['expression' => $pageFunction];
528526

src/Playwright/Locator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ public function press(string $key, ?array $options = null): void
190190
/**
191191
* Select options by value in a select element matching the locator.
192192
*
193-
* @param string|array<string> $values
193+
* @param array<int, string>|string $values
194194
* @param array<string, mixed>|null $options
195195
*/
196-
public function selectOption($values, ?array $options = null): void
196+
public function selectOption(array|string $values, ?array $options = null): void
197197
{
198198
$values = is_array($values) ? $values : [$values];
199199
$params = array_merge(['values' => $values], $options ?? []);

src/Plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Pest\Browser;
66

7+
use Pest\Browser\Support\Screenshot;
78
use Pest\Contracts\Plugins\Bootable;
89
use Pest\Contracts\Plugins\Terminable;
910
use Pest\Plugins\Parallel;
@@ -20,6 +21,8 @@ public function boot(): void
2021
{
2122
if (Parallel::isWorker() === false) {
2223
ServerManager::instance()->playwright()->start();
24+
25+
Screenshot::cleanup();
2326
}
2427
}
2528

src/Support/Process.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
/**
1111
* @internal
12+
*
13+
* @codeCoverageIgnore This class is used at plugin level to manage processes.
1214
*/
1315
final class Process
1416
{

src/Support/Screenshot.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,35 @@
44

55
namespace Pest\Browser\Support;
66

7-
use SplFileObject;
7+
use Pest\TestSuite;
88

99
/**
1010
* @internal
1111
*/
1212
final class Screenshot
1313
{
14-
private const string DEFAULT_DIR = '/tmp/pest-browser-screenshots';
15-
1614
/**
17-
* Return the path to the screenshots directory.
15+
* Return the path to the screenshots' directory.
1816
*/
1917
public static function dir(): string
2018
{
21-
// @phpstan-ignore-next-line
22-
return mb_rtrim((string) $_ENV['PEST_BROWSER_SCREENSHOT_DIR'] ?? self::DEFAULT_DIR, '/');
19+
return TestSuite::getInstance()->rootPath
20+
.'/tests/Browser/screenshots';
2321
}
2422

2523
/**
2624
* Return the full path for a screenshot file.
2725
*/
2826
public static function path(string $filename): string
2927
{
30-
return self::dir().'/'.mb_ltrim($filename, '/').'.png';
28+
$filename = self::dir().'/'.mb_ltrim($filename, '/');
29+
30+
// check if there is extension, if not, add .png
31+
if (pathinfo($filename, PATHINFO_EXTENSION) === '') {
32+
$filename .= '.png';
33+
}
34+
35+
return $filename;
3136
}
3237

3338
/**
@@ -43,11 +48,10 @@ public static function save(string $binary, ?string $filename = null): void
4348
}
4449

4550
if (is_dir(self::dir()) === false) {
46-
mkdir(self::dir(), 0775, true);
51+
mkdir(self::dir(), 0755, true);
4752
}
4853

49-
$file = new SplFileObject(self::path($filename), 'wb');
50-
$file->fwrite($decodedBinary);
54+
file_put_contents(self::path($filename), $decodedBinary);
5155
}
5256

5357
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Pest\Browser\Support\Screenshot;
6+
7+
describe('screenshot', function (): void {
8+
it('deletes the screenshots directory', function (): void {
9+
$screenshotDir = Screenshot::dir();
10+
11+
expect(is_dir($screenshotDir))->toBeFalse();
12+
});
13+
14+
it('may screen a page', function (): void {
15+
$page = $this->page('/test/frame-tests');
16+
17+
$page->screenshot('screenshot.png');
18+
19+
expect(file_exists(
20+
Screenshot::path('screenshot.png')
21+
))->toBeTrue();
22+
});
23+
});

tests/Pest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,6 @@
55
use Pest\Browser\Playwright\Element;
66
use Pest\Browser\Playwright\Locator;
77
use Pest\Expectation;
8-
use Pest\TestSuite;
9-
10-
pest()
11-
->beforeEach(fn () => cleanupScreenshots())
12-
->afterEach(fn () => cleanupScreenshots());
13-
14-
function cleanupScreenshots(): void
15-
{
16-
$basePath = TestSuite::getInstance()->testPath.'/Browser/screenshots';
17-
18-
foreach (glob("$basePath/*") as $file) {
19-
if (is_file($file)) {
20-
unlink($file);
21-
}
22-
}
23-
24-
if (file_exists($basePath)) {
25-
rmdir($basePath);
26-
}
27-
}
288

299
// todo: move this to Pest core
3010
expect()->extend('toBeChecked', function (): Expectation {

0 commit comments

Comments
 (0)