master

laravel/framework

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

Interaction.php

TLDR

This file defines a trait called Interaction in the namespace Illuminate\Testing\Fluent\Concerns. The trait contains methods for marking properties as interacted and checking if all properties have been interacted with.

Methods

interactsWith

This method is used to mark a property as interacted. It takes a string parameter $key which represents the property to be marked.

interacted

This method is used to assert that all properties have been interacted with. It compares the list of interacted properties with the properties retrieved using the prop method. If any unexpected properties are found, an assertion failure is thrown.

etc

This method disables the interaction check by populating the array of interacted properties with all property keys.

Classes

None

<?php

namespace Illuminate\Testing\Fluent\Concerns;

use Illuminate\Support\Str;
use PHPUnit\Framework\Assert as PHPUnit;

trait Interaction
{
    /**
     * The list of interacted properties.
     *
     * @var array
     */
    protected $interacted = [];

    /**
     * Marks the property as interacted.
     *
     * @param  string  $key
     * @return void
     */
    protected function interactsWith(string $key): void
    {
        $prop = Str::before($key, '.');

        if (! in_array($prop, $this->interacted, true)) {
            $this->interacted[] = $prop;
        }
    }

    /**
     * Asserts that all properties have been interacted with.
     *
     * @return void
     */
    public function interacted(): void
    {
        PHPUnit::assertSame(
            [],
            array_diff(array_keys($this->prop()), $this->interacted),
            $this->path
                ? sprintf('Unexpected properties were found in scope [%s].', $this->path)
                : 'Unexpected properties were found on the root level.'
        );
    }

    /**
     * Disables the interaction check.
     *
     * @return $this
     */
    public function etc(): self
    {
        $this->interacted = array_keys($this->prop());

        return $this;
    }

    /**
     * Retrieve a prop within the current scope using "dot" notation.
     *
     * @param  string|null  $key
     * @return mixed
     */
    abstract protected function prop(string $key = null);
}