File: ObjectIdentityTest.php

package info (click to toggle)
php-symfony-security-acl 3.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,020 kB
  • sloc: php: 5,160; sql: 273; makefile: 25; xml: 20
file content (134 lines) | stat: -rw-r--r-- 4,631 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Acl\Tests\Domain
{
    use PHPUnit\Framework\Attributes\DataProvider;
    use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
    use Symfony\Component\Security\Acl\Model\DomainObjectInterface;

    class ObjectIdentityTest extends \PHPUnit\Framework\TestCase
    {
        public function testConstructor()
        {
            $id = new ObjectIdentity('fooid', 'footype');

            $this->assertEquals('fooid', $id->getIdentifier());
            $this->assertEquals('footype', $id->getType());
        }

        // Test that constructor never changes passed type, even with proxies
        public function testConstructorWithProxy()
        {
            $id = new ObjectIdentity('fooid', 'Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject');

            $this->assertEquals('fooid', $id->getIdentifier());
            $this->assertEquals('Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject', $id->getType());
        }

        public function testFromDomainObjectPrefersInterfaceOverGetId()
        {
            $domainObject = new class() implements DomainObjectInterface {
                public function getObjectIdentifier()
                {
                    return 'getObjectIdentifier()';
                }

                public function getId()
                {
                    return 'getId()';
                }
            };

            $id = ObjectIdentity::fromDomainObject($domainObject);
            $this->assertEquals('getObjectIdentifier()', $id->getIdentifier());
        }

        public function testFromDomainObjectWithoutInterface()
        {
            $id = ObjectIdentity::fromDomainObject(new TestDomainObject());
            $this->assertEquals('getId()', $id->getIdentifier());
            $this->assertEquals(TestDomainObject::class, $id->getType());
        }

        public function testFromDomainObjectWithProxy()
        {
            $id = ObjectIdentity::fromDomainObject(new \Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject());
            $this->assertEquals('getId()', $id->getIdentifier());
            $this->assertEquals(TestDomainObject::class, $id->getType());
        }

        public function testFromDomainObjectWithoutInterfaceEnforcesStringIdentifier()
        {
            $domainObject = new TestDomainObject();
            $domainObject->id = 1;
            $id = ObjectIdentity::fromDomainObject($domainObject);

            $this->assertSame('1', $id->getIdentifier());
            $this->assertEquals(TestDomainObject::class, $id->getType());
        }

        public function testFromDomainObjectWithoutInterfaceAllowsZeroAsIdentifier()
        {
            $domainObject = new TestDomainObject();
            $domainObject->id = '0';
            $id = ObjectIdentity::fromDomainObject($domainObject);

            $this->assertSame('0', $id->getIdentifier());
            $this->assertEquals(TestDomainObject::class, $id->getType());
        }

        /**
         * @dataProvider getCompareData
         */
        #[DataProvider('getCompareData')]
        public function testEquals($oid1, $oid2, $equal)
        {
            if ($equal) {
                $this->assertTrue($oid1->equals($oid2));
            } else {
                $this->assertFalse($oid1->equals($oid2));
            }
        }

        public static function getCompareData()
        {
            return [
                [new ObjectIdentity('123', 'foo'), new ObjectIdentity('123', 'foo'), true],
                [new ObjectIdentity('123', 'foo'), new ObjectIdentity(123, 'foo'), true],
                [new ObjectIdentity('1', 'foo'), new ObjectIdentity('2', 'foo'), false],
                [new ObjectIdentity('1', 'bla'), new ObjectIdentity('1', 'blub'), false],
            ];
        }
    }

    class TestDomainObject
    {
        public $id = 'getId()';

        public function getObjectIdentifier()
        {
            return 'getObjectIdentifier()';
        }

        public function getId()
        {
            return $this->id;
        }
    }
}

namespace Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain
{
    class TestDomainObject extends \Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject
    {
    }
}