ExtractorTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 72
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 2
Metric Value
c 3
b 2
f 2
dl 0
loc 72
rs 9.102
cc 1
eloc 61
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace OAuth\Unit\UserData\Extractor;
4
5
use OAuth\UserData\Extractor\Extractor;
6
use OAuth\UserData\Extractor\ExtractorInterface;
7
8
/**
9
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-02-08 at 10:56:38.
10
 */
11
class ExtractorTest extends \PHPUnit_Framework_TestCase
12
{
13
14
    protected $fields;
15
16
    protected $fieldSupportFunctions;
17
18
    protected $fieldGetFunctions;
19
20
    protected $fieldValues;
21
22
    /**
23
     * Sets up the fixture, for example, opens a network connection.
24
     * This method is called before a test is executed.
25
     */
26
    protected function setUp()
27
    {
28
        $this->fields = array(
29
            ExtractorInterface::FIELD_UNIQUE_ID,
30
            ExtractorInterface::FIELD_USERNAME,
31
            ExtractorInterface::FIELD_FIRST_NAME,
32
            ExtractorInterface::FIELD_LAST_NAME,
33
            ExtractorInterface::FIELD_FULL_NAME,
34
            ExtractorInterface::FIELD_EMAIL,
35
            ExtractorInterface::FIELD_DESCRIPTION,
36
            ExtractorInterface::FIELD_LOCATION,
37
            ExtractorInterface::FIELD_PROFILE_URL,
38
            ExtractorInterface::FIELD_IMAGE_URL,
39
            ExtractorInterface::FIELD_WEBSITES,
40
            ExtractorInterface::FIELD_VERIFIED_EMAIL,
41
            ExtractorInterface::FIELD_EXTRA
42
        );
43
44
        $this->fieldSupportFunctions = array(
45
            ExtractorInterface::FIELD_UNIQUE_ID         => 'supportsUniqueId',
46
            ExtractorInterface::FIELD_USERNAME          => 'supportsUsername',
47
            ExtractorInterface::FIELD_FIRST_NAME        => 'supportsFirstName',
48
            ExtractorInterface::FIELD_LAST_NAME         => 'supportsLastName',
49
            ExtractorInterface::FIELD_FULL_NAME         => 'supportsFullName',
50
            ExtractorInterface::FIELD_EMAIL             => 'supportsEmail',
51
            ExtractorInterface::FIELD_DESCRIPTION       => 'supportsDescription',
52
            ExtractorInterface::FIELD_LOCATION          => 'supportsLocation',
53
            ExtractorInterface::FIELD_PROFILE_URL       => 'supportsProfileUrl',
54
            ExtractorInterface::FIELD_IMAGE_URL         => 'supportsImageUrl',
55
            ExtractorInterface::FIELD_WEBSITES          => 'supportsWebsites',
56
            ExtractorInterface::FIELD_VERIFIED_EMAIL    => 'supportsVerifiedEmail',
57
            ExtractorInterface::FIELD_EXTRA             => 'supportsExtra',
58
        );
59
60
        $this->fieldGetFunctions = array(
61
            ExtractorInterface::FIELD_UNIQUE_ID         => 'getUniqueId',
62
            ExtractorInterface::FIELD_USERNAME          => 'getUsername',
63
            ExtractorInterface::FIELD_FIRST_NAME        => 'getFirstName',
64
            ExtractorInterface::FIELD_LAST_NAME         => 'getLastName',
65
            ExtractorInterface::FIELD_FULL_NAME         => 'getFullName',
66
            ExtractorInterface::FIELD_EMAIL             => 'getEmail',
67
            ExtractorInterface::FIELD_DESCRIPTION       => 'getDescription',
68
            ExtractorInterface::FIELD_LOCATION          => 'getLocation',
69
            ExtractorInterface::FIELD_PROFILE_URL       => 'getProfileUrl',
70
            ExtractorInterface::FIELD_IMAGE_URL         => 'getImageUrl',
71
            ExtractorInterface::FIELD_WEBSITES          => 'getWebsites',
72
            ExtractorInterface::FIELD_VERIFIED_EMAIL    => 'isEmailVerified',
73
            ExtractorInterface::FIELD_EXTRA             => 'getExtras',
74
        );
75
76
        $this->fieldValues = array(
77
            ExtractorInterface::FIELD_UNIQUE_ID     => '1234567',
78
            ExtractorInterface::FIELD_USERNAME      => 'johnnydonny',
79
            ExtractorInterface::FIELD_FIRST_NAME    => 'john',
80
            ExtractorInterface::FIELD_LAST_NAME     => 'doe',
81
            ExtractorInterface::FIELD_FULL_NAME     => 'john doe',
82
            ExtractorInterface::FIELD_EMAIL         => '[email protected]',
83
            ExtractorInterface::FIELD_DESCRIPTION   => 'A life on the edge',
84
            ExtractorInterface::FIELD_LOCATION      => 'Rome, Italy',
85
            ExtractorInterface::FIELD_PROFILE_URL   => 'http://social.co/johnnydonny',
86
            ExtractorInterface::FIELD_IMAGE_URL     => 'http://social.co/avatar/johnnydonny.jpg',
87
            ExtractorInterface::FIELD_WEBSITES      => array(
88
                'http://johnnydonny.com',
89
                'http://blog.johnnydonny.com',
90
            ),
91
            ExtractorInterface::FIELD_VERIFIED_EMAIL => true,
92
            ExtractorInterface::FIELD_EXTRA         => array(
93
                'foo' => 'bar',
94
                'skills' => array('php', 'symfony', 'butterflies')
95
            ),
96
        );
97
    }
98
99
    /**
100
     * Tears down the fixture, for example, closes a network connection.
101
     * This method is called after a test is executed.
102
     */
103
    protected function tearDown()
104
    {
105
    }
106
107
    public function testSupportsFields()
108
    {
109
        foreach ($this->fields as $field) {
110
            $supports = array($field);
111
            $value = $this->fieldValues[$field];
112
            $fields = array($field => $value);
113
            $extractor = new Extractor($supports, $fields);
114
            $supportFunction = $this->fieldSupportFunctions[$field];
115
            $getFunction = $this->fieldGetFunctions[$field];
116
            $message = sprintf('Failed asserting that "%s" must return true', $supportFunction);
117
            $this->assertTrue($extractor->$supportFunction(), $message);
118
            $message = sprintf('Failed asserting that "%s" must return %s', $getFunction, json_encode($value));
119
            $this->assertEquals($value, $extractor->$getFunction(), $message);
120
        }
121
    }
122
123
    public function testDoesNotSupportFields()
124
    {
125
        $supports = array();
126
        $fields = array();
127
        foreach ($this->fields as $field) {
128
            $extractor = new Extractor($supports, $fields);
129
            $supportFunction = $this->fieldSupportFunctions[$field];
130
            $getFunction = $this->fieldGetFunctions[$field];
131
            $message = sprintf('Failed asserting that "%s" must return false', $supportFunction);
132
            $this->assertFalse($extractor->$supportFunction(), $message);
133
            $message = sprintf('Failed asserting that "%s" must return null', $getFunction);
134
            $this->assertNull($extractor->$getFunction(), $message);
135
        }
136
    }
137
138
    public function testGetExtra()
139
    {
140
        $supports = array(ExtractorInterface::FIELD_EXTRA);
141
        $fields = array(ExtractorInterface::FIELD_EXTRA => array(
142
            'foo' => 'bar',
143
            'bar' => 'baz'
144
        ));
145
        $extractor = new Extractor($supports, $fields);
146
        $this->assertEquals('bar', $extractor->getExtra('foo'));
147
        $this->assertEquals('baz', $extractor->getExtra('bar'));
148
        $this->assertNull($extractor->getExtra('baz'));
149
    }
150
151
    public function testSetService()
152
    {
153
        /**
154
         * @var \OAuth\Common\Service\ServiceInterface $service
155
         */
156
        $service = $this->getMock('\\OAuth\\Common\\Service\\ServiceInterface');
157
        $extractor = new Extractor();
158
        $extractor->setService($service);
159
    }
160
}
161