Skip to content

Commit

Permalink
Added: Test Interacts With Database (#6386)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-angeli-gimenes authored Dec 17, 2023
1 parent a0b4f54 commit 00be472
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Concerns/InteractsWithDatabase.php
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);
}
}
66 changes: 66 additions & 0 deletions src/Constraint/HasInDatabase.php
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;
}
}
1 change: 1 addition & 0 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
use Concerns\InteractsWithModelFactory;
use Concerns\MakesHttpRequests;
use Concerns\RunTestsInCoroutine;
use Concerns\InteractsWithDatabase;

/**
* The callbacks that should be run after the application is created.
Expand Down

0 comments on commit 00be472

Please sign in to comment.