| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Kreta package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Beñat Espiña <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * (c) Gorka Laucirica <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace Spec\Kreta\TaskManager\Application\Command\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Kreta\SharedKernel\Domain\Model\Identity\Slug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Kreta\TaskManager\Application\Command\Project\CreateProjectCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Kreta\TaskManager\Domain\Model\Organization\Organization; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Kreta\TaskManager\Domain\Model\Organization\OrganizationDoesNotExistException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Kreta\TaskManager\Domain\Model\Organization\OrganizationId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Kreta\TaskManager\Domain\Model\Project\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Kreta\TaskManager\Domain\Model\Project\ProjectAlreadyExists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Kreta\TaskManager\Domain\Model\Project\ProjectId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Kreta\TaskManager\Domain\Model\Project\ProjectName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Kreta\TaskManager\Domain\Model\Project\ProjectRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Kreta\TaskManager\Domain\Model\Project\UnauthorizedCreateProjectException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Kreta\TaskManager\Domain\Model\User\UserId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use PhpSpec\ObjectBehavior; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use Prophecy\Argument; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | class CreateProjectHandlerSpec extends ObjectBehavior | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     private $name; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     private $slug; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     private $id; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     private $organizationId; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     private $organizationSlug; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     private $creatorId; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     function let( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         OrganizationRepository $organizationRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         ProjectRepository $repository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         ProjectSpecificationFactory $specificationFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         CreateProjectCommand $command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->beConstructedWith($organizationRepository, $repository, $specificationFactory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $command->name()->shouldBeCalled()->willReturn('The project name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $command->slug()->shouldBeCalled()->willReturn('the-project-slug'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $command->id()->shouldBeCalled()->willReturn('project-id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $command->organizationId()->shouldBeCalled()->willReturn('organization-id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $command->creatorId()->shouldBeCalled()->willReturn('creator-id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->name = new ProjectName('The project name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->slug = new Slug('the-project-slug'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->id = ProjectId::generate('project-id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->organizationId = OrganizationId::generate('organization-id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->organizationSlug = new Slug('organization-slug'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->creatorId = UserId::generate('creator-id'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 65 |  | View Code Duplication |     function it_does_not_allow_to_create_project_if_organization_does_not_exist( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         OrganizationRepository $organizationRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         CreateProjectCommand $command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $organizationRepository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->shouldThrow(OrganizationDoesNotExistException::class)->during__invoke($command); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     function it_does_not_allow_to_create_project_if_id_already_exists( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ProjectRepository $repository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         CreateProjectCommand $command, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         OrganizationRepository $organizationRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         Organization $organization, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         Project $project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $organizationRepository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $organization->slug()->shouldBeCalled()->willReturn($this->organizationSlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $repository->projectOfId($this->id)->shouldBeCalled()->willReturn($project); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->shouldThrow(ProjectAlreadyExists::class)->during__invoke($command); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 86 |  | View Code Duplication |     function it_does_not_allow_to_create_project_if_slug_already_exists_in_organization( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ProjectRepository $repository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         CreateProjectCommand $command, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         OrganizationRepository $organizationRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         Organization $organization, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         ProjectSpecificationFactory $specificationFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         Project $project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $organizationRepository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $organization->slug()->shouldBeCalled()->willReturn($this->organizationSlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $repository->projectOfId($this->id)->shouldBeCalled()->willReturn(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $specificationFactory->buildBySlugSpecification($this->slug, $this->organizationSlug)->shouldBeCalled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $repository->singleResultQuery(Argument::any())->shouldBeCalled()->willReturn($project); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->shouldThrow(ProjectAlreadyExists::class)->during__invoke($command); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 102 |  | View Code Duplication |     function it_does_not_allow_to_create_project_if_creator_is_not_organization_owner( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         OrganizationRepository $organizationRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         ProjectRepository $repository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         CreateProjectCommand $command, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         Organization $organization, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         ProjectSpecificationFactory $specificationFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $organizationRepository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $repository->projectOfId($this->id)->shouldBeCalled()->willReturn(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $organization->slug()->shouldBeCalled()->willReturn($this->organizationSlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $specificationFactory->buildBySlugSpecification($this->slug, $this->organizationSlug)->shouldBeCalled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $repository->singleResultQuery(Argument::any())->shouldBeCalled()->willReturn(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $organization->isOwner(UserId::generate('creator-id'))->shouldBeCalled()->willReturn(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->shouldThrow(UnauthorizedCreateProjectException::class)->during__invoke($command); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 118 |  | View Code Duplication |     function it_creates_project( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         OrganizationRepository $organizationRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         ProjectRepository $repository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         CreateProjectCommand $command, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         Organization $organization, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         ProjectSpecificationFactory $specificationFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $organizationRepository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $repository->projectOfId($this->id)->shouldBeCalled()->willReturn(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $organization->slug()->shouldBeCalled()->willReturn($this->organizationSlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $specificationFactory->buildBySlugSpecification($this->slug, $this->organizationSlug)->shouldBeCalled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $repository->singleResultQuery(Argument::any())->shouldBeCalled()->willReturn(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $organization->isOwner($this->creatorId)->shouldBeCalled()->willReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $repository->persist(Argument::type(Project::class))->shouldBeCalled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $this->__invoke($command); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     function it_creates_project_with_default_slug( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         OrganizationRepository $organizationRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ProjectRepository $repository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         ProjectSpecificationFactory $specificationFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         CreateProjectCommand $command, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         Organization $organization | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $command->slug()->shouldBeCalled()->willReturn(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $slug = new Slug('The project name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $organizationRepository->organizationOfId($this->organizationId)->shouldBeCalled()->willReturn($organization); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $repository->projectOfId($this->id)->shouldBeCalled()->willReturn(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $organization->slug()->shouldBeCalled()->willReturn($this->organizationSlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $specificationFactory->buildBySlugSpecification($slug, $this->organizationSlug)->shouldBeCalled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $repository->singleResultQuery(Argument::any())->shouldBeCalled()->willReturn(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $organization->isOwner($this->creatorId)->shouldBeCalled()->willReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $repository->persist(Argument::type(Project::class))->shouldBeCalled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $this->__invoke($command); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.