Complex classes like NfgTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NfgTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class NfgTest extends \PHPUnit_Framework_TestCase |
||
| 34 | { |
||
| 35 | private function getBreaker($exception = null) |
||
| 36 | { |
||
| 37 | $breaker = $this->getMockBuilder('Eljam\CircuitBreaker\Breaker') |
||
| 38 | ->disableOriginalConstructor() |
||
| 39 | ->getMock(); |
||
| 40 | |||
| 41 | if ($exception instanceof \Exception) { |
||
| 42 | $breaker->expects($this->once())->method('protect') |
||
| 43 | ->willThrowException($exception); |
||
| 44 | } else { |
||
| 45 | $breaker->expects($this->once())->method('protect') |
||
| 46 | ->willReturnCallback(function (\Closure $closure) { |
||
| 47 | return $closure(); |
||
| 48 | }); |
||
| 49 | } |
||
| 50 | |||
| 51 | return $breaker; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function testLoginRedirectUnavailableAccessId() |
||
| 55 | { |
||
| 56 | $soapService = $this->getMock('\PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoapInterface'); |
||
| 57 | $soapService->expects($this->once())->method('getAccessID') |
||
| 58 | ->willThrowException(new NfgServiceUnavailableException()); |
||
| 59 | |||
| 60 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 61 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 62 | |||
| 63 | $circuitBreaker = $this->getBreaker(); |
||
| 64 | |||
| 65 | /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject $logger */ |
||
| 66 | $logger = $this->getMock('Psr\Log\LoggerInterface'); |
||
| 67 | $logger->expects($this->once())->method('error'); |
||
| 68 | |||
| 69 | $nfg = $this->getNfgService( |
||
| 70 | [ |
||
| 71 | 'session' => $this->getSession($accessId), |
||
| 72 | 'soap' => $soapService, |
||
| 73 | ] |
||
| 74 | ); |
||
| 75 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 76 | $nfg->setLogger($logger); |
||
| 77 | $nfg->login(); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testLoginRedirectUnavailableUnknownError() |
||
| 81 | { |
||
| 82 | $soapService = $this->getMock('\PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoapInterface'); |
||
| 83 | $soapService->expects($this->once())->method('getAccessID') |
||
| 84 | ->willThrowException(new \RuntimeException()); |
||
| 85 | |||
| 86 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 87 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 88 | |||
| 89 | $circuitBreaker = $this->getBreaker(); |
||
| 90 | |||
| 91 | $nfg = $this->getNfgService( |
||
| 92 | [ |
||
| 93 | 'session' => $this->getSession($accessId), |
||
| 94 | 'soap' => $soapService, |
||
| 95 | ] |
||
| 96 | ); |
||
| 97 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 98 | $nfg->login(); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function testLoginRedirectUnavailable() |
||
| 102 | { |
||
| 103 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 104 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 105 | |||
| 106 | $circuitBreaker = $this->getBreaker(new NfgServiceUnavailableException()); |
||
| 107 | |||
| 108 | $nfg = $this->getNfgService( |
||
| 109 | [ |
||
| 110 | 'session' => $this->getSession($accessId), |
||
| 111 | 'soap' => $this->getSoapService($accessId), |
||
| 112 | ] |
||
| 113 | ); |
||
| 114 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 115 | $nfg->login(); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testConnectRedirectUnavailable() |
||
| 119 | { |
||
| 120 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 121 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 122 | |||
| 123 | $circuitBreaker = $this->getBreaker(new NfgServiceUnavailableException()); |
||
| 124 | |||
| 125 | $nfg = $this->getNfgService( |
||
| 126 | [ |
||
| 127 | 'session' => $this->getSession($accessId), |
||
| 128 | 'soap' => $this->getSoapService($accessId), |
||
| 129 | ] |
||
| 130 | ); |
||
| 131 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 132 | $nfg->connect(); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function testLoginRedirect() |
||
| 136 | { |
||
| 137 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 138 | |||
| 139 | $circuitBreaker = $this->getBreaker(); |
||
| 140 | |||
| 141 | /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject $logger */ |
||
| 142 | $logger = $this->getMock('Psr\Log\LoggerInterface'); |
||
| 143 | $logger->expects($this->once())->method('info'); |
||
| 144 | |||
| 145 | $nfg = $this->getNfgService( |
||
| 146 | [ |
||
| 147 | 'session' => $this->getSession($accessId, 'set'), |
||
| 148 | 'soap' => $this->getSoapService($accessId), |
||
| 149 | ] |
||
| 150 | ); |
||
| 151 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 152 | $nfg->setLogger($logger); |
||
| 153 | |||
| 154 | $response = $nfg->login(); |
||
| 155 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
||
| 156 | $this->assertContains($accessId, $response->getContent()); |
||
| 157 | $this->assertContains('nfg_login_callback', $response->getContent()); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function testLoginCallback() |
||
| 161 | { |
||
| 162 | $cpf = '12345678901'; |
||
| 163 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 164 | $secret = "my very super secret secret"; |
||
| 165 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 166 | |||
| 167 | $person = new Person(); |
||
| 168 | $person->setCpf($cpf); |
||
| 169 | $personMeuRS = new PersonMeuRS(); |
||
| 170 | $personMeuRS->setPerson($person) |
||
| 171 | ->setNfgAccessToken('dummy'); |
||
| 172 | $meuRSHelper = $this->getMeuRSHelper($cpf, $personMeuRS); |
||
| 173 | |||
| 174 | $nfg = $this->getNfgService( |
||
| 175 | [ |
||
| 176 | 'session' => $this->getSession($accessId, 'get'), |
||
| 177 | 'soap' => $this->getSoapService($accessId), |
||
| 178 | 'meurs_helper' => $meuRSHelper, |
||
| 179 | 'login_manager' => $this->getLoginManager(true), |
||
| 180 | ] |
||
| 181 | ); |
||
| 182 | |||
| 183 | /** @var RedirectResponse $response */ |
||
| 184 | $response = $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 185 | |||
| 186 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 187 | $this->assertEquals('lc_home', $response->getTargetUrl()); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function testLoginCallbackMissingParams() |
||
| 191 | { |
||
| 192 | $this->setExpectedException('Symfony\Component\HttpKernel\Exception\BadRequestHttpException'); |
||
| 193 | $cpf = '12345678901'; |
||
| 194 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 195 | $secret = "my very super secret secret"; |
||
| 196 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 197 | |||
| 198 | $nfg = $this->getNfgService( |
||
| 199 | [ |
||
| 200 | 'session' => $this->getSession($accessId, 'none'), |
||
| 201 | 'soap' => $this->getSoapService($accessId), |
||
| 202 | ] |
||
| 203 | ); |
||
| 204 | |||
| 205 | $nfg->loginCallback(compact('accessId', 'prsec'), $secret); |
||
| 206 | } |
||
| 207 | |||
| 208 | public function testLoginCallbackInvalidSignature() |
||
| 209 | { |
||
| 210 | $this->setExpectedException('Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException'); |
||
| 211 | $cpf = '12345678901'; |
||
| 212 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 213 | $secret = "my very super secret secret"; |
||
| 214 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret).'_INVALID'; |
||
| 215 | |||
| 216 | $nfg = $this->getNfgService( |
||
| 217 | [ |
||
| 218 | 'session' => $this->getSession($accessId, 'none'), |
||
| 219 | 'soap' => $this->getSoapService($accessId), |
||
| 220 | ] |
||
| 221 | ); |
||
| 222 | |||
| 223 | $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function testLoginCallbackInvalidAccessId() |
||
| 227 | { |
||
| 228 | $this->setExpectedException('Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException'); |
||
| 229 | $cpf = '12345678901'; |
||
| 230 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 231 | $secret = "my very super secret secret"; |
||
| 232 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 233 | |||
| 234 | $nfg = $this->getNfgService( |
||
| 235 | [ |
||
| 236 | 'session' => $this->getSession($accessId.'_INVALID', 'none'), |
||
| 237 | 'soap' => $this->getSoapService($accessId), |
||
| 238 | ] |
||
| 239 | ); |
||
| 240 | |||
| 241 | $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 242 | } |
||
| 243 | |||
| 244 | public function testLoginCallbackInactiveUser() |
||
| 245 | { |
||
| 246 | $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccountStatusException'); |
||
| 247 | |||
| 248 | $cpf = '12345678901'; |
||
| 249 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 250 | $secret = "my very super secret secret"; |
||
| 251 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 252 | |||
| 253 | $person = new Person(); |
||
| 254 | $person->setCpf($cpf); |
||
| 255 | $personMeuRS = new PersonMeuRS(); |
||
| 256 | $personMeuRS->setPerson($person) |
||
| 257 | ->setNfgAccessToken('dummy'); |
||
| 258 | $meuRSHelper = $this->getMeuRSHelper($cpf, $personMeuRS); |
||
| 259 | |||
| 260 | $loginManager = $this->getLoginManager(); |
||
| 261 | $loginManager->method('logInUser')->willThrowException(new AccountExpiredException()); |
||
| 262 | |||
| 263 | $nfg = $this->getNfgService( |
||
| 264 | [ |
||
| 265 | 'session' => $this->getSession($accessId, 'get'), |
||
| 266 | 'soap' => $this->getSoapService($accessId), |
||
| 267 | 'login_manager' => $loginManager, |
||
| 268 | 'meurs_helper' => $meuRSHelper, |
||
| 269 | ] |
||
| 270 | ); |
||
| 271 | |||
| 272 | $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 273 | } |
||
| 274 | |||
| 275 | public function testLoginNonexistentUser() |
||
| 276 | { |
||
| 277 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\ConnectionNotFoundException'); |
||
| 278 | |||
| 279 | $cpf = '12345678901'; |
||
| 280 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 281 | $secret = "my very super secret secret"; |
||
| 282 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 283 | |||
| 284 | $person = new Person(); |
||
| 285 | $person->setCpf($cpf); |
||
| 286 | $meuRSHelper = $this->getMeuRSHelper($cpf, null); |
||
| 287 | |||
| 288 | $nfg = $this->getNfgService( |
||
| 289 | [ |
||
| 290 | 'session' => $this->getSession($accessId, 'get'), |
||
| 291 | 'soap' => $this->getSoapService($accessId), |
||
| 292 | 'meurs_helper' => $meuRSHelper, |
||
| 293 | 'login_manager' => $this->getLoginManager(false), |
||
| 294 | ] |
||
| 295 | ); |
||
| 296 | |||
| 297 | $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 298 | } |
||
| 299 | |||
| 300 | public function testPreUserInfoUnavailable() |
||
| 301 | { |
||
| 302 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 303 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 304 | $soapService = $this->getSoapService($accessId); |
||
| 305 | |||
| 306 | $cpf = '01234567890'; |
||
| 307 | $person = new Person(); |
||
| 308 | $person->setCpf($cpf); |
||
| 309 | $personMeuRS = new PersonMeuRS(); |
||
| 310 | $personMeuRS |
||
| 311 | ->setVoterRegistration('1234567890') |
||
| 312 | ->setPerson($person); |
||
| 313 | |||
| 314 | $circuitBreaker = $this->getBreaker(new NfgServiceUnavailableException()); |
||
| 315 | |||
| 316 | $nfg = $this->getNfgService( |
||
| 317 | [ |
||
| 318 | 'session' => $this->getSession($accessId, 'none'), |
||
| 319 | 'soap' => $soapService, |
||
| 320 | ] |
||
| 321 | ); |
||
| 322 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 323 | |||
| 324 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 325 | $request = $this->getRequest($accessToken); |
||
| 326 | |||
| 327 | $nfg->connectCallback($request, $personMeuRS); |
||
| 328 | } |
||
| 329 | |||
| 330 | public function testConnectCallbackMissingAccessToken() |
||
| 331 | { |
||
| 332 | $this->setExpectedException('Symfony\Component\HttpKernel\Exception\BadRequestHttpException'); |
||
| 333 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 334 | $soapService = $this->getSoapService($accessId); |
||
| 335 | |||
| 336 | $cpf = '01234567890'; |
||
| 337 | $person = new Person(); |
||
| 338 | $person->setCpf($cpf); |
||
| 339 | $personMeuRS = new PersonMeuRS(); |
||
| 340 | $personMeuRS |
||
| 341 | ->setVoterRegistration('1234567890') |
||
| 342 | ->setPerson($person); |
||
| 343 | |||
| 344 | $nfg = $this->getNfgService( |
||
| 345 | [ |
||
| 346 | 'session' => $this->getSession($accessId, 'none'), |
||
| 347 | 'soap' => $soapService, |
||
| 348 | ] |
||
| 349 | ); |
||
| 350 | |||
| 351 | $request = $this->getRequest(null); |
||
| 352 | |||
| 353 | $nfg->connectCallback($request, $personMeuRS); |
||
| 354 | } |
||
| 355 | |||
| 356 | public function testUserInfoUnavailable() |
||
| 357 | { |
||
| 358 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 359 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 360 | |||
| 361 | $soapService = $this->getMock('\PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoapInterface'); |
||
| 362 | $soapService->expects($this->once())->method('getUserInfo') |
||
| 363 | ->willThrowException(new NfgServiceUnavailableException()); |
||
| 364 | |||
| 365 | $cpf = '01234567890'; |
||
| 366 | $person = new Person(); |
||
| 367 | $person->setCpf($cpf); |
||
| 368 | $personMeuRS = new PersonMeuRS(); |
||
| 369 | $personMeuRS |
||
| 370 | ->setVoterRegistration('1234567890') |
||
| 371 | ->setPerson($person); |
||
| 372 | |||
| 373 | $circuitBreaker = $this->getBreaker(); |
||
| 374 | |||
| 375 | $nfg = $this->getNfgService( |
||
| 376 | [ |
||
| 377 | 'session' => $this->getSession($accessId, 'none'), |
||
| 378 | 'soap' => $soapService, |
||
| 379 | ] |
||
| 380 | ); |
||
| 381 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 382 | |||
| 383 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 384 | $request = $this->getRequest($accessToken); |
||
| 385 | |||
| 386 | $nfg->connectCallback($request, $personMeuRS); |
||
| 387 | } |
||
| 388 | |||
| 389 | public function testUserInfoError() |
||
| 390 | { |
||
| 391 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 392 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 393 | |||
| 394 | $soapService = $this->getMock('\PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoapInterface'); |
||
| 395 | $soapService->expects($this->once())->method('getUserInfo') |
||
| 396 | ->willThrowException(new \RuntimeException()); |
||
| 397 | |||
| 398 | $cpf = '01234567890'; |
||
| 399 | $person = new Person(); |
||
| 400 | $person->setCpf($cpf); |
||
| 401 | $personMeuRS = new PersonMeuRS(); |
||
| 402 | $personMeuRS |
||
| 403 | ->setVoterRegistration('1234567890') |
||
| 404 | ->setPerson($person); |
||
| 405 | |||
| 406 | $circuitBreaker = $this->getBreaker(); |
||
| 407 | |||
| 408 | $nfg = $this->getNfgService( |
||
| 409 | [ |
||
| 410 | 'session' => $this->getSession($accessId, 'none'), |
||
| 411 | 'soap' => $soapService, |
||
| 412 | ] |
||
| 413 | ); |
||
| 414 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 415 | |||
| 416 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 417 | $request = $this->getRequest($accessToken); |
||
| 418 | |||
| 419 | $nfg->connectCallback($request, $personMeuRS); |
||
| 420 | } |
||
| 421 | |||
| 422 | public function testIncompleteInfo() |
||
| 423 | { |
||
| 424 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\MissingRequiredInformationException'); |
||
| 425 | |||
| 426 | $nfgProfile = $this->getNfgProfile(); |
||
| 427 | $nfgProfile->setCpf(null) |
||
| 428 | ->setEmail(null) |
||
| 429 | ->setName(null) |
||
| 430 | ->setBirthdate(null) |
||
| 431 | ->setMobile(null); |
||
| 432 | |||
| 433 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 434 | $soapService = $this->getSoapService($accessId); |
||
| 435 | |||
| 436 | $cpf = '01234567890'; |
||
| 437 | $person = new Person(); |
||
| 438 | $person->setCpf($cpf); |
||
| 439 | $personMeuRS = new PersonMeuRS(); |
||
| 440 | $personMeuRS |
||
| 441 | ->setVoterRegistration('1234567890') |
||
| 442 | ->setPerson($person); |
||
| 443 | |||
| 444 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 445 | |||
| 446 | $nfg = $this->getNfgService( |
||
| 447 | [ |
||
| 448 | 'session' => $this->getSession($accessId, 'none'), |
||
| 449 | 'soap' => $soapService, |
||
| 450 | ] |
||
| 451 | ); |
||
| 452 | |||
| 453 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 454 | $request = $this->getRequest($accessToken); |
||
| 455 | |||
| 456 | $nfg->connectCallback($request, $personMeuRS); |
||
| 457 | } |
||
| 458 | |||
| 459 | public function testMinimalInfo() |
||
| 460 | { |
||
| 461 | $cpf = '01234567890'; |
||
| 462 | $nfgProfile = $this->getNfgProfile(); |
||
| 463 | $nfgProfile->setCpf($cpf) |
||
| 464 | ->setEmail('[email protected]') |
||
| 465 | ->setName('Fulano') |
||
| 466 | ->setBirthdate(null) |
||
| 467 | ->setMobile(null); |
||
| 468 | |||
| 469 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 470 | $soapService = $this->getSoapService($accessId); |
||
| 471 | |||
| 472 | $person = new Person(); |
||
| 473 | $person->setCpf($cpf); |
||
| 474 | $personMeuRS = new PersonMeuRS(); |
||
| 475 | $personMeuRS |
||
| 476 | ->setVoterRegistration('1234567890') |
||
| 477 | ->setPerson($person); |
||
| 478 | |||
| 479 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 480 | |||
| 481 | $nfg = $this->getNfgService( |
||
| 482 | [ |
||
| 483 | 'session' => $this->getSession($accessId, 'none'), |
||
| 484 | 'soap' => $soapService, |
||
| 485 | ] |
||
| 486 | ); |
||
| 487 | |||
| 488 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 489 | $request = $this->getRequest($accessToken); |
||
| 490 | |||
| 491 | $nfg->connectCallback($request, $personMeuRS); |
||
| 492 | } |
||
| 493 | |||
| 494 | public function testRegistration() |
||
| 495 | { |
||
| 496 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 497 | $soapService = $this->getSoapService($accessId); |
||
| 498 | |||
| 499 | $nfgProfile = $this->getNfgProfile(); |
||
| 500 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 501 | |||
| 502 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), null); |
||
| 503 | |||
| 504 | $dispatcher = $this->getDispatcher(); |
||
| 505 | $dispatcher->expects($this->atLeastOnce())->method('dispatch') |
||
| 506 | ->willReturnCallback(function ($eventName, $event) { |
||
| 507 | if ($eventName === FOSUserEvents::REGISTRATION_INITIALIZE |
||
| 508 | && $event instanceof GetResponseUserEvent |
||
| 509 | ) { |
||
| 510 | $event->setResponse(new RedirectResponse('dummy')); |
||
| 511 | } |
||
| 512 | }); |
||
| 513 | |||
| 514 | $nfg = $this->getNfgService( |
||
| 515 | [ |
||
| 516 | 'session' => $this->getSession($accessId, 'none'), |
||
| 517 | 'soap' => $soapService, |
||
| 518 | 'meurs_helper' => $meuRSHelper, |
||
| 519 | 'dispatcher' => $dispatcher, |
||
| 520 | ] |
||
| 521 | ); |
||
| 522 | |||
| 523 | $personMeuRS = new PersonMeuRS(); |
||
| 524 | |||
| 525 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 526 | $request = $this->getRequest($accessToken); |
||
| 527 | /** @var RedirectResponse $response */ |
||
| 528 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 529 | |||
| 530 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 531 | $this->assertEquals('dummy', $response->getTargetUrl()); |
||
| 532 | } |
||
| 533 | |||
| 534 | public function testRegistrationWithPreexistingNfgProfile() |
||
| 535 | { |
||
| 536 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 537 | $soapService = $this->getSoapService($accessId); |
||
| 538 | |||
| 539 | $nfgProfile = $this->getNfgProfile(); |
||
| 540 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 541 | |||
| 542 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), null); |
||
| 543 | |||
| 544 | $oldNfgProfile = $this->getNfgProfile(); |
||
| 545 | $oldNfgProfile->setEmail('[email protected]'); |
||
| 546 | $nfgProfileRepo = $this->getNfgProfileRepository(); |
||
| 547 | $nfgProfileRepo->expects($this->atLeastOnce()) |
||
| 548 | ->method('findByCpf')->with($nfgProfile->getCpf())->willReturn($oldNfgProfile); |
||
| 549 | |||
| 550 | $nfg = $this->getNfgService( |
||
| 551 | [ |
||
| 552 | 'session' => $this->getSession($accessId, 'none'), |
||
| 553 | 'soap' => $soapService, |
||
| 554 | 'meurs_helper' => $meuRSHelper, |
||
| 555 | 'nfgprofile_repository' => $nfgProfileRepo, |
||
| 556 | ] |
||
| 557 | ); |
||
| 558 | |||
| 559 | $personMeuRS = new PersonMeuRS(); |
||
| 560 | |||
| 561 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 562 | $request = $this->getRequest($accessToken); |
||
| 563 | /** @var RedirectResponse $response */ |
||
| 564 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 565 | |||
| 566 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 567 | $this->assertEquals('fos_user_registration_confirmed', $response->getTargetUrl()); |
||
| 568 | |||
| 569 | $this->assertEquals($nfgProfile->getEmail(), $oldNfgProfile->getEmail()); |
||
| 570 | } |
||
| 571 | |||
| 572 | public function testRegistrationCpfCollision() |
||
| 573 | { |
||
| 574 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\CpfInUseException'); |
||
| 575 | |||
| 576 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 577 | $soapService = $this->getSoapService($accessId); |
||
| 578 | |||
| 579 | $nfgProfile = $this->getNfgProfile(); |
||
| 580 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 581 | |||
| 582 | $otherPerson = new Person(); |
||
| 583 | $otherPerson->setCpf($nfgProfile->getCpf()); |
||
| 584 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 585 | $otherPersonMeuRS->setPerson($otherPerson); |
||
| 586 | $otherPersonMeuRS->setNfgAccessToken('token'); |
||
| 587 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), $otherPersonMeuRS); |
||
| 588 | |||
| 589 | $nfg = $this->getNfgService( |
||
| 590 | [ |
||
| 591 | 'session' => $this->getSession($accessId, 'none'), |
||
| 592 | 'soap' => $soapService, |
||
| 593 | 'meurs_helper' => $meuRSHelper, |
||
| 594 | ] |
||
| 595 | ); |
||
| 596 | |||
| 597 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 598 | $request = $this->getRequest($accessToken); |
||
| 599 | $nfg->connectCallback($request, new PersonMeuRS()); |
||
| 600 | } |
||
| 601 | |||
| 602 | public function testRegistrationTurnsIntoLogin() |
||
| 603 | { |
||
| 604 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 605 | $soapService = $this->getSoapService($accessId); |
||
| 606 | |||
| 607 | $nfgProfile = $this->getNfgProfile(); |
||
| 608 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 609 | |||
| 610 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 611 | $otherPersonMeuRS |
||
| 612 | ->setNfgProfile($nfgProfile) |
||
| 613 | ->setNfgAccessToken('access_token') |
||
| 614 | ->setPerson(new Person()); |
||
| 615 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), $otherPersonMeuRS); |
||
| 616 | |||
| 617 | $nfg = $this->getNfgService( |
||
| 618 | [ |
||
| 619 | 'session' => $this->getSession($accessId, 'none'), |
||
| 620 | 'soap' => $soapService, |
||
| 621 | 'meurs_helper' => $meuRSHelper, |
||
| 622 | 'login_manager' => $this->getLoginManager(true), |
||
| 623 | ] |
||
| 624 | ); |
||
| 625 | |||
| 626 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 627 | $request = $this->getRequest($accessToken); |
||
| 628 | $nfg->connectCallback($request, new PersonMeuRS()); |
||
| 629 | } |
||
| 630 | |||
| 631 | public function testRegistrationEmailCollision() |
||
| 632 | { |
||
| 633 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\EmailInUseException'); |
||
| 634 | |||
| 635 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 636 | $soapService = $this->getSoapService($accessId); |
||
| 637 | |||
| 638 | $nfgProfile = $this->getNfgProfile(); |
||
| 639 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 640 | |||
| 641 | $meuRSHelper = $this->getMeuRSHelper(); |
||
| 642 | $meuRSHelper->expects($this->atLeastOnce())->method('getPersonByEmail')->willReturn(new Person()); |
||
| 643 | |||
| 644 | $nfg = $this->getNfgService( |
||
| 645 | [ |
||
| 646 | 'session' => $this->getSession($accessId, 'none'), |
||
| 647 | 'soap' => $soapService, |
||
| 648 | 'meurs_helper' => $meuRSHelper, |
||
| 649 | ] |
||
| 650 | ); |
||
| 651 | |||
| 652 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 653 | $request = $this->getRequest($accessToken); |
||
| 654 | $nfg->connectCallback($request, new PersonMeuRS()); |
||
| 655 | } |
||
| 656 | |||
| 657 | public function testLevel1Registration() |
||
| 658 | { |
||
| 659 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 660 | $soapService = $this->getSoapService($accessId); |
||
| 661 | |||
| 662 | $nfgProfile = $this->getNfgProfile(); |
||
| 663 | $nfgProfile->setAccessLvl(1); |
||
| 664 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 665 | |||
| 666 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), null); |
||
| 667 | |||
| 668 | $nfg = $this->getNfgService( |
||
| 669 | [ |
||
| 670 | 'session' => $this->getSession($accessId, 'none'), |
||
| 671 | 'soap' => $soapService, |
||
| 672 | 'meurs_helper' => $meuRSHelper, |
||
| 673 | ] |
||
| 674 | ); |
||
| 675 | |||
| 676 | $personMeuRS = new PersonMeuRS(); |
||
| 677 | |||
| 678 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 679 | $request = $this->getRequest($accessToken); |
||
| 680 | /** @var RedirectResponse $response */ |
||
| 681 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 682 | |||
| 683 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 684 | $this->assertEquals('fos_user_registration_confirmed', $response->getTargetUrl()); |
||
| 685 | |||
| 686 | // Assert that the CPF was moved to $person |
||
| 687 | $this->assertNotNull($personMeuRS->getNfgAccessToken()); |
||
| 688 | $this->assertNotNull($personMeuRS->getNfgProfile()); |
||
| 689 | } |
||
| 690 | |||
| 691 | /** |
||
| 692 | * This tests a user with CPF filled and with no CPF collision |
||
| 693 | */ |
||
| 694 | public function testConnectCallbackWithCpf() |
||
| 695 | { |
||
| 696 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 697 | $soapService = $this->getSoapService($accessId); |
||
| 698 | |||
| 699 | $cpf = '01234567890'; |
||
| 700 | $person = new Person(); |
||
| 701 | $person->setCpf($cpf); |
||
| 702 | $personMeuRS = new PersonMeuRS(); |
||
| 703 | $personMeuRS |
||
| 704 | ->setVoterRegistration('1234567890') |
||
| 705 | ->setPerson($person); |
||
| 706 | |||
| 707 | $nfgProfile = $this->getNfgProfile($personMeuRS->getVoterRegistration()); |
||
| 708 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 709 | |||
| 710 | $nfg = $this->getNfgService( |
||
| 711 | [ |
||
| 712 | 'session' => $this->getSession($accessId, 'none'), |
||
| 713 | 'soap' => $soapService, |
||
| 714 | ] |
||
| 715 | ); |
||
| 716 | |||
| 717 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 718 | $request = $this->getRequest($accessToken); |
||
| 719 | /** @var RedirectResponse $response */ |
||
| 720 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 721 | |||
| 722 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 723 | $this->assertEquals('fos_user_profile_edit', $response->getTargetUrl()); |
||
| 724 | |||
| 725 | // Assert that the CPF was moved to $person |
||
| 726 | $this->assertNotNull($personMeuRS->getNfgAccessToken()); |
||
| 727 | } |
||
| 728 | |||
| 729 | /** |
||
| 730 | * This tests a user with CPF filled that does not match NFG's CPF |
||
| 731 | */ |
||
| 732 | public function testConnectCallbackCpfMismatch() |
||
| 733 | { |
||
| 734 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\CpfMismatchException'); |
||
| 735 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 736 | $soapService = $this->getSoapService($accessId); |
||
| 737 | |||
| 738 | $cpf = '01234567891'; |
||
| 739 | $person = new Person(); |
||
| 740 | $person->setCpf($cpf); |
||
| 741 | $personMeuRS = new PersonMeuRS(); |
||
| 742 | $personMeuRS |
||
| 743 | ->setVoterRegistration('1234567890') |
||
| 744 | ->setPerson($person); |
||
| 745 | |||
| 746 | $nfgProfile = $this->getNfgProfile($personMeuRS->getVoterRegistration()); |
||
| 747 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 748 | |||
| 749 | $nfg = $this->getNfgService( |
||
| 750 | [ |
||
| 751 | 'session' => $this->getSession($accessId, 'none'), |
||
| 752 | 'soap' => $soapService, |
||
| 753 | ] |
||
| 754 | ); |
||
| 755 | |||
| 756 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 757 | $request = $this->getRequest($accessToken); |
||
| 758 | $nfg->connectCallback($request, $personMeuRS); |
||
| 759 | } |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Test scenario where the person making the connection does not have a CPF in the profile but there is another |
||
| 763 | * account that uses the user's CPF but without NFG connection. |
||
| 764 | * |
||
| 765 | * The other user's account should have the CPF set to NULL and the current user will be connected to NFG |
||
| 766 | */ |
||
| 767 | public function testConnectCallbackWithoutCpfAndSimpleCollision() |
||
| 768 | { |
||
| 769 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 770 | $soapService = $this->getSoapService($accessId); |
||
| 771 | |||
| 772 | $cpf = '01234567890'; |
||
| 773 | $person = new Person(); |
||
| 774 | $personMeuRS = new PersonMeuRS(); |
||
| 775 | $personMeuRS |
||
| 776 | ->setId(1) |
||
| 777 | ->setVoterRegistration('1234567890') |
||
| 778 | ->setPerson($person); |
||
| 779 | |||
| 780 | $nfgProfile = $this->getNfgProfile($personMeuRS->getVoterRegistration()); |
||
| 781 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 782 | |||
| 783 | $otherPerson = new Person(); |
||
| 784 | $otherPerson->setCpf($cpf); |
||
| 785 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 786 | $otherPersonMeuRS |
||
| 787 | ->setPerson($otherPerson) |
||
| 788 | ->setId(2); |
||
| 789 | $meuRSHelper = $this->getMeuRSHelper($cpf, $otherPersonMeuRS); |
||
| 790 | |||
| 791 | $nfg = $this->getNfgService( |
||
| 792 | [ |
||
| 793 | // flush() and persist() should be called twice since we change both $person and $otherPerson |
||
| 794 | 'em' => $this->getEntityManager(['flush' => $this->exactly(2), 'persist' => $this->exactly(2)]), |
||
| 795 | 'session' => $this->getSession($accessId, 'none'), |
||
| 796 | 'soap' => $soapService, |
||
| 797 | 'meurs_helper' => $meuRSHelper, |
||
| 798 | ] |
||
| 799 | ); |
||
| 800 | |||
| 801 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 802 | $request = $this->getRequest($accessToken); |
||
| 803 | /** @var RedirectResponse $response */ |
||
| 804 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 805 | |||
| 806 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 807 | $this->assertEquals('fos_user_profile_edit', $response->getTargetUrl()); |
||
| 808 | |||
| 809 | // Assert the connection to NFG was made |
||
| 810 | $this->assertNotNull($personMeuRS->getNfgAccessToken()); |
||
| 811 | |||
| 812 | // Assert that the CPF was moved to $person |
||
| 813 | $this->assertNull($otherPerson->getCpf()); |
||
| 814 | $this->assertEquals($cpf, $person->getCpf()); |
||
| 815 | } |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Test scenario where the person making the connection does not have a CPF in the profile but there is another |
||
| 819 | * account that user's CPF. Also, this other account is linked to the same NFG account. |
||
| 820 | */ |
||
| 821 | public function testConnectCallbackWithoutCpfAndCollision() |
||
| 822 | { |
||
| 823 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgAccountCollisionException'); |
||
| 824 | |||
| 825 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 826 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 827 | $soapService = $this->getSoapService($accessId); |
||
| 828 | |||
| 829 | $cpf = '01234567890'; |
||
| 830 | $voterRegistration = '1234567890'; |
||
| 831 | $nfgProfile = $this->getNfgProfile($voterRegistration); |
||
| 832 | $person = new Person(); |
||
| 833 | $personMeuRS = new PersonMeuRS(); |
||
| 834 | $personMeuRS |
||
| 835 | ->setId(1) |
||
| 836 | ->setVoterRegistration($voterRegistration) |
||
| 837 | ->setPerson($person); |
||
| 838 | |||
| 839 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 840 | |||
| 841 | $otherPerson = new Person(); |
||
| 842 | $otherPerson->setCpf($cpf); |
||
| 843 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 844 | $otherPersonMeuRS |
||
| 845 | ->setId(2) |
||
| 846 | ->setNfgAccessToken($accessToken) |
||
| 847 | ->setNfgProfile($nfgProfile) |
||
| 848 | ->setPerson($otherPerson); |
||
| 849 | $meuRSHelper = $this->getMeuRSHelper($cpf, $otherPersonMeuRS); |
||
| 850 | |||
| 851 | $nfg = $this->getNfgService( |
||
| 852 | [ |
||
| 853 | 'session' => $this->getSession($accessId, 'none'), |
||
| 854 | 'soap' => $soapService, |
||
| 855 | 'meurs_helper' => $meuRSHelper, |
||
| 856 | ] |
||
| 857 | ); |
||
| 858 | |||
| 859 | $request = $this->getRequest($accessToken); |
||
| 860 | $nfg->connectCallback($request, $personMeuRS); |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Test scenario where the person making the connection does not have a CPF in the profile but there is another |
||
| 865 | * account that user's CPF. Also, this other account is linked to the same NFG account. |
||
| 866 | * |
||
| 867 | * In this scenario, the used opted to override the existing connection |
||
| 868 | */ |
||
| 869 | public function testConnectCallbackWithoutCpfAndCollisionWithOverride() |
||
| 870 | { |
||
| 871 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 872 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 873 | $soapService = $this->getSoapService($accessId); |
||
| 874 | |||
| 875 | $cpf = '01234567890'; |
||
| 876 | $voterRegistration = '1234567890'; |
||
| 877 | $nfgProfile = $this->getNfgProfile($voterRegistration); |
||
| 878 | $person = new Person(); |
||
| 879 | $personMeuRS = new PersonMeuRS(); |
||
| 880 | $personMeuRS |
||
| 881 | ->setId(1) |
||
| 882 | ->setVoterRegistration($voterRegistration) |
||
| 883 | ->setPerson($person); |
||
| 884 | |||
| 885 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 886 | |||
| 887 | $otherPerson = new Person(); |
||
| 888 | $otherPerson->setCpf($cpf); |
||
| 889 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 890 | $otherPersonMeuRS |
||
| 891 | ->setId(2) |
||
| 892 | ->setNfgAccessToken($accessToken) |
||
| 893 | ->setNfgProfile($nfgProfile) |
||
| 894 | ->setPerson($otherPerson); |
||
| 895 | $meuRSHelper = $this->getMeuRSHelper($cpf, $otherPersonMeuRS); |
||
| 896 | |||
| 897 | $nfg = $this->getNfgService( |
||
| 898 | [ |
||
| 899 | 'session' => $this->getSession($accessId, 'none'), |
||
| 900 | 'soap' => $soapService, |
||
| 901 | 'meurs_helper' => $meuRSHelper, |
||
| 902 | ] |
||
| 903 | ); |
||
| 904 | |||
| 905 | $request = $this->getRequest($accessToken); |
||
| 906 | $nfg->connectCallback($request, $personMeuRS, true); |
||
| 907 | |||
| 908 | $this->assertNull($otherPersonMeuRS->getNfgProfile()); |
||
| 909 | $this->assertNull($otherPersonMeuRS->getNfgAccessToken()); |
||
| 910 | } |
||
| 911 | |||
| 912 | public function testDisconnection() |
||
| 913 | { |
||
| 914 | $em = $this->getEntityManager(['flush' => $this->atLeastOnce()]); |
||
| 915 | $em->expects($this->atLeastOnce())->method('remove')->willReturn(true); |
||
| 916 | |||
| 917 | $person = new Person(); |
||
| 918 | $personMeuRS = new PersonMeuRS(); |
||
| 919 | $personMeuRS |
||
| 920 | ->setNfgAccessToken('not null') |
||
| 921 | ->setNfgProfile($this->getNfgProfile()) |
||
| 922 | ->setPerson($person); |
||
| 923 | |||
| 924 | // Run the disconnection method |
||
| 925 | $this |
||
| 926 | ->getNfgService(compact('em')) |
||
| 927 | ->disconnect($personMeuRS); |
||
| 928 | |||
| 929 | $this->assertNull($personMeuRS->getNfgProfile()); |
||
| 930 | $this->assertNull($personMeuRS->getNfgAccessToken()); |
||
| 931 | } |
||
| 932 | |||
| 933 | private function getNfgService(array $collaborators) |
||
| 934 | { |
||
| 935 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 936 | if (false === array_key_exists('em', $collaborators)) { |
||
| 937 | $collaborators['em'] = $this->getEntityManager(); |
||
| 938 | } |
||
| 939 | if (false === array_key_exists('soap', $collaborators)) { |
||
| 940 | $collaborators['soap'] = $this->getSoapService($accessId); |
||
| 941 | } |
||
| 942 | if (false === array_key_exists('router', $collaborators)) { |
||
| 943 | $collaborators['router'] = TestsUtil::getRouter($this); |
||
| 944 | } |
||
| 945 | if (false === array_key_exists('session', $collaborators)) { |
||
| 946 | $collaborators['session'] = $this->getSession($accessId, 'none'); |
||
| 947 | } |
||
| 948 | if (false === array_key_exists('login_manager', $collaborators)) { |
||
| 949 | $collaborators['login_manager'] = $this->getLoginManager(false); |
||
| 950 | } |
||
| 951 | if (false === array_key_exists('meurs_helper', $collaborators)) { |
||
| 952 | $collaborators['meurs_helper'] = $this->getMeuRSHelper(); |
||
| 953 | } |
||
| 954 | if (false === array_key_exists('dispatcher', $collaborators)) { |
||
| 955 | $collaborators['dispatcher'] = $this->getDispatcher(); |
||
| 956 | } |
||
| 957 | if (false === array_key_exists('form_factory', $collaborators)) { |
||
| 958 | $collaborators['form_factory'] = $this->getFormFactory(); |
||
| 959 | } |
||
| 960 | if (false === array_key_exists('user_manager', $collaborators)) { |
||
| 961 | $collaborators['user_manager'] = $this->getUserManager(); |
||
| 962 | } |
||
| 963 | if (false === array_key_exists('firewall', $collaborators)) { |
||
| 964 | $collaborators['firewall'] = 'firewall'; |
||
| 965 | } |
||
| 966 | if (false === array_key_exists('login_endpoint', $collaborators)) { |
||
| 967 | $collaborators['login_endpoint'] = 'https://dum.my/login'; |
||
| 968 | } |
||
| 969 | if (false === array_key_exists('auth_endpoint', $collaborators)) { |
||
| 970 | $collaborators['auth_endpoint'] = 'https://dum.my/auth'; |
||
| 971 | } |
||
| 972 | if (false === array_key_exists('nfgprofile_repository', $collaborators)) { |
||
| 973 | $collaborators['nfgprofile_repository'] = $this->getNfgProfileRepository(); |
||
| 974 | } |
||
| 975 | if (false === array_key_exists('mailer', $collaborators)) { |
||
| 976 | $collaborators['mailer'] = $this->getMailer(); |
||
| 977 | } |
||
| 978 | |||
| 979 | $canonicalizer = new Canonicalizer(); |
||
| 980 | |||
| 981 | $nfg = new Nfg( |
||
| 982 | $collaborators['em'], |
||
| 983 | $collaborators['soap'], |
||
| 984 | $collaborators['router'], |
||
| 985 | $collaborators['session'], |
||
| 986 | $collaborators['login_manager'], |
||
| 987 | $collaborators['meurs_helper'], |
||
| 988 | $collaborators['dispatcher'], |
||
| 989 | $collaborators['user_manager'], |
||
| 990 | $collaborators['form_factory'], |
||
| 991 | $collaborators['nfgprofile_repository'], |
||
| 992 | $collaborators['mailer'], |
||
| 993 | $canonicalizer, |
||
| 994 | $collaborators['firewall'], |
||
| 995 | $collaborators['login_endpoint'], |
||
| 996 | $collaborators['auth_endpoint'] |
||
| 997 | ); |
||
| 998 | |||
| 999 | return $nfg; |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | private function getSoapService($accessId) |
||
| 1003 | { |
||
| 1004 | $soapService = $this->getMock('\PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoapInterface'); |
||
| 1005 | $soapService->expects($this->any())->method('getAccessID')->willReturn($accessId); |
||
| 1006 | |||
| 1007 | return $soapService; |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * @param bool $shouldCallLogInUser |
||
| 1012 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
| 1013 | */ |
||
| 1014 | private function getLoginManager($shouldCallLogInUser = false) |
||
| 1015 | { |
||
| 1016 | if ($shouldCallLogInUser) { |
||
| 1017 | $frequency = $this->atLeastOnce(); |
||
| 1018 | } else { |
||
| 1019 | $frequency = $this->any(); |
||
| 1020 | } |
||
| 1021 | $loginManager = $this->getMock('\FOS\UserBundle\Security\LoginManagerInterface'); |
||
| 1022 | $loginManager->expects($frequency) |
||
| 1023 | ->method('logInUser')->with( |
||
| 1024 | $this->isType('string'), |
||
| 1025 | $this->isInstanceOf('\FOS\UserBundle\Model\UserInterface'), |
||
| 1026 | $this->isInstanceOf('\Symfony\Component\HttpFoundation\Response') |
||
| 1027 | ); |
||
| 1028 | |||
| 1029 | return $loginManager; |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * @param $accessId |
||
| 1034 | * @param null $shouldCall |
||
| 1035 | * @return \PHPUnit_Framework_MockObject_MockObject|SessionInterface |
||
| 1036 | */ |
||
| 1037 | private function getSession($accessId, $shouldCall = null) |
||
| 1038 | { |
||
| 1039 | $session = $this->getMock('\Symfony\Component\HttpFoundation\Session\SessionInterface'); |
||
| 1040 | switch ($shouldCall) { |
||
| 1041 | case 'get': |
||
| 1042 | $session->expects($this->atLeastOnce()) |
||
| 1043 | ->method('get')->with(Nfg::ACCESS_ID_SESSION_KEY)->willReturn($accessId); |
||
| 1044 | break; |
||
| 1045 | case 'set': |
||
| 1046 | $session->expects($this->atLeastOnce()) |
||
| 1047 | ->method('set')->with(Nfg::ACCESS_ID_SESSION_KEY, $accessId); |
||
| 1048 | break; |
||
| 1049 | default: |
||
| 1050 | |||
| 1051 | } |
||
| 1052 | |||
| 1053 | return $session; |
||
| 1054 | } |
||
| 1055 | |||
| 1056 | private function getEntityManager(array $matchers = []) |
||
| 1057 | { |
||
| 1058 | $matchers = array_merge( |
||
| 1059 | [ |
||
| 1060 | 'persist' => $this->any(), |
||
| 1061 | 'flush' => $this->any(), |
||
| 1062 | ], |
||
| 1063 | $matchers |
||
| 1064 | ); |
||
| 1065 | |||
| 1066 | $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
||
| 1067 | ->disableOriginalConstructor() |
||
| 1068 | ->getMock(); |
||
| 1069 | $em->expects($matchers['persist'])->method('persist')->willReturn(true); |
||
| 1070 | $em->expects($matchers['flush'])->method('flush')->willReturn(true); |
||
| 1071 | |||
| 1072 | return $em; |
||
| 1073 | } |
||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * @param $accessToken |
||
| 1077 | * @return \PHPUnit_Framework_MockObject_MockObject|Request |
||
| 1078 | */ |
||
| 1079 | private function getRequest($accessToken) |
||
| 1080 | { |
||
| 1081 | $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); |
||
| 1082 | $request->expects($this->atLeastOnce())->method('get')->with('paccessid')->willReturn($accessToken); |
||
| 1083 | |||
| 1084 | return $request; |
||
| 1085 | } |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
| 1089 | */ |
||
| 1090 | private function getDispatcher() |
||
| 1091 | { |
||
| 1092 | return $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * @return FormFactory|\PHPUnit_Framework_MockObject_MockObject |
||
| 1097 | */ |
||
| 1098 | private function getFormFactory() |
||
| 1099 | { |
||
| 1100 | $formFactory = $this->getMockBuilder('FOS\UserBundle\Form\Factory\FormFactory') |
||
| 1101 | ->disableOriginalConstructor() |
||
| 1102 | ->getMock(); |
||
| 1103 | |||
| 1104 | $formFactory->expects($this->any())->method('createForm') |
||
| 1105 | ->willReturnCallback(function () { |
||
| 1106 | return $this->getMock('Symfony\Component\Form\FormInterface'); |
||
| 1107 | }); |
||
| 1108 | |||
| 1109 | return $formFactory; |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | private function getNfgProfile($voterRegistration = null) |
||
| 1113 | { |
||
| 1114 | $nfgProfile = new NfgProfile(); |
||
| 1115 | $nfgProfile->setName('John Doe') |
||
| 1116 | ->setEmail('[email protected]') |
||
| 1117 | ->setBirthdate('1970-01-01T00:00:00') |
||
| 1118 | ->setMobile('+555193333333') |
||
| 1119 | ->setVoterRegistration($voterRegistration) |
||
| 1120 | ->setCpf('1234567890')// NFG treats CPF as integers, hence no leading 0s |
||
| 1121 | ->setAccessLvl(2); |
||
| 1122 | |||
| 1123 | return $nfgProfile; |
||
| 1124 | } |
||
| 1125 | |||
| 1126 | /** |
||
| 1127 | * @return UserManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
||
| 1128 | */ |
||
| 1129 | private function getUserManager() |
||
| 1130 | { |
||
| 1131 | $userManager = $this->getMock('FOS\UserBundle\Model\UserManagerInterface'); |
||
| 1132 | $userManager->expects($this->any())->method('createUser')->willReturnCallback(function () { |
||
| 1133 | return new Person(); |
||
| 1134 | }); |
||
| 1135 | |||
| 1136 | return $userManager; |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | private function getMeuRSHelper($expectedCpf = null, $response = null) |
||
| 1140 | { |
||
| 1141 | $meuRSHelper = $this->getMockBuilder('PROCERGS\LoginCidadao\CoreBundle\Helper\MeuRSHelper') |
||
| 1142 | ->disableOriginalConstructor() |
||
| 1143 | ->getMock(); |
||
| 1144 | |||
| 1145 | if ($expectedCpf) { |
||
| 1146 | $meuRSHelper->expects($this->atLeastOnce()) |
||
| 1147 | ->method('getPersonByCpf')->with($expectedCpf)->willReturn($response); |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | return $meuRSHelper; |
||
| 1151 | } |
||
| 1152 | |||
| 1153 | private function getNfgProfileRepository() |
||
| 1154 | { |
||
| 1155 | return $this->getMockBuilder('PROCERGS\LoginCidadao\NfgBundle\Entity\NfgProfileRepository') |
||
| 1156 | ->disableOriginalConstructor() |
||
| 1157 | ->getMock(); |
||
| 1158 | } |
||
| 1159 | |||
| 1160 | private function getMailer() |
||
| 1161 | { |
||
| 1162 | return $this->getMock('PROCERGS\LoginCidadao\NfgBundle\Mailer\MailerInterface'); |
||
| 1163 | } |
||
| 1164 | |||
| 1165 | public function testRegisterCpfTakeOver() |
||
| 1166 | { |
||
| 1167 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 1168 | $soapService = $this->getSoapService($accessId); |
||
| 1169 | |||
| 1170 | $nfgProfile = $this->getNfgProfile(); |
||
| 1171 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 1172 | |||
| 1173 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 1174 | $otherPersonMeuRS->setPerson(new Person()); |
||
| 1175 | $otherPersonMeuRS->getPerson()->setCpf($nfgProfile->getCpf()); |
||
| 1176 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), $otherPersonMeuRS); |
||
| 1177 | |||
| 1178 | $dispatcher = $this->getDispatcher(); |
||
| 1179 | $dispatcher->expects($this->atLeastOnce())->method('dispatch') |
||
| 1180 | ->willReturnCallback(function ($eventName, $event) { |
||
| 1181 | if ($eventName === FOSUserEvents::REGISTRATION_INITIALIZE |
||
| 1182 | && $event instanceof GetResponseUserEvent |
||
| 1183 | ) { |
||
| 1184 | $event->setResponse(new RedirectResponse('dummy')); |
||
| 1185 | } |
||
| 1186 | }); |
||
| 1187 | |||
| 1188 | $mailer = $this->getMailer(); |
||
| 1189 | $mailer->expects($this->atLeastOnce())->method('notifyCpfLost'); |
||
| 1190 | |||
| 1191 | $nfg = $this->getNfgService( |
||
| 1192 | [ |
||
| 1193 | 'session' => $this->getSession($accessId, 'none'), |
||
| 1194 | 'soap' => $soapService, |
||
| 1195 | 'meurs_helper' => $meuRSHelper, |
||
| 1196 | 'dispatcher' => $dispatcher, |
||
| 1197 | 'mailer' => $mailer, |
||
| 1198 | ] |
||
| 1199 | ); |
||
| 1200 | |||
| 1201 | $personMeuRS = new PersonMeuRS(); |
||
| 1202 | |||
| 1203 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 1204 | $request = $this->getRequest($accessToken); |
||
| 1205 | /** @var RedirectResponse $response */ |
||
| 1206 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 1207 | |||
| 1208 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 1209 | $this->assertEquals('dummy', $response->getTargetUrl()); |
||
| 1210 | } |
||
| 1211 | |||
| 1212 | public function testRegisterEmailMissing() |
||
| 1213 | { |
||
| 1214 | $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\MissingRequiredInformationException'); |
||
| 1215 | |||
| 1216 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 1217 | $soapService = $this->getSoapService($accessId); |
||
| 1218 | |||
| 1219 | $nfgProfile = $this->getNfgProfile(); |
||
| 1220 | $nfgProfile->setEmail(null); |
||
| 1221 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 1222 | |||
| 1223 | $nfg = $this->getNfgService( |
||
| 1224 | [ |
||
| 1225 | 'session' => $this->getSession($accessId, 'none'), |
||
| 1226 | 'soap' => $soapService, |
||
| 1227 | ] |
||
| 1228 | ); |
||
| 1229 | |||
| 1230 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 1231 | $request = $this->getRequest($accessToken); |
||
| 1232 | $nfg->connectCallback($request, new PersonMeuRS()); |
||
| 1233 | } |
||
| 1234 | } |
||
| 1235 |