ViewModelNodeTest::testViewModelNode()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 117
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 117
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 76
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 gotakk\ViewModelBundle\Tests\ViewModel;
4
5
use gotakk\ViewModelBundle\ViewModel\ViewModelNode;
6
7
class ViewModelNodeTest extends \PHPUnit_Framework_TestCase
8
{
9
    public function testViewModelNode()
10
    {
11
        // TEST SIMPLE OPERATIONS
12
        $vm = new ViewModelNode();
13
14
        $this->assertInstanceOf('gotakk\ViewModelBundle\ViewModel\ViewModelNode', $vm);
15
        $this->assertEmpty($vm->toArray());
16
17
        $vm->setAuthor('gotakk');
18
        $this->assertEquals($vm->toArray(), array(
19
            'author' => 'gotakk',
20
        ));
21
22
        $vm->setContactInfos(array(
23
            'tel' => '+1-202-555-0123',
24
            'fax' => '+1-202-555-0181',
25
            'mail' => '[email protected]',
26
        ));
27
        $this->assertEquals($vm->toArray(), array(
28
            'author' => 'gotakk',
29
            'contactInfos' => array(
30
                'tel' => '+1-202-555-0123',
31
                'fax' => '+1-202-555-0181',
32
                'mail' => '[email protected]',
33
            ),
34
        ));
35
36
        $movie = $vm->addMovie(array(
37
            'title' => "The Lord of the Rings: The Fellowship of the Ring",
38
            'resume' => "In the Second Age of Middle Earth, the Dark Lord Sauron forges the One Ring in Mount Doom to conquer the land. An alliance of men and elves battle Sauron’s forces in Mordor, where Isildur destroys Sauron by chopping off the Ring from his body. However, the Ring’s power corrupts Isildur to prevent its destruction. Isildur is assassinated by Orcs, but the Ring is lost for 2500 years until discovered by Sméagol who is consumed by the Ring and later named Gollum. After 500 years, it abandons him, only to be unexpectedly recovered by a Hobbit named Bilbo Baggins.",
39
        ));
40
41
        $this->assertInstanceOf('gotakk\ViewModelBundle\ViewModel\ViewModelNode', $movie);
42
        $this->assertEquals($movie->toArray(), array(
43
            'title' => "The Lord of the Rings: The Fellowship of the Ring",
44
            'resume' => "In the Second Age of Middle Earth, the Dark Lord Sauron forges the One Ring in Mount Doom to conquer the land. An alliance of men and elves battle Sauron’s forces in Mordor, where Isildur destroys Sauron by chopping off the Ring from his body. However, the Ring’s power corrupts Isildur to prevent its destruction. Isildur is assassinated by Orcs, but the Ring is lost for 2500 years until discovered by Sméagol who is consumed by the Ring and later named Gollum. After 500 years, it abandons him, only to be unexpectedly recovered by a Hobbit named Bilbo Baggins.",
45
        ));
46
47
        $this->assertEquals($vm->toArray(), array(
48
            'author' => 'gotakk',
49
            'contactInfos' => array(
50
                'tel' => '+1-202-555-0123',
51
                'fax' => '+1-202-555-0181',
52
                'mail' => '[email protected]',
53
            ),
54
            'movies' => array(
55
                array(
56
                    'title' => "The Lord of the Rings: The Fellowship of the Ring",
57
                    'resume' => "In the Second Age of Middle Earth, the Dark Lord Sauron forges the One Ring in Mount Doom to conquer the land. An alliance of men and elves battle Sauron’s forces in Mordor, where Isildur destroys Sauron by chopping off the Ring from his body. However, the Ring’s power corrupts Isildur to prevent its destruction. Isildur is assassinated by Orcs, but the Ring is lost for 2500 years until discovered by Sméagol who is consumed by the Ring and later named Gollum. After 500 years, it abandons him, only to be unexpectedly recovered by a Hobbit named Bilbo Baggins.",
58
                ),
59
            ),
60
        ));
61
62
        $movie->addCharacter('Frodon');
63
        $this->assertEquals($movie->toArray(), array(
64
            'title' => "The Lord of the Rings: The Fellowship of the Ring",
65
            'resume' => "In the Second Age of Middle Earth, the Dark Lord Sauron forges the One Ring in Mount Doom to conquer the land. An alliance of men and elves battle Sauron’s forces in Mordor, where Isildur destroys Sauron by chopping off the Ring from his body. However, the Ring’s power corrupts Isildur to prevent its destruction. Isildur is assassinated by Orcs, but the Ring is lost for 2500 years until discovered by Sméagol who is consumed by the Ring and later named Gollum. After 500 years, it abandons him, only to be unexpectedly recovered by a Hobbit named Bilbo Baggins.",
66
            'characters' => array(
67
                'Frodon',
68
            ),
69
        ));
70
71
        $movie->setProperties(array(
72
            'language' => 'english',
73
            'budget' => '$93 million',
74
            'boxOffice' => '$871.5 million',
75
        ));
76
77
        $this->assertEquals($movie->toArray(), array(
78
            'title' => "The Lord of the Rings: The Fellowship of the Ring",
79
            'resume' => "In the Second Age of Middle Earth, the Dark Lord Sauron forges the One Ring in Mount Doom to conquer the land. An alliance of men and elves battle Sauron’s forces in Mordor, where Isildur destroys Sauron by chopping off the Ring from his body. However, the Ring’s power corrupts Isildur to prevent its destruction. Isildur is assassinated by Orcs, but the Ring is lost for 2500 years until discovered by Sméagol who is consumed by the Ring and later named Gollum. After 500 years, it abandons him, only to be unexpectedly recovered by a Hobbit named Bilbo Baggins.",
80
            'characters' => array(
81
                'Frodon',
82
            ),
83
            'properties' => array(
84
                'language' => 'english',
85
                'budget' => '$93 million',
86
                'boxOffice' => '$871.5 million',
87
            ),
88
        ));
89
90
        $this->assertEquals($vm['movies'][0]['characters'][0], 'Frodon');
91
92
        $vm['movies'][0]['characters'][] = 'Sam';
93
        $this->assertTrue(isset($vm['movies'][0]['characters'][1]));
94
        $this->assertEquals($vm->getMovies()[0]->getCharacters()[1], 'Sam');
95
96
        unset($vm['movies'][0]['characters'][1]);
97
        $this->assertEquals(count($vm['movies'][0]['characters']), 1);
98
99
        $vm['movies'][0]['characters']->add('Sam');
100
        $this->assertEquals($vm['movies'][0]['characters']->toArray(), array(
101
            'Frodon',
102
            'Sam',
103
        ));
104
105
        $m = $vm->getMovies();
106
        $this->assertEquals($m[0]->getProperties()->toArray(), array(
107
            'language' => 'english',
108
            'budget' => '$93 million',
109
            'boxOffice' => '$871.5 million',
110
        ));
111
112
        $this->assertEquals($m[0]->getChapter(), null);
113
114
        $vm->addMovie(array(
115
            'title' => "Incption",
116
            'resume' => "Dominick \"Dom\" Cobb (Leonardo DiCaprio) and Arthur (Joseph Gordon-Levitt) are \"extractors\", people who perform corporate espionage using an experimental military technology to infiltrate the subconscious of their targets and extract information while experiencing shared dreaming. Their latest target is Japanese businessman Saito (Ken Watanabe). The extraction from Saito fails when sabotaged by a memory of Cobb's deceased wife Mal (Marion Cotillard). Saito reveals that he was actually auditioning the team to perform the difficult act of \"inception\": planting an idea in a person's subconscious.",
117
        ));
118
119
        $vm['movies'][1]['title'] = 'Inception';
120
121
        $this->assertEquals($m[1]->toArray(), array(
122
            'title' => "Inception",
123
            'resume' => "Dominick \"Dom\" Cobb (Leonardo DiCaprio) and Arthur (Joseph Gordon-Levitt) are \"extractors\", people who perform corporate espionage using an experimental military technology to infiltrate the subconscious of their targets and extract information while experiencing shared dreaming. Their latest target is Japanese businessman Saito (Ken Watanabe). The extraction from Saito fails when sabotaged by a memory of Cobb's deceased wife Mal (Marion Cotillard). Saito reveals that he was actually auditioning the team to perform the difficult act of \"inception\": planting an idea in a person's subconscious.",
124
        ));
125
    }
126
127
    /**
128
     * @expectedException        BadMethodCallException
129
     * @expectedExceptionMessage Error while parsing method name 'fail()'
130
     */
131
    public function testParsingMethodException()
132
    {
133
        $vm = new ViewModelNode();
134
        $vm->fail();
135
    }
136
137
    /**
138
     * @expectedException        BadMethodCallException
139
     * @expectedExceptionMessage removeMovie(): Undefined method
140
     */
141
    public function testUndefinedMethod()
142
    {
143
        $vm = new ViewModelNode();
144
        $vm->removeMovie();
145
    }
146
}
147