Migrate tests to Pest #8
18
tests/Unit/Mapper/ArrayMapperTest.php
Normal file
18
tests/Unit/Mapper/ArrayMapperTest.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\Mapper\ArrayMapper;
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
describe('->map()', function () {
|
||||
test('returns the given array', function () {
|
||||
$result = ['one' => 2, 'three' => 4, 'eight' => 'five'];
|
||||
expect((new ArrayMapper())->map($result))->toBe($result);
|
||||
});
|
||||
});
|
17
tests/Unit/Mapper/CountMapperTest.php
Normal file
17
tests/Unit/Mapper/CountMapperTest.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\Mapper\CountMapper;
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
describe('->map()', function () {
|
||||
test('returns item 0 in the given array', function () {
|
||||
expect((new CountMapper())->map([5, 8, 10]))->toBe(5);
|
||||
});
|
||||
});
|
65
tests/Unit/Mapper/DocumentMapperTest.php
Normal file
65
tests/Unit/Mapper/DocumentMapperTest.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{DocumentException, Field};
|
||||
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
||||
use Test\{PjsonDocument, PjsonId};
|
||||
|
||||
// ** Test class hierarchy for serialization **
|
||||
|
||||
class DocMapSubDoc
|
||||
{
|
||||
public function __construct(public int $id = 0, public string $name = '') { }
|
||||
}
|
||||
|
||||
class DocMapTestDoc
|
||||
{
|
||||
public function __construct(public int $id = 0, public DocMapSubDoc $subDoc = new DocMapSubDoc()) { }
|
||||
}
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
describe('Constructor', function () {
|
||||
test('uses "data" as the default field name', function () {
|
||||
expect(new DocumentMapper(Field::class))->fieldName->toBe('data');
|
||||
});
|
||||
test('uses the provided field name', function () {
|
||||
expect(new DocumentMapper(Field::class, 'json'))->fieldName->toBe('json');
|
||||
});
|
||||
});
|
||||
|
||||
describe('->map()', function () {
|
||||
test('deserializes valid JSON', function () {
|
||||
$doc = (new DocumentMapper(DocMapTestDoc::class))
|
||||
->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']);
|
||||
expect($doc)
|
||||
->not->toBeNull()
|
||||
->id->toBe(7)
|
||||
->and($doc->subDoc)
|
||||
->not->toBeNull()
|
||||
->id->toBe(22)
|
||||
->name->toBe('tester');
|
||||
});
|
||||
test('deserializes valid JSON [Pjson]', function () {
|
||||
$doc = (new DocumentMapper(PjsonDocument::class))->map(['data' => '{"id":"seven","name":"bob","num_value":8}']);
|
||||
expect($doc)
|
||||
->not->toBeNull()
|
||||
->id->toEqual(new PjsonId('seven'))
|
||||
->name->toBe('bob')
|
||||
->numValue->toBe(8)
|
||||
->skipped->toBeEmpty();
|
||||
});
|
||||
test('throws for invalid JSON', function () {
|
||||
expect(fn() => (new DocumentMapper(DocMapTestDoc::class))->map(['data' => 'this is not valid']))
|
||||
->toThrow(DocumentException::class);
|
||||
});
|
||||
test('throws for invalid JSON [Pjson]', function () {
|
||||
expect(fn() => (new DocumentMapper(PjsonDocument::class))->map(['data' => 'not even close']))
|
||||
->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
28
tests/Unit/Mapper/ExistsMapperTest.php
Normal file
28
tests/Unit/Mapper/ExistsMapperTest.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
|
||||
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
afterEach(function () { Configuration::overrideMode(null); });
|
||||
|
||||
describe('->map()', function () {
|
||||
test('returns a boolean value from index 0 [PostgreSQL]', function () {
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
expect((new ExistsMapper())->map([false, 'nope']))->toBeFalse();
|
||||
});
|
||||
test('returns a number value as boolean from index 0 [SQLite]', function () {
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
expect((new ExistsMapper())->map([1, 'yep']))->toBeTrue();
|
||||
});
|
||||
test('throws if mode is not set', function () {
|
||||
expect(fn() => (new ExistsMapper())->map(['0']))->toThrow(DocumentException::class);
|
||||
});
|
||||
});
|
23
tests/Unit/Mapper/StringMapperTest.php
Normal file
23
tests/Unit/Mapper/StringMapperTest.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use BitBadger\PDODocument\Mapper\StringMapper;
|
||||
|
||||
pest()->group('unit');
|
||||
|
||||
describe('->map()', function () {
|
||||
test('returns existing string column value', function () {
|
||||
expect(new StringMapper('test_field'))->map(['test_field' => 'test_value'])->toBe('test_value');
|
||||
});
|
||||
test('returns string value of non-string column', function () {
|
||||
expect(new StringMapper('a_number'))->map(['a_number' => 6.7])->toBe('6.7');
|
||||
});
|
||||
test('returns null for a missing column', function () {
|
||||
expect(new StringMapper('something_else'))->map([])->toBeNull();
|
||||
});
|
||||
});
|
|
@ -1,28 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Mapper;
|
||||
|
||||
use BitBadger\PDODocument\Mapper\ArrayMapper;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the ArrayMapper class
|
||||
*/
|
||||
#[TestDox('Array Mapper (Unit tests)')]
|
||||
class ArrayMapperTest extends TestCase
|
||||
{
|
||||
#[TestDox('map() succeeds')]
|
||||
public function testMapSucceeds(): void
|
||||
{
|
||||
$result = ['one' => 2, 'three' => 4, 'eight' => 'five'];
|
||||
$mapped = (new ArrayMapper())->map($result);
|
||||
$this->assertSame($result, $mapped, 'The array mapper should return the parameter given to it');
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Mapper;
|
||||
|
||||
use BitBadger\PDODocument\Mapper\CountMapper;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the CountMapper class
|
||||
*/
|
||||
#[TestDox('Count Mapper (Unit tests)')]
|
||||
class CountMapperTest extends TestCase
|
||||
{
|
||||
#[TestDox('map() succeeds')]
|
||||
public function testMapSucceeds(): void
|
||||
{
|
||||
$this->assertEquals(5, (new CountMapper())->map([5, 8, 10]), 'Count not correct');
|
||||
}
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Mapper;
|
||||
|
||||
use BitBadger\PDODocument\{DocumentException, Field};
|
||||
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Test\{PjsonDocument, PjsonId};
|
||||
|
||||
// ** Test class hierarchy for serialization **
|
||||
|
||||
class SubDocument
|
||||
{
|
||||
public function __construct(public int $id = 0, public string $name = '') { }
|
||||
}
|
||||
|
||||
class TestDocument
|
||||
{
|
||||
public function __construct(public int $id = 0, public SubDocument $subDoc = new SubDocument()) { }
|
||||
}
|
||||
|
||||
/**
|
||||
* Unit tests for the DocumentMapper class
|
||||
*/
|
||||
#[TestDox('Document Mapper (Unit tests)')]
|
||||
class DocumentMapperTest extends TestCase
|
||||
{
|
||||
public function testConstructorSucceedsWithDefaultField(): void
|
||||
{
|
||||
$mapper = new DocumentMapper(Field::class);
|
||||
$this->assertEquals('data', $mapper->fieldName, 'Default field name should have been "data"');
|
||||
}
|
||||
|
||||
public function testConstructorSucceedsWithSpecifiedField(): void
|
||||
{
|
||||
$mapper = new DocumentMapper(Field::class, 'json');
|
||||
$this->assertEquals('json', $mapper->fieldName, 'Field name not recorded correctly');
|
||||
}
|
||||
|
||||
#[TestDox('map() succeeds with valid JSON')]
|
||||
public function testMapSucceedsWithValidJSON(): void
|
||||
{
|
||||
$doc = (new DocumentMapper(TestDocument::class))->map(['data' => '{"id":7,"subDoc":{"id":22,"name":"tester"}}']);
|
||||
$this->assertNotNull($doc, 'The document should not have been null');
|
||||
$this->assertEquals(7, $doc->id, 'ID not filled correctly');
|
||||
$this->assertNotNull($doc->subDoc, 'The sub-document should not have been null');
|
||||
$this->assertEquals(22, $doc->subDoc->id, 'Sub-document ID not filled correctly');
|
||||
$this->assertEquals('tester', $doc->subDoc->name, 'Sub-document name not filled correctly');
|
||||
}
|
||||
|
||||
#[TestDox('map() succeeds with valid JSON for Pjson class')]
|
||||
public function testMapSucceedsWithValidJSONForPjsonClass(): void
|
||||
{
|
||||
$doc = (new DocumentMapper(PjsonDocument::class))->map(['data' => '{"id":"seven","name":"bob","num_value":8}']);
|
||||
$this->assertNotNull($doc, 'The document should not have been null');
|
||||
$this->assertEquals(new PjsonId('seven'), $doc->id, 'ID not filled correctly');
|
||||
$this->assertEquals('bob', $doc->name, 'Name not filled correctly');
|
||||
$this->assertEquals(8, $doc->numValue, 'Numeric value not filled correctly');
|
||||
$this->assertFalse(isset($doc->skipped), 'Non-JSON field has not been set');
|
||||
}
|
||||
|
||||
#[TestDox('map() fails with invalid JSON')]
|
||||
public function testMapFailsWithInvalidJSON(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
(new DocumentMapper(TestDocument::class))->map(['data' => 'this is not valid']);
|
||||
}
|
||||
|
||||
#[TestDox('map() fails with invalid JSON for Pjson class')]
|
||||
public function testMapFailsWithInvalidJSONForPjsonClass(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
(new DocumentMapper(PjsonDocument::class))->map(['data' => 'not even close']);
|
||||
}
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Mapper;
|
||||
|
||||
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
|
||||
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the ExistsMapper class
|
||||
*/
|
||||
#[TestDox('Exists Mapper (Unit tests)')]
|
||||
class ExistsMapperTest extends TestCase
|
||||
{
|
||||
#[TestDox('map() succeeds for PostgreSQL')]
|
||||
public function testMapSucceedsForPostgreSQL(): void
|
||||
{
|
||||
try {
|
||||
Configuration::overrideMode(Mode::PgSQL);
|
||||
$this->assertFalse((new ExistsMapper())->map([false, 'nope']), 'Result should have been false');
|
||||
} finally {
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('map() succeeds for SQLite')]
|
||||
public function testMapSucceedsForSQLite(): void
|
||||
{
|
||||
try {
|
||||
Configuration::overrideMode(Mode::SQLite);
|
||||
$this->assertTrue((new ExistsMapper())->map([1, 'yep']), 'Result should have been true');
|
||||
} finally {
|
||||
Configuration::overrideMode(null);
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('map() fails when mode not set')]
|
||||
public function testMapFailsWhenModeNotSet(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
Configuration::overrideMode(null);
|
||||
(new ExistsMapper())->map(['0']);
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Mapper;
|
||||
|
||||
use BitBadger\PDODocument\Mapper\StringMapper;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the StringMapper class
|
||||
*/
|
||||
#[TestDox('String Mapper (Unit tests)')]
|
||||
class StringMapperTest extends TestCase
|
||||
{
|
||||
#[TestDox('map() succeeds when field is present and string')]
|
||||
public function testMapSucceedsWhenFieldIsPresentAndString(): void
|
||||
{
|
||||
$result = ['test_field' => 'test_value'];
|
||||
$mapper = new StringMapper('test_field');
|
||||
$this->assertEquals('test_value', $mapper->map($result), 'String value not returned correctly');
|
||||
}
|
||||
|
||||
#[TestDox('map() succeeds when field is present and not string')]
|
||||
public function testMapSucceedsWhenFieldIsPresentAndNotString(): void
|
||||
{
|
||||
$result = ['a_number' => 6.7];
|
||||
$mapper = new StringMapper('a_number');
|
||||
$this->assertEquals('6.7', $mapper->map($result), 'Number value not returned correctly');
|
||||
}
|
||||
|
||||
#[TestDox('map() succeeds when field is not present')]
|
||||
public function testMapSucceedsWhenFieldIsNotPresent(): void
|
||||
{
|
||||
$mapper = new StringMapper('something_else');
|
||||
$this->assertNull($mapper->map([]), 'Missing value not returned correctly');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user