Skip to content
Merged
22 changes: 20 additions & 2 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,31 @@
]
},
"timeout": 10,
"minMsi": 54,
"minCoveredMsi": 54,
"minMsi": 90,
"minCoveredMsi": 90,
"testFramework": "testo",
"tmpDir": "runtime",
"logs": {
"stryker": {
"report": "1.x"
}
},
"mutators": {
"@default": true,
"IncrementInteger": {
"ignoreSourceCodeByRegex": [
".*limit: 1.*"
]
},
"LogicalAnd": {
"ignoreSourceCodeByRegex": [
".*\\$a::class === \\$b::class && \\$a == \\$b.*"
]
},
"UnwrapArrayValues": {
"ignore": [
"Cycle\\Testing\\Database\\Internal\\DatabaseInterceptor::connections"
]
}
}
}
1 change: 1 addition & 0 deletions tests/Feature/Common/DatabaseContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function tableBuilderMatchesDatabase(): void
$schema = $this->schema('child');
Assert::true($schema->exists());
Assert::true($schema->hasForeignKey(['parent_id']));
Assert::true($schema->hasIndex(['note']));
SchemaAssert::sameAsInDb($schema);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/Common/PurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function dropsTablesLinkedByForeignKeysBothWays(): void
$this->makeFK('author', 'book_id', 'book', 'id', ForeignKeyInterface::NO_ACTION, ForeignKeyInterface::NO_ACTION);
$this->database->table('author')->insertOne(['book_id' => null]);
$this->database->table('book')->insertOne(['author_id' => 1]);
Assert::true($this->schema('book')->hasForeignKey(['author_id']));
Assert::true($this->schema('author')->hasForeignKey(['book_id']));

Purger::dropTables($this->database->getDriver());

Expand Down
32 changes: 32 additions & 0 deletions tests/Feature/Common/QueryCountersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace Cycle\Testing\Tests\Feature\Common;

use Cycle\Testing\Database\Assert\QueryAssert;
use Cycle\Testing\Database\DatabaseTestCase;
use Cycle\Testing\Database\QueryLog;
use Testo\Assert\State\Assertion\AssertionException;
use Testo\Expect;
use Testo\Lifecycle\BeforeTest;
use Testo\Test;

Expand Down Expand Up @@ -42,4 +46,32 @@ public function withoutCaptureCountsFromTheTestStart(): void

$this->assertNumWrites(1);
}

public function queryAssertCountsOnTheSuiteLog(): void
{
QueryAssert::writes(1, fn() => $this->database->table('counted')->insertOne(['name' => 'e']));
}

public function queryAssertPrefersTheGivenLog(): void
{
$log = new QueryLog();

QueryAssert::count(0, 1, fn() => $log->info('DELETE FROM t', ['elapsed' => 0.1]), $log);
}

public function readCountMismatchFails(): never
{
$this->captureReadQueries();

Expect::exception(AssertionException::class)->withMessageContaining('Number of read SQL queries do not match, expected 1 got 0.');

$this->assertNumReads(1);
}

public function wrapDriverNeedsTheAttribute(): never
{
Expect::exception(\LogicException::class)->withMessageContaining('WrapDriver');

$this->wrapDriver();
}
}
11 changes: 11 additions & 0 deletions tests/Feature/Common/WrapDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ public function countsDisconnects(): void
$driver->disconnect();

Assert::same($driver->disconnectCalls, 1);
Assert::false($driver->isConnected());
}

public function oneFailedBeginIsRetried(): void
{
$this->wrapDriver()->exceptionOnTransactionBegin = 1;

$this->database->begin();

Assert::same($this->database->getDriver()->getTransactionLevel(), 1);
$this->database->rollback();
}

public function knobsAreResetBetweenTests(): void
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/FailingMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Cycle\Testing\Tests\Fixtures;

use Cycle\ORM\Mapper\Mapper;

final class FailingMapper extends Mapper
{
public function extract(object $entity): array
{
throw new \Error('mapper failed');
}
}
17 changes: 17 additions & 0 deletions tests/Fixtures/HarnessSuite/Functions/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\Testing\Tests\Fixtures\HarnessSuite\Functions;

use Testo\Assert;
use Testo\Test;

/**
* A function test has no case class: no reflection and no instance.
*/
#[Test]
function runsWithoutACaseClass(): void
{
Assert::true(true);
}
32 changes: 32 additions & 0 deletions tests/Fixtures/HarnessSuite/Isolation/AttachCountingCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Cycle\Testing\Tests\Fixtures\HarnessSuite\Isolation;

use Cycle\Testing\Database\DatabaseAware;
use Cycle\Testing\Database\DatabaseContext;
use Testo\Assert;
use Testo\Test;

final class AttachCountingCase implements DatabaseAware
{
private int $attached = 0;

public function attachDatabase(DatabaseContext $context): void
{
++$this->attached;
}

#[Test]
public function first(): void
{
Assert::same($this->attached, 1);
}

#[Test]
public function second(): void
{
Assert::same($this->attached, 1);
}
}
29 changes: 29 additions & 0 deletions tests/Fixtures/HarnessSuite/Isolation/DropTablesCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Cycle\Testing\Tests\Fixtures\HarnessSuite\Isolation;

