Skip to content

Commit 83a5658

Browse files
committed
chore: add TestCase for integration and DB testing
1 parent ec9ac35 commit 83a5658

File tree

6 files changed

+80
-13
lines changed

6 files changed

+80
-13
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"hyperf/utils": "~3.1.0",
2222
"phpunit/phpunit": "^10.0",
2323
"psr/container": "^1.0 || ^2.0",
24-
"symfony/http-foundation": "^5.4 || ^6.0"
24+
"symfony/http-foundation": "^5.4 || ^6.0",
25+
"hyperf/database-sqlite": "^3.1",
26+
"hyperf/database": "^3.1"
2527
},
2628
"suggest": {
2729
"fakerphp/faker": "Required to use Faker feature.(^1.23)"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyperf\Testing\Concerns;
6+
7+
use Hyperf\Context\ApplicationContext;
8+
use Hyperf\Database\Commands\Migrations\FreshCommand;
9+
use Hyperf\Database\Commands\Migrations\RollbackCommand;
10+
use Symfony\Component\Console\Input\ArrayInput;
11+
use Symfony\Component\Console\Output\BufferedOutput;
12+
13+
trait InteractsWithConsole
14+
{
15+
public function fresh(): string
16+
{
17+
$input = new ArrayInput([]);
18+
$output = new BufferedOutput();
19+
20+
ApplicationContext::getContainer()->get(FreshCommand::class)->run($input, $output);
21+
22+
return $output->fetch();
23+
}
24+
25+
public function rollback(): string
26+
{
27+
$input = new ArrayInput([]);
28+
$output = new BufferedOutput();
29+
30+
ApplicationContext::getContainer()->get(RollbackCommand::class)->run($input, $output);
31+
32+
return $output->fetch();
33+
}
34+
}

src/Concerns/InteractsWithDatabase.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@
2020

2121
trait InteractsWithDatabase
2222
{
23-
private function assertDatabaseHas(string $table, array $data, ?string $connection = null): void
23+
24+
protected function assertDatabaseCount(string $table, int $count, ?string $connection = null): void
25+
{
26+
$this->assertThat([], new HasInDatabase($this->getConnection($connection), $table, $count), $table);
27+
}
28+
29+
protected function assertDatabaseHas(string $table, array $data, ?string $connection = null): void
2430
{
2531
$this->assertThat($data, new HasInDatabase($this->getConnection($connection), $table));
2632
}
2733

28-
private function assertDatabaseMissing(string $table, array $data, ?string $connection = null): void
34+
protected function assertDatabaseMissing(string $table, array $data, ?string $connection = null): void
2935
{
3036
$this->assertThat($data, new LogicalNot(new HasInDatabase($this->getConnection($connection), $table)));
3137
}

src/Constraint/HasInDatabase.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@
1818

1919
class HasInDatabase extends Constraint
2020
{
21-
protected ConnectionInterface $connection;
22-
23-
protected string $table;
24-
25-
public function __construct(ConnectionInterface $connection, string $table)
26-
{
27-
$this->table = $table;
28-
$this->connection = $connection;
21+
public function __construct(
22+
protected ConnectionInterface $connection,
23+
protected string $table,
24+
protected int $count = 0
25+
) {
2926
}
3027

3128
public function matches($data): bool
3229
{
33-
return $this->query($data)->count() > 0;
30+
return $this->query($data)->count() > $this->count;
3431
}
3532

3633
public function failureDescription($data): string
@@ -49,7 +46,6 @@ public function toString(): string
4946

5047
private function query(array $data): Builder
5148
{
52-
/** @var Builder $query */
5349
$query = $this->connection->table($this->table);
5450

5551
foreach ($data as $index => $value) {

src/DatabaseMigrations.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyperf\Testing;
6+
7+
use Hyperf\Context\ApplicationContext;
8+
use Hyperf\Contract\ConfigInterface;
9+
10+
trait DatabaseMigrations
11+
{
12+
protected array $database = [
13+
'driver' => 'sqlite',
14+
'database' => ':memory:',
15+
];
16+
17+
public function setUpDatabaseMigrations(): void
18+
{
19+
ApplicationContext::getContainer()->get(ConfigInterface::class)->set('databases.default', $this->database);
20+
21+
$this->fresh();
22+
}
23+
24+
public function tearDownDatabaseMigrations(): void
25+
{
26+
$this->rollback();
27+
}
28+
}

src/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
abstract class TestCase extends \PHPUnit\Framework\TestCase
2626
{
27+
use Concerns\InteractsWithConsole;
2728
use Concerns\InteractsWithContainer;
2829
use Concerns\InteractsWithModelFactory;
2930
use Concerns\MakesHttpRequests;

0 commit comments

Comments
 (0)