Code Duplication    Length = 57-57 lines in 2 locations

eZ/Publish/Core/Repository/Tests/Service/Mock/RoleTest.php 2 locations

@@ 426-482 (lines=57) @@
423
     * @covers \eZ\Publish\Core\Repository\RoleService::assignRoleToUser
424
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
425
     */
426
    public function testAssignRoleToUserWithNullLimitation()
427
    {
428
        $repository = $this->getRepositoryMock();
429
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues']);
430
        $roleMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\Role');
431
        $userMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\User');
432
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
433
434
        $userMock->expects($this->any())
435
            ->method('__get')
436
            ->with('id')
437
            ->will($this->returnValue(24));
438
439
        $repository->expects($this->once())
440
            ->method('canUser')
441
            ->with(
442
                $this->equalTo('role'),
443
                $this->equalTo('assign'),
444
                $this->equalTo($userMock),
445
                $this->equalTo($roleMock)
446
            )->will($this->returnValue(true));
447
448
        $roleMock->expects($this->any())
449
            ->method('__get')
450
            ->with('id')
451
            ->will($this->returnValue(42));
452
453
        $userHandlerMock->expects($this->once())
454
            ->method('loadRole')
455
            ->with($this->equalTo(42))
456
            ->will($this->returnValue(new SPIRole(['id' => 42])));
457
458
        $userHandlerMock->expects($this->once())
459
            ->method('load')
460
            ->with($this->equalTo(24))
461
            ->will($this->returnValue(new SPIUser(['id' => 24])));
462
463
        $roleServiceMock->expects($this->once())
464
            ->method('checkAssignmentAndFilterLimitationValues')
465
            ->with(24, $this->isInstanceOf('\\eZ\\Publish\\SPI\\Persistence\\User\\Role'), null)
466
            ->will($this->returnValue(null));
467
468
        $repository->expects($this->once())->method('beginTransaction');
469
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
470
        $userHandlerMock->expects($this->once())
471
            ->method('assignRole')
472
            ->with(
473
                $this->equalTo(24),
474
                $this->equalTo(42),
475
                $this->equalTo(null)
476
            );
477
        $repository->expects($this->once())->method('commit');
478
479
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
480
        /* @var \eZ\Publish\API\Repository\Values\User\User $userMock */
481
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, null);
482
    }
483
484
    /**
485
     * Test for the assignRoleToUser() method.
@@ 491-547 (lines=57) @@
488
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
489
     * @expectedException \Exception
490
     */
491
    public function testAssignRoleToUserWithRollback()
492
    {
493
        $repository = $this->getRepositoryMock();
494
        $roleServiceMock = $this->getPartlyMockedRoleService(['checkAssignmentAndFilterLimitationValues']);
495
        $roleMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\Role');
496
        $userMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\User');
497
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
498
499
        $userMock->expects($this->any())
500
            ->method('__get')
501
            ->with('id')
502
            ->will($this->returnValue(24));
503
504
        $repository->expects($this->once())
505
            ->method('canUser')
506
            ->with(
507
                $this->equalTo('role'),
508
                $this->equalTo('assign'),
509
                $this->equalTo($userMock),
510
                $this->equalTo($roleMock)
511
            )->will($this->returnValue(true));
512
513
        $roleMock->expects($this->any())
514
            ->method('__get')
515
            ->with('id')
516
            ->will($this->returnValue(42));
517
518
        $userHandlerMock->expects($this->once())
519
            ->method('loadRole')
520
            ->with($this->equalTo(42))
521
            ->will($this->returnValue(new SPIRole(['id' => 42])));
522
523
        $userHandlerMock->expects($this->once())
524
            ->method('load')
525
            ->with($this->equalTo(24))
526
            ->will($this->returnValue(new SPIUser(['id' => 24])));
527
528
        $roleServiceMock->expects($this->once())
529
            ->method('checkAssignmentAndFilterLimitationValues')
530
            ->with(24, $this->isInstanceOf('\\eZ\\Publish\\SPI\\Persistence\\User\\Role'), null)
531
            ->will($this->returnValue(null));
532
533
        $repository->expects($this->once())->method('beginTransaction');
534
        $userHandlerMock = $this->getPersistenceMockHandler('User\\Handler');
535
        $userHandlerMock->expects($this->once())
536
            ->method('assignRole')
537
            ->with(
538
                $this->equalTo(24),
539
                $this->equalTo(42),
540
                $this->equalTo(null)
541
            )->will($this->throwException(new \Exception()));
542
        $repository->expects($this->once())->method('rollback');
543
544
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
545
        /* @var \eZ\Publish\API\Repository\Values\User\User $userMock */
546
        $roleServiceMock->assignRoleToUser($roleMock, $userMock, null);
547
    }
548
549
    /**
550
     * Test for the assignRoleToUserGroup() method.