Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Recorder/FilesystemRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ final class FilesystemRecorder implements RecorderInterface, PlayerInterface, Lo
*/
private $filesystem;

public function __construct(string $directory, ?Filesystem $filesystem = null)
/**
* @var array
*/
private $filters;

public function __construct(string $directory, ?Filesystem $filesystem = null, array $filters = [])
{
$this->filesystem = $filesystem ?? new Filesystem();

Expand All @@ -44,6 +49,7 @@ public function __construct(string $directory, ?Filesystem $filesystem = null)
}

$this->directory = realpath($directory).\DIRECTORY_SEPARATOR;
$this->filters = $filters;
}

public function replay(string $name): ?ResponseInterface
Expand All @@ -67,7 +73,8 @@ public function record(string $name, ResponseInterface $response): void
$filename = "{$this->directory}$name.txt";
$context = compact('name', 'filename');

$this->filesystem->dumpFile($filename, Psr7\str($response));
$content = preg_replace(array_keys($this->filters), array_values($this->filters), Psr7\str($response));
$this->filesystem->dumpFile($filename, $content);

$this->log('Response for {name} stored into {filename}', $context);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Recorder/FilesystemRecorderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,28 @@ protected function tearDown(): void
$this->filesystem->remove($this->workspace);
umask($this->umask);
}

public function testRecordWithFilter(): void
{
$original = new Response(200, ['X-Foo' => 'Bar', 'X-Bar' => 'private-token-065a1bb33f000032ab'], 'The content');

$recorder = new FilesystemRecorder($this->workspace, $this->filesystem, [
'!private-token-[0-9a-z]+!' => 'private-token-xxxx',
'!The content!' => 'The big content',
]);
$recorder->record('my_awesome_response', $original);

$this->assertFileExists(sprintf('%s%smy_awesome_response.txt', $this->workspace, \DIRECTORY_SEPARATOR));

$replayed = (new FilesystemRecorder($this->workspace))->replay('my_awesome_response');

$this->assertNotNull($replayed, 'Response should not be null');

$this->assertSame($original->getStatusCode(), $replayed->getStatusCode());
$expectedHeaders = $original->getHeaders();
$expectedHeaders['X-Bar'] = ['private-token-xxxx'];
$this->assertSame($expectedHeaders, $replayed->getHeaders());
$this->assertSame('The big content', (string) $replayed->getBody());
}

}