Skip to content

Commit

Permalink
getInsertId() in Database mock can use a queue of ids to return in ca…
Browse files Browse the repository at this point in the history
…se there's multiple getInsertId() calls in a test
  • Loading branch information
spaze committed Nov 27, 2024
1 parent 2771fa8 commit 17305f6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
22 changes: 18 additions & 4 deletions app/src/Test/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class Database extends Explorer
use WillThrow;


private string $insertId = '';
private string $defaultInsertId = '';

/** @var list<string> */
private array $insertIds = [];

private int $insertIdsPosition = 0;

/** @var array<string, array<string|int, string|int|bool|null>> */
private array $queriesScalarParams = [];
Expand Down Expand Up @@ -54,6 +59,9 @@ class Database extends Explorer

public function reset(): void
{
$this->defaultInsertId = '';
$this->insertIds = [];
$this->insertIdsPosition = 0;
$this->queriesScalarParams = [];
$this->queriesArrayParams = [];
$this->fetchResult = [];
Expand Down Expand Up @@ -88,16 +96,22 @@ public function rollBack(): void
}


public function setInsertId(string $insertId): void
public function setDefaultInsertId(string $insertId): void
{
$this->defaultInsertId = $insertId;
}


public function addInsertId(string $insertId): void
{
$this->insertId = $insertId;
$this->insertIds[] = $insertId;
}


#[Override]
public function getInsertId(?string $sequence = null): string
{
return $this->insertId;
return $this->insertIds[$this->insertIdsPosition++] ?? $this->defaultInsertId;
}


Expand Down
2 changes: 1 addition & 1 deletion app/tests/Articles/Blog/BlogPostTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class BlogPostTest extends TestCase
{
$post = $this->buildBlogPost();
$insertId = 1337;
$this->database->setInsertId((string)$insertId);
$this->database->setDefaultInsertId((string)$insertId);
$insertedPost = $this->blogPosts->add($post);
Assert::null($post->getId());
Assert::same($insertId, $insertedPost->getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class PasswordHashingDisclosuresTest extends TestCase

public function testAddDisclosure(): void
{
$this->database->setInsertId('123');
$this->database->setDefaultInsertId('123');
$lastInsertId = $this->disclosures->addDisclosure(1, 'https://example.com/', 'https://archive.example.com/', 'note', 'now');
Assert::same(123, $lastInsertId);
}
Expand Down
2 changes: 1 addition & 1 deletion app/tests/Tls/CertificatesTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CertificatesTest extends TestCase

public function testLog(): void
{
$this->database->setInsertId('42');
$this->database->setDefaultInsertId('42');
$certificates = [
new Certificate('foo.example', null, $this->notBefore, $this->notAfter, 0, null),
new Certificate('bar.example', null, $this->notBefore, $this->notAfter, 0, null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TrainingApplicationStorageTest extends TestCase
]);
$this->database->addFetchFieldResult(self::STATUS_CREATED); // For ApplicationStatuses::getStatusId() in TrainingApplicationStorage::insertApplication()
$this->database->addFetchFieldResult(self::SOURCE_ID); // For TrainingApplicationSources::getSourceId in TrainingApplicationStorage::insertApplication()
$this->database->setInsertId((string)self::INSERT_ID);
$this->database->setDefaultInsertId((string)self::INSERT_ID);
$this->database->setFetchResult([ // For ApplicationStatuses::setStatus() in ApplicationStatuses::updateStatusCallbackReturnId()
'statusId' => self::STATUS_CREATED,
'statusTime' => new DateTime(),
Expand Down

0 comments on commit 17305f6

Please sign in to comment.