master

laravel/framework

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

SeeInOrder.php

TLDR

This file contains the SeeInOrder class, which is a constraint class used for testing purposes in the Illuminate Testing package. The class extends the Constraint class from the PHPUnit framework and is used to determine if a given array of values appears in a specified order within a given string.

Classes

SeeInOrder

The SeeInOrder class is a constraint class that extends the Constraint class from the PHPUnit framework. It is used to determine if a given array of values appears in a specified order within a given string. The class has the following properties:

  • $content: The string under validation.
  • $failedValue: The last value that failed to pass validation.

The class contains the following methods:

  • __construct($content): Constructor method that initializes the $content property with the provided string.
  • matches($values): Method that takes an array of values and determines if they appear in the specified order within the string. It returns a boolean indicating whether the rule passes validation.
  • failureDescription($values): Method that returns a string describing the failure if the rule does not pass validation.
  • toString(): Method that returns a string representation of the object.
<?php

namespace Illuminate\Testing\Constraints;

use PHPUnit\Framework\Constraint\Constraint;
use ReflectionClass;

class SeeInOrder extends Constraint
{
    /**
     * The string under validation.
     *
     * @var string
     */
    protected $content;

    /**
     * The last value that failed to pass validation.
     *
     * @var string
     */
    protected $failedValue;

    /**
     * Create a new constraint instance.
     *
     * @param  string  $content
     * @return void
     */
    public function __construct($content)
    {
        $this->content = $content;
    }

    /**
     * Determine if the rule passes validation.
     *
     * @param  array  $values
     * @return bool
     */
    public function matches($values): bool
    {
        $position = 0;

        foreach ($values as $value) {
            if (empty($value)) {
                continue;
            }

            $valuePosition = mb_strpos($this->content, $value, $position);

            if ($valuePosition === false || $valuePosition < $position) {
                $this->failedValue = $value;

                return false;
            }

            $position = $valuePosition + mb_strlen($value);
        }

        return true;
    }

    /**
     * Get the description of the failure.
     *
     * @param  array  $values
     * @return string
     */
    public function failureDescription($values): string
    {
        return sprintf(
            'Failed asserting that \'%s\' contains "%s" in specified order.',
            $this->content,
            $this->failedValue
        );
    }

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