| Conditions | 1 |
| Paths | 1 |
| Total Lines | 96 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 90 | public function testCreateService() |
||
| 91 | { |
||
| 92 | |||
| 93 | $user = new User(); |
||
| 94 | $user->setId('testUser'); |
||
| 95 | $user->setEmail('test@user'); |
||
| 96 | |||
| 97 | |||
| 98 | $options = array( |
||
| 99 | 'token' => 'testToken', |
||
| 100 | 'user' => $user, |
||
| 101 | 'template' => 'testTemplate', |
||
| 102 | ); |
||
| 103 | |||
| 104 | $ownerOrg = new Organization(); |
||
| 105 | $ownerOrg->setId('ownerOrg'); |
||
| 106 | $ownerOrgName = new OrganizationName('TestOwnerOrg'); |
||
| 107 | $ownerOrg->setOrganizationName($ownerOrgName); |
||
| 108 | |||
| 109 | $userOrg = new Organization(); |
||
| 110 | $userOrg->setId('userOrg'); |
||
| 111 | $userOrgName = new OrganizationName('TestUserOrg'); |
||
| 112 | $userOrg->setOrganizationName($userOrgName); |
||
| 113 | |||
| 114 | |||
| 115 | $orgRep = $this->getMockBuilder('\Organizations\Repository\Organization')->disableOriginalConstructor()->getMock(); |
||
| 116 | $orgRep->expects($this->exactly(2)) |
||
| 117 | ->method('findByUser') |
||
| 118 | ->withConsecutive(array('testOwner'), array('testUser')) |
||
| 119 | ->will($this->onConsecutiveCalls($ownerOrg, $userOrg)); |
||
| 120 | |||
| 121 | |||
| 122 | $ownerOrgRef = new OrganizationReference('testOwner', $orgRep); |
||
| 123 | $userOrgRef = new OrganizationReference('testUser', $orgRep); |
||
| 124 | |||
| 125 | $user->setOrganization($userOrgRef); |
||
| 126 | |||
| 127 | $owner = new User(); |
||
| 128 | $owner->getInfo()->setFirstName('Test')->setLastName('Owner'); |
||
| 129 | $owner->setOrganization($ownerOrgRef); |
||
| 130 | |||
| 131 | $authService = $this->getMockBuilder('\Auth\AuthenticationService')->disableOriginalConstructor()->getMock(); |
||
| 132 | $authService->expects($this->once())->method('getUser')->willReturn($owner); |
||
| 133 | |||
| 134 | $router = $this->getMockForAbstractClass('\Zend\Mvc\Router\RouteStackInterface'); |
||
| 135 | $router->expects($this->once()) |
||
| 136 | ->method('assemble') |
||
| 137 | ->with(array('action' => 'accept'), |
||
| 138 | array('name' => 'lang/organizations/invite', |
||
| 139 | 'query' => array('token' => $options['token'], 'organization' => $ownerOrg->getId())) |
||
| 140 | ) |
||
| 141 | ->willReturn('testUrl'); |
||
| 142 | |||
| 143 | $services = $this->getMockBuilder('\Zend\ServiceManager\ServiceManager')->disableOriginalConstructor()->getMock(); |
||
| 144 | |||
| 145 | $services->expects($this->exactly(2)) |
||
| 146 | ->method('get') |
||
| 147 | ->withConsecutive( |
||
| 148 | array('AuthenticationService'), |
||
| 149 | array('Router') |
||
| 150 | )->will($this->onConsecutiveCalls($authService, $router)); |
||
| 151 | |||
| 152 | $mailService = $this->getMockBuilder('\Core\Mail\MailService')->disableOriginalConstructor()->getMock(); |
||
| 153 | |||
| 154 | $mailService->expects($this->once())->method('getServiceLocator')->willReturn($services); |
||
| 155 | |||
| 156 | $mailMock = new HTMLTemplateMessage(); |
||
| 157 | $translator = $this->getMockBuilder('\Zend\I18n\Translator\Translator')->disableOriginalConstructor()->getMock(); |
||
| 158 | $translator->expects($this->any())->method('translate')->will($this->returnArgument(0)); |
||
| 159 | $mailMock->setTranslator($translator); |
||
| 160 | $mailService->expects($this->once())->method('get')->with('htmltemplate') |
||
| 161 | ->willReturn($mailMock); |
||
| 162 | |||
| 163 | |||
| 164 | $target = new EmployeeInvitationFactory(); |
||
| 165 | $target->setCreationOptions($options); |
||
| 166 | $mail = $target->createService($mailService); |
||
| 167 | |||
| 168 | |||
| 169 | $vars = $mail->getVariables()->getArrayCopy(); |
||
| 170 | |||
| 171 | $expected = Array( |
||
| 172 | 'inviter' => 'Test Owner', |
||
| 173 | 'organization' => 'TestOwnerOrg', |
||
| 174 | 'token' => $options['token'], |
||
| 175 | 'user' => '', |
||
| 176 | 'hasAssociation' => true, |
||
| 177 | 'url' => 'testUrl', |
||
| 178 | 'isOwner' => true, |
||
| 179 | 'currentOrganization' => 'TestUserOrg', |
||
| 180 | 'template' => 'testTemplate', |
||
| 181 | ); |
||
| 182 | |||
| 183 | $this->assertEquals($expected, $vars); |
||
| 184 | $this->assertEquals($user->getEmail(), $mail->getTo()->current()->getEmail()); |
||
| 185 | } |
||
| 186 | } |