use Cycle\Testing\Database\DatabaseTestCase;
use Testo\Assert;
use Testo\Test;

/**
* The tests run in declaration order: the second checks what the first left behind, a transaction included.
*/
#[Test]
final class DropTablesCase extends DatabaseTestCase
{
public function leavesTablesBehind(): void
{
$this->makeTable('left_behind', ['id' => 'primary']);
$this->database->begin();

Assert::true($this->database->hasTable('left_behind'));
}

public function startsWithoutThem(): void
{
Assert::false($this->database->hasTable('left_behind'));
}
}
33 changes: 33 additions & 0 deletions tests/Fixtures/HarnessSuite/Isolation/NoIsolationCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Cycle\Testing\Tests\Fixtures\HarnessSuite\Isolation;

use Cycle\Testing\Database\Attribute\Isolate;
use Cycle\Testing\Database\DatabaseTestCase;
use Cycle\Testing\Database\Isolation;
use Testo\Assert;
use Testo\Test;

/**
* The tests run in declaration order: each later one checks what the earlier left behind.
*/
#[Test]
#[Isolate(Isolation::None)]
final class NoIsolationCase extends DatabaseTestCase
{
public function leavesATransactionOpen(): void
{
$this->makeTable('kept', ['id' => 'primary']);
$this->database->begin();

Assert::same($this->database->getDriver()->getTransactionLevel(), 1);
}

public function startsOutsideATransactionWithTheTableKept(): void
{
Assert::same($this->database->getDriver()->getTransactionLevel(), 0);
Assert::true($this->database->hasTable('kept'));
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/HarnessSuite/Isolation/PlainCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Cycle\Testing\Tests\Fixtures\HarnessSuite\Isolation;

use Testo\Assert;
use Testo\Test;

/**
* Not database-aware: the harness leaves the instance alone.
*/
#[Test]
final class PlainCase
{
public function runs(): void
{
Assert::true(true);
}
}
38 changes: 38 additions & 0 deletions tests/Fixtures/HarnessSuite/Memory/ReconnectCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Cycle\Testing\Tests\Fixtures\HarnessSuite\Memory;

use Cycle\Database\Database;
use Cycle\Testing\Database\DatabaseTestCase;
use Testo\Assert;
use Testo\Test;

/**
* An in-memory database is reconnected after each test, which drops even the temporary tables a purge
* would keep. The tests run in declaration order.
*/
#[Test]
final class ReconnectCase extends DatabaseTestCase
{
public function leavesATemporaryTable(): void
{
$this->database->execute('CREATE TEMP TABLE temp_left_behind (id INTEGER)');

Assert::same(self::temporaryTables($this->database), ['temp_left_behind']);
}

public function startsWithoutIt(): void
{
Assert::same(self::temporaryTables($this->database), []);
}

/**
* @return list<string>
*/
private static function temporaryTables(Database $database): array
{
return \array_column($database->query('SELECT name FROM sqlite_temp_master')->fetchAll(), 'name');
}
}
32 changes: 32 additions & 0 deletions tests/Fixtures/HarnessSuite/Orm/SkippedHeapCheckCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Cycle\Testing\Tests\Fixtures\HarnessSuite\Orm;

use Cycle\Testing\ORM\Attribute\SkipHeapCheck;
use Cycle\Testing\ORM\OrmTestCase;
use Cycle\Testing\Tests\Fixtures\User;
use Cycle\Testing\Tests\Fixtures\UserSchema;
use Testo\Assert;
use Testo\Test;

/**
* Ends with a heap out of sync, but skips the heap check for the whole class.
*/
#[Test]
#[SkipHeapCheck]
final class SkippedHeapCheckCase extends OrmTestCase
{
public function changesASavedEntity(): void
{
$this->makeTable('user', UserSchema::COLUMNS);
$this->withSchema(UserSchema::schema());
$user = new User();
$this->save($user);

$user->email = 'changed@example.com';

Assert::notNull($user->id);
}
}
30 changes: 30 additions & 0 deletions tests/Fixtures/HarnessSuite/Orm/UnsyncedHeapCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Cycle\Testing\Tests\Fixtures\HarnessSuite\Orm;

use Cycle\Testing\ORM\OrmTestCase;
use Cycle\Testing\Tests\Fixtures\User;
use Cycle\Testing\Tests\Fixtures\UserSchema;
use Testo\Assert;
use Testo\Test;

/**
* Ends with a heap out of sync, which only a heap check reports.
*/
#[Test]
final class UnsyncedHeapCase extends OrmTestCase
{
public function changesASavedEntity(): void
{
$this->makeTable('user', UserSchema::COLUMNS);
$this->withSchema(UserSchema::schema());
$user = new User();
$this->save($user);

$user->email = 'changed@example.com';

Assert::notNull($user->id);
}
}
Loading
Loading