Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Count.php

class Count

Same name in this branch
  1. 11.1.x vendor/symfony/validator/Constraints/Count.php \Symfony\Component\Validator\Constraints\Count

@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit

Hierarchy

  • class \PHPUnit\Framework\Constraint\Constraint implements \Countable, \PHPUnit\Framework\SelfDescribing
    • class \PHPUnit\Framework\Constraint\Count extends \PHPUnit\Framework\Constraint\Constraint

Expanded class hierarchy of Count

2 files declare their use of Count
Assert.php in vendor/phpunit/phpunit/src/Framework/Assert.php
Functions.php in vendor/phpunit/phpunit/src/Framework/Assert/Functions.php
24 string references to 'Count'
AssertSameWithCountRule::processNode in vendor/phpstan/phpstan-phpunit/src/Rules/PHPUnit/AssertSameWithCountRule.php
AssertTypeSpecifyingExtensionHelper::getExpressionResolvers in vendor/phpstan/phpstan-phpunit/src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php
*
BumpCommand::doBump in vendor/composer/composer/src/Composer/Command/BumpCommand.php
Clover::process in vendor/phpunit/php-code-coverage/src/Report/Clover.php
CommentStorage::getDisplayOrdinal in core/modules/comment/src/CommentStorage.php
Gets the display ordinal or page number for a comment.

... See full list

File

vendor/phpunit/phpunit/src/Framework/Constraint/Cardinality/Count.php, line 27

Namespace

PHPUnit\Framework\Constraint
View source
class Count extends Constraint {
    private readonly int $expectedCount;
    public function __construct(int $expected) {
        $this->expectedCount = $expected;
    }
    public function toString() : string {
        return sprintf('count matches %d', $this->expectedCount);
    }
    
    /**
     * Evaluates the constraint for parameter $other. Returns true if the
     * constraint is met, false otherwise.
     *
     * @throws Exception
     */
    protected function matches(mixed $other) : bool {
        return $this->expectedCount === $this->getCountOf($other);
    }
    
    /**
     * @throws Exception
     */
    protected function getCountOf(mixed $other) : ?int {
        if (is_countable($other)) {
            return count($other);
        }
        if ($other instanceof EmptyIterator) {
            return 0;
        }
        if ($other instanceof Traversable) {
            while ($other instanceof IteratorAggregate) {
                try {
                    $other = $other->getIterator();
                } catch (\Exception $e) {
                    throw new Exception($e->getMessage(), $e->getCode(), $e);
                }
            }
            $iterator = $other;
            if ($iterator instanceof Generator) {
                throw new GeneratorNotSupportedException();
            }
            if (!$iterator instanceof Iterator) {
                return iterator_count($iterator);
            }
            $key = $iterator->key();
            $count = iterator_count($iterator);
            // Manually rewind $iterator to previous key, since iterator_count
            // moves pointer.
            if ($key !== null) {
                $iterator->rewind();
                while ($iterator->valid() && $key !== $iterator->key()) {
                    $iterator->next();
                }
            }
            return $count;
        }
        return null;
    }
    
    /**
     * Returns the description of the failure.
     *
     * The beginning of failure messages is "Failed asserting that" in most
     * cases. This method should return the second part of that sentence.
     *
     * @throws Exception
     */
    protected function failureDescription(mixed $other) : string {
        return sprintf('actual size %d matches expected size %d', (int) $this->getCountOf($other), $this->expectedCount);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Constraint::additionalFailureDescription protected function Return additional failure description where needed. 7
Constraint::count public function Counts the number of constraint elements. 3
Constraint::evaluate public function Evaluates the constraint for parameter $other. 7
Constraint::exporter protected function
Constraint::fail protected function Throws an exception for the given compared value and test description. 1
Constraint::failureDescriptionInContext protected function Returns the description of the failure when this constraint appears in
context of an $operator expression.
Constraint::reduce protected function Reduces the sub-expression starting at $this by skipping degenerate
sub-expression and returns first descendant constraint that starts
a non-reducible sub-expression.
2
Constraint::toStringInContext protected function Returns a custom string representation of the constraint object when it
appears in context of an $operator expression.
Constraint::valueToTypeStringFragment protected function @psalm-return non-empty-string
Count::$expectedCount private property
Count::failureDescription protected function Returns the description of the failure. Overrides Constraint::failureDescription
Count::getCountOf protected function
Count::matches protected function Evaluates the constraint for parameter $other. Returns true if the
constraint is met, false otherwise.
Overrides Constraint::matches
Count::toString public function Returns a string representation of the object. Overrides SelfDescribing::toString
Count::__construct public function 1

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal