-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: Test Interacts With Database (#6386)
- Loading branch information
1 parent
a0b4f54
commit 00be472
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact group@hyperf.io | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Testing\Concerns; | ||
|
||
use Hyperf\Context\ApplicationContext; | ||
use Hyperf\Database\ConnectionInterface; | ||
use Hyperf\Database\ConnectionResolverInterface; | ||
use Hyperf\Testing\Constraint\HasInDatabase; | ||
use PHPUnit\Framework\Constraint\LogicalNot; | ||
|
||
trait InteractsWithDatabase | ||
{ | ||
private function assertDatabaseHas(string $table, array $data, ?string $connection = null): void | ||
{ | ||
$this->assertThat($data, new HasInDatabase($this->getConnection($connection), $table)); | ||
} | ||
|
||
private function assertDatabaseMissing(string $table, array $data, ?string $connection = null): void | ||
{ | ||
$this->assertThat($data, new LogicalNot(new HasInDatabase($this->getConnection($connection), $table))); | ||
} | ||
|
||
private function getConnection(?string $name): ConnectionInterface | ||
{ | ||
return ApplicationContext::getContainer()->get(ConnectionResolverInterface::class)->connection($name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact group@hyperf.io | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Testing\Constraint; | ||
|
||
use Hyperf\Database\ConnectionInterface; | ||
use Hyperf\Database\Query\Builder; | ||
use PHPUnit\Framework\Constraint\Constraint; | ||
|
||
class HasInDatabase extends Constraint | ||
{ | ||
protected ConnectionInterface $connection; | ||
|
||
protected string $table; | ||
|
||
public function __construct(ConnectionInterface $connection, string $table) | ||
{ | ||
$this->table = $table; | ||
$this->connection = $connection; | ||
} | ||
|
||
public function matches($data): bool | ||
{ | ||
return $this->query($data)->count() > 0; | ||
} | ||
|
||
public function failureDescription($data): string | ||
{ | ||
return sprintf( | ||
'a row in the table [%s] matches the attributes %s', | ||
$this->table, | ||
json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) | ||
); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return sprintf('There is a record of table %s with the specified data', $this->table); | ||
} | ||
|
||
private function query(array $data): Builder | ||
{ | ||
/** @var Builder $query */ | ||
$query = $this->connection->table($this->table); | ||
|
||
foreach ($data as $index => $value) { | ||
if (is_array($value)) { | ||
[$column, $operator, $expected] = $value; | ||
$query->where($column, $operator, $expected); | ||
continue; | ||
} | ||
|
||
$query->where($index, $value); | ||
} | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters