File tree Expand file tree Collapse file tree 7 files changed +206
-0
lines changed
resources/views/docs/1/testing Expand file tree Collapse file tree 7 files changed +206
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ title : Testing
3
+ order : 5
4
+ ---
Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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 `
Original file line number Diff line number Diff line change
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 `
Original file line number Diff line number Diff line change
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 `
Original file line number Diff line number Diff line change
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 `
Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments