testBuildCallsTheBuilderModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 9.7251
c 0
b 0
f 0
cc 1
eloc 35
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
/*
4
 * This file is part of the MilioooMessageBundle package.
5
 *
6
 * (c) Michiel boeckaert <[email protected]>
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Miliooo\Messaging\Tests\Builder\Message;
12
13
use Miliooo\Messaging\Builder\Message\NewThreadBuilder;
14
use Miliooo\Messaging\Builder\Model\ThreadBuilderModel;
15
use Miliooo\Messaging\Form\FormModel\NewThreadSingleRecipient;
16
use Miliooo\Messaging\TestHelpers\ParticipantTestHelper;
17
18
/**
19
 *
20
 * This test file is stupidly complex because we want to test
21
 * mock expectations for multiple calls to a function
22
 *
23
 * @link http://www.kreamer.org/phpunit-cookbook/1.0/mocks/set-mock-expectations-for-multiple-calls-to-a-function
24
 *
25
 * @author Michiel Boeckaert <[email protected]>
26
 */
27
class ThreadBuilderModelTest extends \PHPUnit_Framework_TestCase
28
{
29
    /**
30
     * The class under test.
31
     *
32
     * @var NewThreadBuilder
33
     */
34
    private $builder;
35
36
    public function setUp()
37
    {
38
        $this->builder = new NewThreadBuilder();
39
        $this->builder->setMessageClass('Miliooo\Messaging\TestHelpers\Model\Message');
40
        $this->builder->setThreadClass('Miliooo\Messaging\TestHelpers\Model\Thread');
41
        $this->builder->setMessageMetaClass('Miliooo\Messaging\TestHelpers\Model\MessageMeta');
42
        $this->builder->setThreadMetaClass('Miliooo\Messaging\TestHelpers\Model\ThreadMeta');
43
    }
44
45
    public function testBuildReturnsThreadObject()
46
    {
47
        $sender = new ParticipantTestHelper(1);
48
        $recipient = new ParticipantTestHelper(2);
49
50
        $newThreadModel = new NewThreadSingleRecipient();
51
        $newThreadModel->setSender($sender);
52
        $newThreadModel->setRecipient($recipient);
53
        $newThreadModel->setSubject('subject');
54
        $newThreadModel->setBody('body');
55
        $newThreadModel->setCreatedAt(new \DateTime('2013-10-10 00:00:00'));
56
57
        $builderModel = new ThreadBuilderModel($newThreadModel);
58
        $build = $this->builder->build($builderModel);
59
60
        $this->assertInstanceOf('Miliooo\Messaging\Model\ThreadInterface', $build);
61
    }
62
63
    public function testBuildCallsTheBuilderModel()
64
    {
65
        $sender = new ParticipantTestHelper(1);
66
        $recipient = new ParticipantTestHelper(2);
67
68
        $builderModel = $this->getMockBuilder('Miliooo\Messaging\Builder\Model\ThreadBuilderModel')
69
            ->disableOriginalConstructor()
70
            ->getMock();
71
72
        $builderModel->expects($this->at(0))->method('getSender')
73
            ->will($this->returnValue($sender));
74
75
        $builderModel->expects($this->at(1))->method('getRecipients')
76
            ->will($this->returnValue([$recipient]));
77
78
        $builderModel->expects($this->at(2))->method('getThreadData')
79
            ->with(null);
80
81
        $builderModel->expects($this->at(3))->method('getThreadMeta')
82
            ->with('all');
83
84
        $builderModel->expects($this->at(4))->method('getThreadMeta')
85
            ->with('sender');
86
87
        //the recipient update updates first the values that are happening for all...
88
        $builderModel->expects($this->at(5))->method('getThreadMeta')
89
            ->with('all');
90
91
        $builderModel->expects($this->at(6))->method('getThreadMeta')
92
            ->with('recipients');
93
94
        $builderModel->expects($this->at(7))->method('getMessageData')
95
            ->with(null);
96
97
        $builderModel->expects($this->at(8))->method('getMessageMeta')
98
            ->with('all');
99
100
        $builderModel->expects($this->at(9))->method('getMessageMeta')
101
            ->with('sender');
102
103
        $builderModel->expects($this->at(10))->method('getMessageMeta')
104
            ->with('all');
105
106
        $builderModel->expects($this->at(11))->method('getMessageMeta')
107
            ->with('recipients');
108
109
        //2 times for all, one time for sender, one time for recipient
110
        $builderModel->expects($this->exactly(4))->method('getMessageMeta');
111
        //2 times for all, one time for sender, one time for recipient
112
        $builderModel->expects($this->exactly(4))->method('getThreadMeta');
113
114
        $builderModel->expects($this->exactly(1))->method('getThreadData');
115
        $builderModel->expects($this->exactly(1))->method('getMessageData');
116
117
        $this->builder->build($builderModel);
118
    }
119
120
    /**
121
     * @expectedException \InvalidArgumentException
122
     * @expectedExceptionMessage could not find setter for foo
123
     */
124
    public function testBuilderThrowsExceptionWhenNotAbleToCreateSetter()
125
    {
126
        $builderModel = $this->getMockBuilder('Miliooo\Messaging\Builder\Model\ThreadBuilderModel')
127
            ->disableOriginalConstructor()
128
            ->getMock();
129
130
        $builderModel->expects($this->atLeastOnce())->method('getThreadData')
131
            ->will($this->returnValue(['foo' => 'bar']));
132
        $this->builder->build($builderModel);
133
    }
134
135
    /**
136
     * @expectedException \InvalidArgumentException
137
     * @expectedExceptionMessage could not create setter method, no string given
138
     */
139
    public function testBuilderThrowsExceptionWhenSetterKeyNotString()
140
    {
141
        $builderModel = $this->getMockBuilder('Miliooo\Messaging\Builder\Model\ThreadBuilderModel')
142
            ->disableOriginalConstructor()
143
            ->getMock();
144
145
        $builderModel->expects($this->atLeastOnce())->method('getThreadData')
146
            ->will($this->returnValue([['foo' => 'bar'], 'bar']));
147
148
        $this->builder->build($builderModel);
149
    }
150
}
151