BaseClientTest.php 4.74 KB
Newer Older
1 2 3 4
<?php

namespace yiiunit\extensions\authclient;

5
use yii\authclient\BaseClient;
6

7
class BaseClientTest extends TestCase
8
{
9 10 11
    public function testSetGet()
    {
        $client = new Client();
12

13 14 15
        $id = 'test_id';
        $client->setId($id);
        $this->assertEquals($id, $client->getId(), 'Unable to setup id!');
16

17 18 19
        $name = 'test_name';
        $client->setName($name);
        $this->assertEquals($name, $client->getName(), 'Unable to setup name!');
20

21 22 23
        $title = 'test_title';
        $client->setTitle($title);
        $this->assertEquals($title, $client->getTitle(), 'Unable to setup title!');
24

25 26 27 28 29 30
        $userAttributes = [
            'attribute1' => 'value1',
            'attribute2' => 'value2',
        ];
        $client->setUserAttributes($userAttributes);
        $this->assertEquals($userAttributes, $client->getUserAttributes(), 'Unable to setup user attributes!');
31

32 33 34 35 36 37
        $normalizeUserAttributeMap = [
            'name' => 'some/name',
            'email' => 'some/email',
        ];
        $client->setNormalizeUserAttributeMap($normalizeUserAttributeMap);
        $this->assertEquals($normalizeUserAttributeMap, $client->getNormalizeUserAttributeMap(), 'Unable to setup normalize user attribute map!');
38

39 40 41 42 43 44 45
        $viewOptions = [
            'option1' => 'value1',
            'option2' => 'value2',
        ];
        $client->setViewOptions($viewOptions);
        $this->assertEquals($viewOptions, $client->getViewOptions(), 'Unable to setup view options!');
    }
46

47 48 49
    public function testGetDefaults()
    {
        $client = new Client();
50

51 52 53 54 55
        $this->assertNotEmpty($client->getName(), 'Unable to get default name!');
        $this->assertNotEmpty($client->getTitle(), 'Unable to get default title!');
        $this->assertNotNull($client->getViewOptions(), 'Unable to get default view options!');
        $this->assertNotNull($client->getNormalizeUserAttributeMap(), 'Unable to get default normalize user attribute map!');
    }
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
     * Data provider for [[testNormalizeUserAttributes()]]
     * @return array test data
     */
    public function dataProviderNormalizeUserAttributes()
    {
        return [
            [
                [
                    'name' => 'raw/name',
                    'email' => 'raw/email',
                ],
                [
                    'raw/name' => 'name value',
                    'raw/email' => 'email value',
                ],
                [
                    'name' => 'name value',
                    'email' => 'email value',
                ],
            ],
            [
                [
                    'name' => function ($attributes) {
                            return $attributes['firstName'] . ' ' . $attributes['lastName'];
                        },
                ],
                [
                    'firstName' => 'John',
                    'lastName' => 'Smith',
                ],
                [
                    'name' => 'John Smith',
                ],
            ],
            [
                [
                    'email' => ['emails', 'prime'],
                ],
                [
                    'emails' => [
                        'prime' => 'some@email.com'
                    ],
                ],
                [
                    'email' => 'some@email.com',
                ],
            ],
            [
                [
                    'email' => ['emails', 0],
                    'secondaryEmail' => ['emails', 1],
                ],
                [
                    'emails' => [
                        'some@email.com',
                    ],
                ],
                [
                    'email' => 'some@email.com',
                ],
            ],
119 120 121 122 123 124 125 126 127 128 129
            [
                [
                    'name' => 'file_get_contents',
                ],
                [
                    'file_get_contents' => 'value',
                ],
                [
                    'name' => 'value',
                ],
            ],
130 131 132 133 134 135
        ];
    }

    /**
     * @dataProvider dataProviderNormalizeUserAttributes
     *
136
     * @depends testSetGet
137 138 139 140
     *
     * @param array $normalizeUserAttributeMap
     * @param array $rawUserAttributes
     * @param array $expectedNormalizedUserAttributes
141
     */
142
    public function testNormalizeUserAttributes($normalizeUserAttributeMap, $rawUserAttributes, $expectedNormalizedUserAttributes)
143 144 145
    {
        $client = new Client();
        $client->setNormalizeUserAttributeMap($normalizeUserAttributeMap);
146

147 148
        $client->setUserAttributes($rawUserAttributes);
        $normalizedUserAttributes = $client->getUserAttributes();
149 150

        $this->assertEquals(array_merge($rawUserAttributes, $expectedNormalizedUserAttributes), $normalizedUserAttributes);
151
    }
152 153
}

154
class Client extends BaseClient
155
{
AlexGx committed
156
}