master

laravel/framework

Last updated at: 29/12/2023 09:25

CountInDatabase.php

TLDR

The CountInDatabase.php file is a class that extends the Constraint class from PHPUnit. It is used to assert that the count of entries in a database table matches an expected count.

Classes

Class CountInDatabase

The CountInDatabase class extends the Constraint class from PHPUnit. It is used to assert that the count of entries in a database table matches an expected count. It has the following properties:

  • $database - An instance of the Illuminate\Database\Connection class representing the database connection.
  • $expectedCount - An integer representing the expected count of entries in the table.
  • $actualCount - An integer representing the actual count of entries in the table.

The CountInDatabase class has the following methods:

__construct(Connection $database, int $expectedCount)

  • Constructs a new instance of the CountInDatabase class.
  • Parameters:
    • $database - An instance of the Illuminate\Database\Connection class representing the database connection.
    • $expectedCount - An integer representing the expected count of entries in the table.

matches(string $table): bool

  • Checks if the expected and actual count of entries in the specified table are equal.
  • Returns true if the counts are equal, false otherwise.
  • Parameters:
    • $table - A string representing the name of the table to check.

failureDescription(string $table): string

  • Generates a description of the failure when the expected and actual counts do not match.
  • Returns a formatted string describing the failure.
  • Parameters:
    • $table - A string representing the name of the table.

toString(int $options = 0): string

  • Returns a string representation of the CountInDatabase object.
  • Returns the fully qualified name of the class.
  • Parameters:
    • $options - Optional integer representing options for the string representation (unused).
<?php

namespace Illuminate\Testing\Constraints;

use Illuminate\Database\Connection;
use PHPUnit\Framework\Constraint\Constraint;
use ReflectionClass;

class CountInDatabase extends Constraint
{
    /**
     * The database connection.
     *
     * @var \Illuminate\Database\Connection
     */
    protected $database;

    /**
     * The expected table entries count that will be checked against the actual count.
     *
     * @var int
     */
    protected $expectedCount;

    /**
     * The actual table entries count that will be checked against the expected count.
     *
     * @var int
     */
    protected $actualCount;

    /**
     * Create a new constraint instance.
     *
     * @param  \Illuminate\Database\Connection  $database
     * @param  int  $expectedCount
     * @return void
     */
    public function __construct(Connection $database, int $expectedCount)
    {
        $this->expectedCount = $expectedCount;

        $this->database = $database;
    }

    /**
     * Check if the expected and actual count are equal.
     *
     * @param  string  $table
     * @return bool
     */
    public function matches($table): bool
    {
        $this->actualCount = $this->database->table($table)->count();

        return $this->actualCount === $this->expectedCount;
    }

    /**
     * Get the description of the failure.
     *
     * @param  string  $table
     * @return string
     */
    public function failureDescription($table): string
    {
        return sprintf(
            "table [%s] matches expected entries count of %s. Entries found: %s.\n",
            $table, $this->expectedCount, $this->actualCount
        );
    }

    /**
     * Get a string representation of the object.
     *
     * @param  int  $options
     * @return string
     */
    public function toString($options = 0): string
    {
        return (new ReflectionClass($this))->name;
    }
}