File: PropertyTypeMatcherTest.php

package info (click to toggle)
php-deepcopy 1.13.4-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 612 kB
  • sloc: php: 1,793; makefile: 26
file content (82 lines) | stat: -rw-r--r-- 2,014 bytes parent folder | download | duplicates (2)
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
<?php declare(strict_types=1);

namespace DeepCopyTest\Matcher;

use DeepCopy\f009;
use DeepCopy\Matcher\PropertyTypeMatcher;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;

/**
 * @covers \DeepCopy\Matcher\PropertyTypeMatcher
 */
class PropertyTypeMatcherTest extends TestCase
{
    /**
     * @dataProvider providePairs
     */
    #[DataProvider('providePairs')]
    public function test_it_matches_the_given_property($object, $expected)
    {
        $matcher = new PropertyTypeMatcher(PropertyTypeMatcherTestFixture2::class);

        $actual = $matcher->matches($object, 'foo');

        $this->assertEquals($expected, $actual);
    }

    /**
     * @requires PHP 7.4
     */
    public function test_it_ignores_uninitialized_typed_properties()
    {
        $object = new f009\TypedObjectProperty();

        $matcher = new PropertyTypeMatcher(\DateTime::class);

        $this->assertFalse($matcher->matches($object, 'date'));
    }

    /**
     * @requires PHP 7.4
     */
    public function test_it_matches_initialized_typed_properties()
    {
        $object = new f009\TypedObjectProperty();
        $object->date = new \DateTime();

        $matcher = new PropertyTypeMatcher(\DateTime::class);

        $this->assertTrue($matcher->matches($object, 'date'));
    }

    public static function providePairs()
    {
        $object1 = new PropertyTypeMatcherTestFixture1();
        $object1->foo = new PropertyTypeMatcherTestFixture2();

        $object2 = new PropertyTypeMatcherTestFixture1();
        $object2->foo = new stdClass();

        $object3 = new PropertyTypeMatcherTestFixture1();
        $object3->foo = true;

        return [
            [new PropertyTypeMatcherTestFixture1(), false],
            [$object1, true],
            [$object2, false],
            [$object3, false],
            [new stdClass(), false],
        ];
    }
}

class PropertyTypeMatcherTestFixture1
{
    public $foo;
}

class PropertyTypeMatcherTestFixture2
{
}