-
Notifications
You must be signed in to change notification settings - Fork 27
Record filtering and mapping #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
timacdonald
wants to merge
20
commits into
1.x
Choose a base branch
from
filter-and-map
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
81634f5
Clean up
timacdonald cd1a1e0
Allow records to be filtered
timacdonald 34a74c7
Add pending test
timacdonald 0652c9f
Support filtering on fake ingest
timacdonald b5d7d97
wip
timacdonald 33f0a15
Merge branch '1.x' into filter-and-map
timacdonald b7d1dc0
wip
timacdonald 967cb1c
wip
timacdonald a682444
wip
timacdonald 3618da8
wip
timacdonald 11bdcff
wip
timacdonald e88852a
wip
timacdonald c32887c
wip
timacdonald a7e2fc0
wip
timacdonald a20eabd
wip
timacdonald cd6f830
wip
timacdonald bc77979
wip
timacdonald 553b685
wip
timacdonald db081c6
wip
timacdonald d3e1462
wip
timacdonald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Illuminate\Support\Collection; | ||
use PHPUnit\Framework\Assert; | ||
use RuntimeException; | ||
|
||
use function call_user_func_array; | ||
use function is_string; | ||
use function strlen; | ||
|
||
class FakeTcpStream | ||
{ | ||
private static ?Collection $instances = null; | ||
|
||
public $context; | ||
|
||
public string $value = ''; | ||
|
||
public function __construct() | ||
{ | ||
self::instances()->push($this); | ||
} | ||
|
||
public static function instances(): Collection | ||
{ | ||
return self::$instances ??= new Collection; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $arguments | ||
*/ | ||
public function __call(string $name, array $arguments): mixed | ||
{ | ||
$handler = match ($name) { | ||
'stream_open' => fn (string $path, string $mode, int $options, ?string &$openedPath): bool => true, | ||
'stream_set_option' => fn (int $option, int $arg1, int $arg2): bool => true, | ||
'stream_write' => function (string $value): int { | ||
$this->value .= $value; | ||
|
||
return strlen($value); | ||
}, | ||
'stream_read' => fn (int $length): string|false => '2:OK', | ||
'stream_eof' => fn (): bool => false, | ||
'stream_flush' => fn (): bool => true, | ||
'stream_close' => function (): void { | ||
// | ||
}, | ||
default => throw new RuntimeException("FakeTcpStream method not implemented [{$name}]"), | ||
}; | ||
|
||
return call_user_func_array($handler, $arguments); | ||
} | ||
|
||
public function assertWritten(string|callable $value): self | ||
{ | ||
if (is_string($value)) { | ||
Assert::assertSame($value, $this->value); | ||
} else { | ||
Assert::assertTrue($value($this->value)); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public static function flush(): void | ||
{ | ||
self::$instances = null; | ||
} | ||
} |
This file contains hidden or 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,134 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Laravel\Nightwatch\Facades\Nightwatch; | ||
use Laravel\Nightwatch\Payload; | ||
use Laravel\Nightwatch\Records\Query; | ||
use Laravel\Nightwatch\Records\Record; | ||
use RuntimeException; | ||
use Tests\FakeRecord; | ||
use Tests\TestCase; | ||
|
||
use function array_shift; | ||
use function collect; | ||
use function is_numeric; | ||
use function str_contains; | ||
|
||
class FilteringAndMappingTest extends TestCase | ||
{ | ||
public function test_it_can_filter_records() | ||
{ | ||
$streams = $this->fakeTcpStreams(); | ||
|
||
Nightwatch::filter(function (Record $record): bool { | ||
if ($record instanceof Query) { | ||
return ! str_contains($record->sql, 'Laravel 3'); | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
DB::select('select * from users where name = "Laravel 1"'); | ||
DB::select('select * from users where name = "Laravel 2"'); | ||
DB::select('select * from users where name = "Laravel 3"'); | ||
$this->core->digest(); | ||
|
||
$streams->first()->assertWritten(function ($value) { | ||
$this->assertStringContainsString('Laravel 1', $value); | ||
$this->assertStringContainsString('Laravel 2', $value); | ||
$this->assertStringNotContainsString('Laravel 3', $value); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
public function test_filtered_payloads_are_always_an_array(): void | ||
{ | ||
$streams = $this->fakeTcpStreams(); | ||
$filterResult = [false, true]; | ||
|
||
Nightwatch::filter(function (Record $record) use (&$filterResult): bool { | ||
return array_shift($filterResult); | ||
}); | ||
|
||
$this->core->ingest->write(new FakeRecord); | ||
$this->core->ingest->write(new FakeRecord); | ||
$this->core->digest(); | ||
|
||
$streams->first()->assertWritten('29:'.Payload::SIGNATURE.':[{"t":"fake-record"}]'); | ||
} | ||
|
||
public function test_it_filters_falsey_values() | ||
{ | ||
$streams = $this->fakeTcpStreams(); | ||
$filterResult = [null, false, '', 0, true]; | ||
|
||
Nightwatch::filter(function (Record $record) use (&$filterResult): mixed { | ||
return array_shift($filterResult); | ||
}); | ||
|
||
$this->core->ingest->write(new FakeRecord); | ||
$this->core->ingest->write(new FakeRecord); | ||
$this->core->ingest->write(new FakeRecord); | ||
$this->core->ingest->write(new FakeRecord); | ||
$this->core->ingest->write(new FakeRecord('accepted-record')); | ||
$this->core->digest(); | ||
|
||
$streams->first()->assertWritten('33:'.Payload::SIGNATURE.':[{"t":"accepted-record"}]'); | ||
} | ||
|
||
public function test_it_rejects_records_when_exceptions_occurs() | ||
{ | ||
$streams = $this->fakeTcpStreams(); | ||
Nightwatch::handleUnrecoverableExceptionsUsing(($exceptions = collect())->push(...)); | ||
|
||
Nightwatch::filter(function (Record $record): mixed { | ||
if (is_numeric($record->t) && ($record->t % 2)) { | ||
throw new RuntimeException("Whoops {$record->t}"); | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
$this->core->ingest->write(new FakeRecord('1')); // throw | ||
$this->core->ingest->write(new FakeRecord('2')); | ||
$this->core->ingest->write(new FakeRecord('3')); // throw | ||
$this->core->ingest->write(new FakeRecord('4')); | ||
$this->core->digest(); | ||
|
||
$streams->first()->assertWritten('29:'.Payload::SIGNATURE.':[{"t":"2"},{"t":"4"}]'); | ||
$this->assertCount(2, $exceptions); | ||
$this->assertSame('Whoops 1', $exceptions[0]->getMessage()); | ||
$this->assertSame('Whoops 3', $exceptions[1]->getMessage()); | ||
} | ||
|
||
public function test_it_has_already_resolved_lazy_values() | ||
{ | ||
$this->markTestIncomplete('TODO'); | ||
} | ||
|
||
public function test_it_can_modify_records() | ||
{ | ||
$streams = $this->fakeTcpStreams(); | ||
|
||
Nightwatch::filter(function (Record $record): bool { | ||
if ($record instanceof Query) { | ||
$record->sql = 'sleep 10'; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
DB::select('select * from users'); | ||
$this->core->digest(); | ||
|
||
$streams->first()->assertWritten(function ($value) { | ||
$this->assertStringContainsString('"sql":"sleep 10"', $value); | ||
$this->assertStringNotContainsString('select * from users', $value); | ||
|
||
return true; | ||
}); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing guzzle and does a real request. Setting timeouts here so that it fails quickly when there are network issues.