Skip to content

Commit 00be472

Browse files
Added: Test Interacts With Database (#6386)
1 parent a0b4f54 commit 00be472

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Testing\Concerns;
13+
14+
use Hyperf\Context\ApplicationContext;
15+
use Hyperf\Database\ConnectionInterface;
16+
use Hyperf\Database\ConnectionResolverInterface;
17+
use Hyperf\Testing\Constraint\HasInDatabase;
18+
use PHPUnit\Framework\Constraint\LogicalNot;
19+
20+
trait InteractsWithDatabase
21+
{
22+
private function assertDatabaseHas(string $table, array $data, ?string $connection = null): void
23+
{
24+
$this->assertThat($data, new HasInDatabase($this->getConnection($connection), $table));
25+
}
26+
27+
private function assertDatabaseMissing(string $table, array $data, ?string $connection = null): void
28+
{
29+
$this->assertThat($data, new LogicalNot(new HasInDatabase($this->getConnection($connection), $table)));
30+
}
31+
32+
private function getConnection(?string $name): ConnectionInterface
33+
{
34+
return ApplicationContext::getContainer()->get(ConnectionResolverInterface::class)->connection($name);
35+
}
36+
}

src/Constraint/HasInDatabase.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Testing\Constraint;
13+
14+
use Hyperf\Database\ConnectionInterface;
15+
use Hyperf\Database\Query\Builder;
16+
use PHPUnit\Framework\Constraint\Constraint;
17+
18+
class HasInDatabase extends Constraint
19+
{
20+
protected ConnectionInterface $connection;
21+
22+
protected string $table;
23+
24+
public function __construct(ConnectionInterface $connection, string $table)
25+
{
26+
$this->table = $table;
27+
$this->connection = $connection;
28+
}
29+
30+
public function matches($data): bool
31+
{
32+
return $this->query($data)->count() > 0;
33+
}
34+
35+
public function failureDescription($data): string
36+
{
37+
return sprintf(
38+
'a row in the table [%s] matches the attributes %s',
39+
$this->table,
40+
json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
41+
);
42+
}
43+
44+
public function toString(): string
45+
{
46+
return sprintf('There is a record of table %s with the specified data', $this->table);
47+
}
48+
49+
private function query(array $data): Builder
50+
{
51+
/** @var Builder $query */
52+
$query = $this->connection->table($this->table);
53+
54+
foreach ($data as $index => $value) {
55+
if (is_array($value)) {
56+
[$column, $operator, $expected] = $value;
57+
$query->where($column, $operator, $expected);
58+
continue;
59+
}
60+
61+
$query->where($index, $value);
62+
}
63+
64+
return $query;
65+
}
66+
}

src/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
2727
use Concerns\InteractsWithModelFactory;
2828
use Concerns\MakesHttpRequests;
2929
use Concerns\RunTestsInCoroutine;
30+
use Concerns\InteractsWithDatabase;
3031

3132
/**
3233
* The callbacks that should be run after the application is created.

0 commit comments

Comments
 (0)