File: ValueExtractorTraitTest.php

package info (click to toggle)
php-ramsey-collection 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: php: 3,239; xml: 31; makefile: 22
file content (91 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

declare(strict_types=1);

namespace Ramsey\Collection\Test\Tool;

use Ramsey\Collection\Exception\InvalidPropertyOrMethod;
use Ramsey\Collection\Test\TestCase;
use Ramsey\Collection\Tool\ValueExtractorTrait;

/**
 * Cover up all possible outcomes of the ValueExtractorTrait.
 */
class ValueExtractorTraitTest extends TestCase
{
    public function testShouldRaiseExceptionWhenPropertyOrMethodNotExist(): void
    {
        $test = new class {
            use ValueExtractorTrait;

            /**
             * @return mixed
             */
            public function __invoke(string $propertyOrMethod)
            {
                return $this->extractValue($this, $propertyOrMethod);
            }

            public function getType(): string
            {
                return 'foo';
            }
        };

        $this->expectException(InvalidPropertyOrMethod::class);
        $this->expectExceptionMessage('Method or property "undefinedMethod" not defined in');

        $test('undefinedMethod');
    }

    public function testShouldExtractValueByMethod(): void
    {
        $test = new class {
            use ValueExtractorTrait;

            /**
             * @return mixed
             */
            public function __invoke(string $propertyOrMethod)
            {
                return $this->extractValue($this, $propertyOrMethod);
            }

            public function testMethod(): string
            {
                return 'works!';
            }

            public function getType(): string
            {
                return 'bar';
            }
        };

        $this->assertSame('works!', $test('testMethod'), 'Could not extract value by method');
    }

    public function testShouldExtractValueByProperty(): void
    {
        $test = new class {
            use ValueExtractorTrait;

            public string $testProperty = 'works!';

            /**
             * @return mixed
             */
            public function __invoke(string $propertyOrMethod)
            {
                return $this->extractValue($this, $propertyOrMethod);
            }

            public function getType(): string
            {
                return 'baz';
            }
        };

        $this->assertSame('works!', $test('testProperty'), 'Could not extract value by property');
    }
}