InteractsWithDatabase.php 1.1 KB

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