2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
namespace Test\Integration\SQLite;
|
|
|
|
|
2024-06-21 13:46:41 +00:00
|
|
|
use BitBadger\PDODocument\{Count, DocumentException, Field, Find, Patch};
|
2024-06-08 23:58:45 +00:00
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Test\Integration\TestDocument;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQLite integration tests for the Patch class
|
|
|
|
*/
|
|
|
|
#[TestDox('Patch (SQLite integration)')]
|
|
|
|
class PatchTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var string Database name for throwaway database */
|
|
|
|
private string $dbName;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->dbName = ThrowawayDb::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
ThrowawayDb::destroy($this->dbName);
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byId() succeeds when a document is updated')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByIdSucceedsWhenADocumentIsUpdated(): void
|
|
|
|
{
|
|
|
|
Patch::byId(ThrowawayDb::TABLE, 'one', ['num_value' => 44]);
|
|
|
|
$doc = Find::byId(ThrowawayDb::TABLE, 'one', TestDocument::class);
|
2024-07-28 23:45:29 +00:00
|
|
|
$this->assertTrue($doc->isSome(), 'There should have been a document returned');
|
2024-06-25 02:04:11 +00:00
|
|
|
$this->assertEquals(44, $doc->get()->num_value, 'The updated document is not correct');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byId() succeeds when no document is updated')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByIdSucceedsWhenNoDocumentIsUpdated(): void
|
|
|
|
{
|
|
|
|
Patch::byId(ThrowawayDb::TABLE, 'forty-seven', ['foo' => 'green']);
|
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byFields() succeeds when a document is updated')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByFieldsSucceedsWhenADocumentIsUpdated(): void
|
|
|
|
{
|
2024-09-21 00:29:47 +00:00
|
|
|
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'purple')], ['num_value' => 77]);
|
|
|
|
$after = Count::byFields(ThrowawayDb::TABLE, [Field::equal('num_value', 77)]);
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertEquals(2, $after, 'There should have been 2 documents updated');
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byFields() succeeds when no document is updated')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testByFieldsSucceedsWhenNoDocumentIsUpdated(): void
|
|
|
|
{
|
2024-09-21 00:29:47 +00:00
|
|
|
Patch::byFields(ThrowawayDb::TABLE, [Field::equal('value', 'burgundy')], ['foo' => 'green']);
|
2024-06-08 23:58:45 +00:00
|
|
|
$this->assertTrue(true, 'The above not throwing an exception is the test');
|
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byContains() fails')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByContainsFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Patch::byContains('', [], []);
|
|
|
|
}
|
|
|
|
|
2024-09-23 02:13:51 +00:00
|
|
|
#[TestDox('byJsonPath() fails')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testByJsonPathFails(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Patch::byJsonPath('', '', []);
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|