Skip to content

Commit e5def23

Browse files
authored
testing docs (#68)
1 parent c0290ef commit e5def23

File tree

7 files changed

+206
-0
lines changed

7 files changed

+206
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Testing
3+
order: 5
4+
---
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: Basics
3+
order: 99
4+
---
5+
# Understanding fake test doubles
6+
When working with a NativePHP application, you may encounter an elevated level of difficulty when writing tests for your code.
7+
This is because NativePHP relies on an Electron/Tauri application to be open at all times, listening to HTTP requests. Obviously,
8+
emulating this in a test environment can be cumbersome. You will often hit an HTTP error, and this is normal. This is where
9+
NativePHP's fake test doubles come in.
10+
11+
```php
12+
use Native\Laravel\Facades\Window;
13+
14+
#[\PHPUnit\Framework\Attributes\Test]
15+
public function example(): void
16+
{
17+
Window::fake();
18+
19+
$this->get('/whatever-action');
20+
21+
Window::assertOpened('window-name');
22+
}
23+
```
24+
25+
## Where have I seen this before?
26+
27+
If you've ever written tests for a Laravel application, you may have seen the `*::fake()` method available on
28+
all sorts of facades. Under the hood, these methods are swapping the real implementation and behavior – in NativePHP's case,
29+
an HTTP call that forces us to keep the server up and running, in turn degrading the ability to write expressive tests – with a fake one
30+
that follows the same API. This means you do not have to change any of your code to write great tests.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: Child Process
3+
order: 100
4+
---
5+
# Fake Child Processes
6+
7+
## Example test case
8+
9+
```php
10+
use Native\Laravel\Facades\ChildProcess;
11+
12+
#[\PHPUnit\Framework\Attributes\Test]
13+
public function example(): void
14+
{
15+
ChildProcess::fake();
16+
17+
$this->get('/whatever-action');
18+
19+
ChildProcess::assertGet('background-worker');
20+
ChildProcess::assertMessage(fn (string $message, string|null $alias) => $message === '{"some-payload":"for-the-worker"}' && $alias === null);
21+
}
22+
```
23+
24+
## Available assertions
25+
- `assertGet`
26+
- `assertStarted`
27+
- `assertPhp`
28+
- `assertArtisan`
29+
- `assertStop`
30+
- `assertRestart`
31+
- `assertMessage`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: Global Shortcut
3+
order: 100
4+
---
5+
# Fake Global Shortcuts
6+
7+
## Example test case
8+
9+
```php
10+
use Native\Laravel\Facades\GlobalShortcut;
11+
12+
#[\PHPUnit\Framework\Attributes\Test]
13+
public function example(): void
14+
{
15+
GlobalShortcut::fake();
16+
17+
$this->get('/whatever-action');
18+
19+
GlobalShortcut::assertKey('CmdOrCtrl+,');
20+
GlobalShortcut::assertRegisteredCount(1);
21+
GlobalShortcut::assertEvent(OpenPreferencesEvent::class);
22+
}
23+
```
24+
25+
## Available assertions
26+
- `assertKey`
27+
- `assertRegisteredCount`
28+
- `assertUnregisteredCount`
29+
- `assertEvent`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Power Monitor
3+
order: 100
4+
---
5+
# Fake Power Monitor
6+
7+
## Example test case
8+
9+
```php
10+
use Native\Laravel\Facades\PowerMonitor;
11+
12+
#[\PHPUnit\Framework\Attributes\Test]
13+
public function example(): void
14+
{
15+
PowerMonitor::fake();
16+
17+
$this->get('/whatever-action');
18+
19+
PowerMonitor::assertGetSystemIdleState('...');
20+
}
21+
```
22+
23+
## Available assertions
24+
- `assertGetSystemIdleState`
25+
- `assertGetSystemIdleStateCount`
26+
- `assertGetSystemIdleTimeCount`
27+
- `assertGetCurrentThermalStateCount`
28+
- `assertIsOnBatteryPowerCount`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Queue Worker
3+
order: 100
4+
---
5+
# Fake Queue Worker
6+
7+
## Example test case
8+
9+
```php
10+
use Native\Laravel\Facades\QueueWorker;
11+
use Native\Laravel\DTOs\QueueConfig;
12+
13+
#[\PHPUnit\Framework\Attributes\Test]
14+
public function example(): void
15+
{
16+
QueueWorker::fake();
17+
18+
$this->get('/whatever-action');
19+
20+
QueueWorker::assertUp(fn (QueueConfig $config) => $config->alias === 'custom');
21+
}
22+
```
23+
24+
## Available assertions
25+
- `assertUp`
26+
- `assertDown`
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Windows
3+
order: 100
4+
---
5+
# Fake Windows
6+
7+
## Example test case
8+
9+
```php
10+
use Native\Laravel\Facades\Window;
11+
use Illuminate\Support\Facades\Http;
12+
13+
#[\PHPUnit\Framework\Attributes\Test]
14+
public function example(): void
15+
{
16+
Http::fake();
17+
Window::fake();
18+
19+
$this->get('/whatever-action');
20+
21+
Window::assertOpened(fn (string $windowId) => Str::startsWith($windowId, ['window-name']));
22+
Window::assertClosed('window-name');
23+
Window::assertHidden('window-name');
24+
}
25+
```
26+
27+
## Available assertions
28+
- `assertOpened`
29+
- `assertClosed`
30+
- `assertHidden`
31+
32+
## Asserting against a window instance (advanced)
33+
34+
```php
35+
use Illuminate\Support\Facades\Http;
36+
use Native\Laravel\Facades\Window;
37+
use Native\Laravel\Windows\Window as WindowImplementation;
38+
use Mockery;
39+
40+
#[\PHPUnit\Framework\Attributes\Test]
41+
public function example(): void
42+
{
43+
Http::fake();
44+
Window::fake();
45+
Window::alwaysReturnWindows([
46+
$mockWindow = Mockery::mock(WindowImplementation::class)->makePartial(),
47+
]);
48+
49+
$mockWindow->shouldReceive('route')->once()->with('action')->andReturnSelf();
50+
$mockWindow->shouldReceive('transparent')->once()->andReturnSelf();
51+
$mockWindow->shouldReceive('height')->once()->with(500)->andReturnSelf();
52+
$mockWindow->shouldReceive('width')->once()->with(775)->andReturnSelf();
53+
$mockWindow->shouldReceive('minHeight')->once()->with(500)->andReturnSelf();
54+
$mockWindow->shouldReceive('minWidth')->once()->with(775)->andReturnSelf();
55+
56+
$this->get(route('action'));
57+
}
58+
```

0 commit comments

Comments
 (0)