-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fake test double for WindowManager::Class: (#422)
- Usage of (relatively lenient) WindowManagerContract::class in Laravel container - Implement WindowManagerFake::class with several testing assertions and helper methods - Laravel-style facade Window::fake() method - Tests
- Loading branch information
Showing
6 changed files
with
278 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Contracts; | ||
|
||
use Native\Laravel\Windows\Window; | ||
|
||
interface WindowManager | ||
{ | ||
public function open(string $id = 'main'); | ||
|
||
public function close($id = null); | ||
|
||
public function hide($id = null); | ||
|
||
public function current(): Window; | ||
|
||
/** | ||
* @return array<int, Window> | ||
*/ | ||
public function all(): array; | ||
|
||
public function get(string $id): Window; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Native\Laravel\Fakes; | ||
|
||
use Illuminate\Support\Arr; | ||
use Native\Laravel\Contracts\WindowManager as WindowManagerContract; | ||
use Native\Laravel\Windows\Window; | ||
use PHPUnit\Framework\Assert as PHPUnit; | ||
use RuntimeException; | ||
|
||
class WindowManagerFake implements WindowManagerContract | ||
{ | ||
public array $opened = []; | ||
|
||
public array $closed = []; | ||
|
||
public array $hidden = []; | ||
|
||
public array $forcedWindowReturnValues = []; | ||
|
||
/** | ||
* @param array<int, Window> $windows | ||
*/ | ||
public function alwaysReturnWindows(array $windows): self | ||
{ | ||
$this->forcedWindowReturnValues = $windows; | ||
|
||
return $this; | ||
} | ||
|
||
public function open(string $id = 'main') | ||
{ | ||
$this->opened[] = $id; | ||
} | ||
|
||
public function close($id = null) | ||
{ | ||
$this->closed[] = $id; | ||
} | ||
|
||
public function hide($id = null) | ||
{ | ||
$this->hidden[] = $id; | ||
} | ||
|
||
public function current(): Window | ||
{ | ||
$this->ensureForceReturnWindowsProvided(); | ||
|
||
return $this->forcedWindowReturnValues[array_rand($this->forcedWindowReturnValues)]; | ||
} | ||
|
||
/** | ||
* @return array<int, Window> | ||
*/ | ||
public function all(): array | ||
{ | ||
$this->ensureForceReturnWindowsProvided(); | ||
|
||
return $this->forcedWindowReturnValues; | ||
} | ||
|
||
public function get(string $id): Window | ||
{ | ||
$this->ensureForceReturnWindowsProvided(); | ||
|
||
$matchingWindows = array_filter($this->forcedWindowReturnValues, fn (Window $window) => $window->getId() === $id); | ||
|
||
PHPUnit::assertNotEmpty($matchingWindows); | ||
PHPUnit::assertCount(1, $matchingWindows); | ||
|
||
return Arr::first($matchingWindows); | ||
} | ||
|
||
public function assertOpened(string $id): void | ||
{ | ||
PHPUnit::assertContains($id, $this->opened); | ||
} | ||
|
||
public function assertClosed(?string $id): void | ||
{ | ||
PHPUnit::assertContains($id, $this->closed); | ||
} | ||
|
||
public function assertHidden(?string $id): void | ||
{ | ||
PHPUnit::assertContains($id, $this->hidden); | ||
} | ||
|
||
private function ensureForceReturnWindowsProvided(): void | ||
{ | ||
PHPUnit::assertNotEmpty($this->forcedWindowReturnValues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
use Native\Laravel\Contracts\WindowManager as WindowManagerContract; | ||
use Native\Laravel\Facades\Window; | ||
use Native\Laravel\Fakes\WindowManagerFake; | ||
use Native\Laravel\Windows\Window as WindowClass; | ||
use PHPUnit\Framework\AssertionFailedError; | ||
|
||
use function Pest\Laravel\swap; | ||
|
||
it('swaps implementations using facade', function () { | ||
Window::fake(); | ||
|
||
expect(app(WindowManagerContract::class))->toBeInstanceOf(WindowManagerFake::class); | ||
}); | ||
|
||
it('asserts that a window was opened', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
app(WindowManagerContract::class)->open('main'); | ||
app(WindowManagerContract::class)->open('secondary'); | ||
|
||
$fake->assertOpened('main'); | ||
$fake->assertOpened('secondary'); | ||
|
||
try { | ||
$fake->assertOpened('tertiary'); | ||
} catch (AssertionFailedError) { | ||
expect(true)->toBeTrue(); | ||
|
||
return; | ||
} | ||
|
||
$this->fail('Expected assertion to fail'); | ||
}); | ||
|
||
it('asserts that a window was closed', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
app(WindowManagerContract::class)->close('main'); | ||
app(WindowManagerContract::class)->close('secondary'); | ||
|
||
$fake->assertClosed('main'); | ||
$fake->assertClosed('secondary'); | ||
|
||
try { | ||
$fake->assertClosed('tertiary'); | ||
} catch (AssertionFailedError) { | ||
expect(true)->toBeTrue(); | ||
|
||
return; | ||
} | ||
|
||
$this->fail('Expected assertion to fail'); | ||
}); | ||
|
||
it('asserts that a window was hidden', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
app(WindowManagerContract::class)->hide('main'); | ||
app(WindowManagerContract::class)->hide('secondary'); | ||
|
||
$fake->assertHidden('main'); | ||
$fake->assertHidden('secondary'); | ||
|
||
try { | ||
$fake->assertHidden('tertiary'); | ||
} catch (AssertionFailedError) { | ||
expect(true)->toBeTrue(); | ||
|
||
return; | ||
} | ||
|
||
$this->fail('Expected assertion to fail'); | ||
}); | ||
|
||
it('forces the return value of current window', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
$fake->alwaysReturnWindows($windows = [ | ||
new WindowClass('testA'), | ||
new WindowClass('testB'), | ||
]); | ||
|
||
expect($windows)->toContain(app(WindowManagerContract::class)->current()); | ||
}); | ||
|
||
it('forces the return value of all windows', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
$fake->alwaysReturnWindows($windows = [ | ||
new WindowClass('testA'), | ||
new WindowClass('testB'), | ||
]); | ||
|
||
expect(app(WindowManagerContract::class)->all())->toBe($windows); | ||
}); | ||
|
||
it('forces the return value of a specific window', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
$fake->alwaysReturnWindows($windows = [ | ||
new WindowClass('testA'), | ||
new WindowClass('testB'), | ||
]); | ||
|
||
expect(app(WindowManagerContract::class)->get('testA'))->toBe($windows[0]); | ||
expect(app(WindowManagerContract::class)->get('testB'))->toBe($windows[1]); | ||
}); | ||
|
||
test('that the get method throws an exception if multiple matching window ids exist', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
$fake->alwaysReturnWindows($windows = [ | ||
new WindowClass('testA'), | ||
new WindowClass('testA'), | ||
]); | ||
|
||
app(WindowManagerContract::class)->get('testA'); | ||
})->throws(AssertionFailedError::class); | ||
|
||
test('that the get method throws an exception if no matching window id exists', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
$fake->alwaysReturnWindows($windows = [ | ||
new WindowClass('testA'), | ||
]); | ||
|
||
app(WindowManagerContract::class)->get('testB'); | ||
})->throws(AssertionFailedError::class); | ||
|
||
test('that the current method throws an exception if no forced window return values are provided', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
app(WindowManagerContract::class)->current(); | ||
})->throws(AssertionFailedError::class); | ||
|
||
test('that the all method throws an exception if no forced window return values are provided', function () { | ||
swap(WindowManagerContract::class, $fake = new WindowManagerFake); | ||
|
||
app(WindowManagerContract::class)->all(); | ||
})->throws(AssertionFailedError::class); |