Skip to content
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

Add tests for NormalizingSaver #405

Merged
merged 6 commits into from
Dec 25, 2020
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
20 changes: 10 additions & 10 deletions tests/Controller/RunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testSymbol(): void
public function testCallgraph(): void
{
$this->searcher->truncate();
$this->loadFixture($this->saver);
$this->importFixture($this->saver);
Environment::mock([
'SCRIPT_NAME' => 'index.php',
'PATH_INFO' => '/',
Expand All @@ -153,7 +153,7 @@ public function testCallgraph(): void
public function testCallgraphData(): void
{
$this->searcher->truncate();
$this->loadFixture($this->saver);
$this->importFixture($this->saver);
Environment::mock([
'SCRIPT_NAME' => 'index.php',
'PATH_INFO' => '/',
Expand All @@ -171,7 +171,7 @@ public function testDeleteSubmit(): void
{
$this->skipIfPdo('Undefined index: page');
$searcher = $this->searcher->truncate();
$this->loadFixture($this->saver);
$this->importFixture($this->saver);

Environment::mock([
'REQUEST_METHOD' => 'POST',
Expand All @@ -190,19 +190,19 @@ public function testDeleteSubmit(): void
->method('redirect');

$result = $searcher->getAll(new SearchOptions());
$this->assertCount(5, $result['results']);
$this->assertCount(7, $result['results']);

$this->runs->deleteSubmit($this->app->request());

$result = $searcher->getAll(new SearchOptions());
$this->assertCount(4, $result['results']);
$this->assertCount(6, $result['results']);
}

public function testDeleteAllSubmit(): void
{
$this->skipIfPdo('Undefined index: page');
$this->searcher->truncate();
$this->loadFixture($this->saver);
$this->importFixture($this->saver);

Environment::mock([
'SCRIPT_NAME' => 'index.php',
Expand All @@ -217,7 +217,7 @@ public function testDeleteAllSubmit(): void
->method('redirect');

$result = $this->searcher->getAll(new SearchOptions());
$this->assertCount(5, $result['results']);
$this->assertCount(7, $result['results']);

$this->runs->deleteAllSubmit();

Expand All @@ -228,7 +228,7 @@ public function testDeleteAllSubmit(): void
public function testFilterCustomMethods(): void
{
$this->searcher->truncate();
$this->loadFixture($this->saver);
$this->importFixture($this->saver);

Environment::mock([
'SCRIPT_NAME' => 'index.php',
Expand All @@ -245,7 +245,7 @@ public function testFilterCustomMethods(): void
public function testFilterCustomMethod(): void
{
$this->searcher->truncate();
$this->loadFixture($this->saver);
$this->importFixture($this->saver);

Environment::mock([
'SCRIPT_NAME' => 'index.php',
Expand All @@ -262,7 +262,7 @@ public function testFilterCustomMethod(): void
public function testFilterMethods(): void
{
$this->searcher->truncate();
$this->loadFixture($this->saver);
$this->importFixture($this->saver);

Environment::mock([
'SCRIPT_NAME' => 'index.php',
Expand Down
4 changes: 2 additions & 2 deletions tests/Saver/MongoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class MongoTest extends TestCase
{
public function testSave(): void
{
$data = json_decode(file_get_contents(__DIR__ . '/../../tests/fixtures/results.json'), true);
$data = $this->loadFixture('normalized.json');

$collection = $this->getMockBuilder(MongoCollection::class)
->disableOriginalConstructor()
->getMock();
$collection->expects($this->exactly(5))
$collection->expects($this->exactly(count($data)))
->method('insert')
->withConsecutive($this->equalTo($data));

Expand Down
55 changes: 55 additions & 0 deletions tests/Saver/NormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace XHGui\Test\Saver;

use XHGui\Saver\NormalizingSaver;
use XHGui\Saver\SaverInterface;
use XHGui\Test\TestCase;

class NormalizerTest extends TestCase
{
/** @var NormalizingSaver */
private $normalizer;
/** @var SaverInterface */
private $saver;

public function setUp(): void
{
$this->saver = new class() implements SaverInterface {
private $store = [];

public function save(array $data, string $id = null): string
{
$this->store[] = $data;

return $id;
}

public function first(): array
{
return $this->store[0];
}
};

$this->normalizer = new NormalizingSaver($this->saver);
}

/**
* @dataProvider dataProvider
*/
public function testSave(array $profile, array $normalized): void
{
$this->normalizer->save($profile, $profile['_id'] ?? null);
$this->assertEquals($normalized, $this->saver->first());
}

public function dataProvider(): iterable
{
$results = $this->loadFixture('results.json');
$normalized = $this->loadFixture('normalized.json');

foreach ($results as $index => $result) {
yield $index => [$result, $normalized[$index]];
}
}
}
4 changes: 2 additions & 2 deletions tests/Searcher/MongoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function setUp(): void

$this->skipIfPdo('This is MongoDB test');
$this->di[MongoDB::class]->watches->drop();
$this->loadFixture($this->di['saver.mongodb']);
$this->importFixture($this->di['saver.mongodb']);
}

public function testCustomQuery(): void
Expand All @@ -39,7 +39,7 @@ public function testGetForUrl(): void
];
$result = $this->mongo->getForUrl('/', $options);
$this->assertEquals(1, $result['page']);
$this->assertEquals(2, $result['totalPages']);
$this->assertEquals(4, $result['totalPages']);
$this->assertEquals(1, $result['perPage']);

$this->assertCount(1, $result['results']);
Expand Down
16 changes: 12 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
/**
* Load a fixture into the database.
*/
protected function loadFixture(SaverInterface $saver, string $fileName = 'results.json'): void
protected function importFixture(SaverInterface $saver, string $fileName = 'results.json'): void
{
$file = __DIR__ . '/fixtures/' . $fileName;
$data = json_decode(file_get_contents($file), true);
foreach ($data as $record) {
foreach ($this->loadFixture($fileName) as $record) {
$saver->save($record, $record['_id'] ?? null);
}
}

protected function loadFixture(string $fileName): array
{
$file = __DIR__ . '/fixtures/' . $fileName;
$this->assertFileExists($file);
$data = json_decode(file_get_contents($file), true);
$this->assertNotEmpty($data);

return $data;
}

protected function skipIfPdo($details = null): void
{
$saveHandler = ServiceContainer::instance()['config']['save.handler'];
Expand Down
Loading