PHP Classes

File: tests/Attributes/CallableAttrTest.php

Recommend this page to a friend!
  Packages of Rodolfo Berrios Arce   Parameter   tests/Attributes/CallableAttrTest.php   Download  
File: tests/Attributes/CallableAttrTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Parameter
Validate function parameters with PHP attributes
Author: By
Last change:
Date: 1 year ago
Size: 1,713 bytes
 

Contents

Class file image Download
<?php

/*
 * This file is part of Chevere.
 *
 * (c) Rodolfo Berrios <rodolfo@chevere.org>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Chevere\Tests\Attributes;

use Chevere\Parameter\Attributes\CallableAttr;
use Chevere\Parameter\Interfaces\StringParameterInterface;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use function Chevere\Parameter\string;

final class CallableAttrTest extends TestCase
{
    public function testCallableNoReturn(): void
    {
        $callable = function (): void {
        };
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage(
            <<<PLAIN
            Callable must return a `Chevere\Parameter\Interfaces\ParameterInterface` instance
            PLAIN
        );
        new CallableAttr($callable);
    }

    public function testCallableWrongReturn(): void
    {
        $callable = function (): string {
            return 'test';
        };
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage(
            <<<PLAIN
            Callable must return a `Chevere\Parameter\Interfaces\ParameterInterface` instance
            PLAIN
        );
        new CallableAttr($callable);
    }

    public function testConstruct(): void
    {
        $parameter = string('/^foo/');
        $callable = function () use ($parameter): StringParameterInterface {
            return $parameter;
        };
        $attr = new CallableAttr($callable);
        $this->assertSame($parameter, $attr->parameter());
        $attr('foo');
    }
}