LazyExtractorTest::testLazyLoadGet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 57
rs 9.6818
cc 1
eloc 44
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\ExtractorInterface;
6
use OAuth\UserData\Extractor\LazyExtractor;
7
8
/**
9
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-02-08 at 10:56:49.
10
 */
11
class LazyExtractorTest extends \PHPUnit_Framework_TestCase
12
{
13
14
    /**
15
     * Sets up the fixture, for example, opens a network connection.
16
     * This method is called before a test is executed.
17
     */
18
    protected function setUp()
19
    {
20
    }
21
22
    /**
23
     * Tears down the fixture, for example, closes a network connection.
24
     * This method is called after a test is executed.
25
     */
26
    protected function tearDown()
27
    {
28
    }
29
30
    public function testLazyLoadGet()
31
    {
32
        $loadersMap = array(
33
            ExtractorInterface::FIELD_UNIQUE_ID => 'id',
34
            ExtractorInterface::FIELD_USERNAME => 'profile'
35
        );
36
        $normalizersMap = array(
37
            ExtractorInterface::FIELD_UNIQUE_ID => ExtractorInterface::FIELD_UNIQUE_ID,
38
            ExtractorInterface::FIELD_USERNAME => ExtractorInterface::FIELD_USERNAME
39
        );
40
        $supports = array(
41
            ExtractorInterface::FIELD_UNIQUE_ID,
42
            ExtractorInterface::FIELD_USERNAME
43
        );
44
        $fields = array(
45
            ExtractorInterface::FIELD_UNIQUE_ID => 123
46
        );
47
        $constructorArgs = array(
48
            $loadersMap,
49
            $normalizersMap,
50
            $supports,
51
            $fields
52
        );
53
        $methods = array(
54
            'idLoader',
55
            'uniqueIdNormalizer',
56
            'profileLoader',
57
            'usernameNormalizer'
58
        );
59
        $profileData = array(
60
            'data' => array(
61
                'nickname' => 'johnnydonny'
62
            )
63
        );
64
        $extractor = $this->getMockBuilder('\\OAuth\\UserData\\Extractor\\LazyExtractor')
65
            ->setMethods($methods)
66
            ->setConstructorArgs($constructorArgs)
67
            ->getMock();
68
        $extractor->expects($this->never())
69
            ->method('idLoader');
70
        $extractor->expects($this->never())
71
            ->method('uniqueIdNormalizer');
72
        $extractor->expects($this->once())
73
            ->method('profileLoader')
74
            ->with()
75
            ->will($this->returnValue($profileData));
76
        $extractor->expects($this->once())
77
            ->method('usernameNormalizer')
78
            ->with($profileData)
79
            ->will($this->returnValue($profileData['data']['nickname']));
80
        /**
81
         * @var \OAuth\UserData\Extractor\LazyExtractor $extractor
82
         */
83
        $this->assertEquals(123, $extractor->getUniqueId()); // prefetched field, does not trigger loader and normalizer
84
        $this->assertEquals('johnnydonny', $extractor->getUsername()); // triggers the loader and the normalizer
85
        $this->assertEquals('johnnydonny', $extractor->getUsername()); // does not trigger them again
86
    }
87
88
    public function testUnsupportedField()
89
    {
90
        $extractor = new LazyExtractor();
91
        $this->assertNull($extractor->getUniqueId());
92
    }
93
}
94