function it_throws_an_exception_when_recipe_with_same_name_already_exists(Recipes $recipes)
 {
     $recipes->hasRecipeWithName(Argument::type(Name::class))->willReturn(true);
     $command = new CreateNewRecipeCommand();
     $command->name = "Screwdriver";
     $this->shouldThrow(RecipeAlreadyExistsException::class)->during('handle', [$command]);
 }
 /**
  * @param AddRecipeStepCommand $command
  * @throws RecipeNotFoundException
  * @throws UnknownStepException
  * @throws \MyDrinks\Domain\Exception\Recipe\GlassCapacityOverflowException
  * @throws \MyDrinks\Domain\Exception\Recipe\MissingGlassException
  */
 public function handle(AddRecipeStepCommand $command)
 {
     $recipe = $this->recipes->findBySlug($command->slug);
     if (is_null($recipe)) {
         throw new RecipeNotFoundException(sprintf("Recipe with slug \"%s\" does not exists.", $command->slug));
     }
     switch ($command->type) {
         case Actions::PREPARE_GLASS:
             $amount = is_null($command->amount) ? null : new Amount($command->amount);
             $recipe->prepareTheGlass(new Name($command->name), new Capacity($command->capacity), $amount);
             break;
         case Actions::POUR_INTO_GLASS:
             $recipe->pourIntoGlass(new Name($command->name), new Capacity($command->capacity));
             break;
         case Actions::STRAIN_INTO_GLASS_FROM_SHAKER:
             $recipe->strainIntoGlassFromShaker();
             break;
         case Actions::ADD_INGREDIENT_INTO_GLASS:
             $recipe->addIngredientIntoGlass(new Name($command->name), new Amount($command->amount));
             break;
         case Actions::STIR_GLASS_CONTENT:
             $recipe->stirGlassContent();
             break;
         case Actions::FILL_GLASS:
             $recipe->fillGlassWith(new Name($command->name));
             break;
         case Actions::IGNITE_GLASS_CONTENT:
             $recipe->igniteGlassContent();
             break;
         case Actions::GARNISH_GLASS:
             $recipe->garnishGlass(new Name($command->name));
             break;
         case Actions::EMPTY_GLASS_CONTENT:
             $recipe->emptyTheGlass();
             break;
         case Actions::MUDDLE_GLASS_CONTENT:
             $recipe->muddleContent();
             break;
         case Actions::TOP_UP_GLASS:
             $recipe->topUpGlass(new Name($command->name));
             break;
         case Actions::PREPARE_SHAKER:
             $recipe->prepareTheShaker(new Capacity($command->capacity));
             break;
         case Actions::POUR_INTO_SHAKER:
             $recipe->pourIntoShaker(new Name($command->name), new Capacity($command->capacity));
             break;
         case Actions::SHAKE_SHAKER_CONTENT:
             $recipe->shakeShakerContent();
             break;
         case Actions::FILL_SHAKER:
             $recipe->fillShakerWith(new Name($command->name));
             break;
         case Actions::ADD_INGREDIENT_INTO_SHAKER:
             $recipe->addIngredientIntoShaker(new Name($command->name), new Amount($command->amount));
             break;
         default:
             throw new UnknownStepException(sprintf("Unknown step type \"%s\"", $command->type));
     }
 }
 /**
  * @param CreateNewRecipeCommand $command
  * @throws RecipeAlreadyExistsException
  */
 public function handle(CreateNewRecipeCommand $command)
 {
     $recipe = $this->recipeFactory->createRecipe($command->name);
     if ($this->recipes->hasRecipeWithName($recipe->getName())) {
         throw new RecipeAlreadyExistsException();
     }
     $this->recipes->add($recipe);
 }
 /**
  * @param RemoveRecipeCommand $command
  * @throws RecipeNotFoundException
  */
 public function handle(RemoveRecipeCommand $command)
 {
     $recipe = $this->recipes->findBySlug($command->slug);
     if (is_null($recipe)) {
         throw new RecipeNotFoundException(sprintf("Recipe with slug \"%s\" does not exists.", $command->slug));
     }
     $this->recipes->remove($recipe);
 }
 function it_updates_recipe_description(Recipes $recipes, ImageStorage $imageStorage)
 {
     $recipes->findBySlug("screwdriver")->willReturn($this->createRecipe());
     $command = new RemoveRecipeImageCommand();
     $command->slug = 'screwdriver';
     $imageStorage->hasImageFor("screwdriver")->willReturn(true);
     $imageStorage->removeImageFor("screwdriver")->shouldBeCalled();
     $this->handle($command);
 }
 public function it_remove_step_from_recipe(Recipes $recipes)
 {
     $recipe = $this->createRecipe();
     $recipes->findBySlug("screwdriver")->willReturn($recipe);
     $recipes->remove($recipe)->shouldBeCalled();
     $command = new RemoveRecipeCommand();
     $command->slug = 'screwdriver';
     $this->handle($command);
 }
 function it_updates_recipe_description(Recipes $recipes, ImageStorage $imageStorage, Filesystem $filesystem)
 {
     $recipes->findBySlug("screwdriver")->willReturn(new Recipe(new Name("Screwdriver")));
     $command = new UploadRecipeImageCommand();
     $command->slug = 'screwdriver';
     $command->image = new FakeFile("fake_image.jpg");
     $command->extension = 'jpg';
     $filesystem->read('fake_image.jpg')->willReturn('content');
     $imageStorage->saveImageFor(Argument::type(Image::class), "screwdriver")->shouldBeCalled();
     $this->handle($command);
 }
 function it_publish_command(Recipes $recipes)
 {
     $recipe = $this->createRecipe();
     $recipes->findBySlug("screwdriver")->willReturn($recipe);
     $command = new PublishRecipeCommand();
     $command->slug = 'screwdriver';
     $this->handle($command);
     if (!$recipe->isPublished()) {
         throw new \RuntimeException("Expected recipe to be published.");
     }
 }
 /**
  * @param RemoveRecipeImageCommand $command
  * @throws RecipeImageNotFoundException
  * @throws RecipeNotFoundException
  */
 public function handle(RemoveRecipeImageCommand $command)
 {
     $recipe = $this->recipes->findBySlug($command->slug);
     if (is_null($recipe)) {
         throw new RecipeNotFoundException();
     }
     if (!$this->imageStorage->hasImageFor($command->slug)) {
         throw new RecipeImageNotFoundException();
     }
     $this->imageStorage->removeImageFor($command->slug);
 }
 /**
  * @param PublishRecipeCommand $command
  * @throws RecipeAlreadyPublishedException
  * @throws RecipeNotFoundException
  */
 public function handle(PublishRecipeCommand $command)
 {
     $recipe = $this->recipes->findBySlug($command->slug);
     if (is_null($recipe)) {
         throw new RecipeNotFoundException(sprintf("Recipe with slug \"%s\" does not exists.", $command->slug));
     }
     if ($recipe->isPublished()) {
         throw new RecipeAlreadyPublishedException();
     }
     $recipe->publish();
 }
 public function it_remove_step_from_recipe(Recipes $recipes)
 {
     $recipe = $this->createRecipe();
     $recipes->findBySlug("screwdriver")->willReturn($recipe);
     $command = new RemoveRecipeStepCommand();
     $command->slug = 'screwdriver';
     $command->number = 2;
     $this->handle($command);
     if (count($recipe->getSteps()) !== 1) {
         throw new \RuntimeException(sprintf("Expected recipe step count is 1, %d given.", count($recipe->getSteps())));
     }
 }
 /**
  * @param UploadRecipeImageCommand $command
  * @throws RecipeNotFoundException
  * @throws RuntimeException
  */
 public function handle(UploadRecipeImageCommand $command)
 {
     $recipe = $this->recipes->findBySlug($command->slug);
     if (is_null($recipe)) {
         throw new RecipeNotFoundException();
     }
     if (!$command->image instanceof \SplFileInfo) {
         throw new RuntimeException("UploadRecipeImageHandler expected image to implement \\SplFileInfo");
     }
     $image = new Image($command->slug . '.' . $command->extension, $this->filesystem->read($command->image->getRealPath()));
     $this->imageStorage->saveImageFor($image, $command->slug);
 }
 function it_updates_recipe_description(Recipes $recipes)
 {
     $recipe = $this->createRecipe();
     $recipes->findBySlug("screwdriver")->willReturn($recipe);
     $command = new UpdateRecipeDescriptionCommand();
     $command->slug = 'screwdriver';
     $command->text = 'Lorem ipsum';
     $command->IBAOfficial = true;
     $command->taste = [Tastes::SWEET, Tastes::SOUR];
     $this->handle($command);
     if (!$recipe->getDescription()->isOfficialIBA()) {
         throw new \RuntimeException("Expected recipe to be IBA official.");
     }
     if (!$recipe->getDescription()->getTaste()->isSweet() && !$recipe->getDescription()->getTaste()->isSour()) {
         throw new \RuntimeException("Expected recipe to be sweet and sour.");
     }
     if ($recipe->getDescription()->getText() !== $command->text) {
         throw new \RuntimeException(sprintf("Expected recipe description is %s, %s given.", $command->text, $recipe->getDescription()->getText()));
     }
 }
 /**
  * @param UpdateRecipeDescriptionCommand $command
  * @throws RecipeNotFoundException
  * @throws \MyDrinks\Domain\Exception\InvalidArgumentException
  */
 public function handle(UpdateRecipeDescriptionCommand $command)
 {
     $recipe = $this->recipes->findBySlug($command->slug);
     if (is_null($recipe)) {
         throw new RecipeNotFoundException();
     }
     $description = new Description();
     if ((bool) $command->IBAOfficial) {
         $description->markAsIBAOfficial();
     }
     if (strlen($command->text)) {
         $description->setText($command->text);
     }
     if (!is_null($command->alcoholContent)) {
         $description->setAlcoholContent($command->alcoholContent);
     }
     if (!empty($command->taste) && is_array($command->taste)) {
         $tasteBuilder = new TasteBuilder();
         foreach ($command->taste as $tasteName) {
             switch ($tasteName) {
                 case Tastes::SWEET:
                     $tasteBuilder->sweet();
                     break;
                 case Tastes::BITTER:
                     $tasteBuilder->bitter();
                     break;
                 case Tastes::SALTY:
                     $tasteBuilder->salty();
                     break;
                 case Tastes::SPICY:
                     $tasteBuilder->spicy();
                     break;
                 case Tastes::SOUR:
                     $tasteBuilder->sour();
                     break;
             }
         }
         $description->changeTaste($tasteBuilder->buildTaste());
     }
     $recipe->updateDescription($description);
 }
 function let(Recipes $recipes, Recipe $recipe)
 {
     $recipes->findBySlug("invalid")->willReturn(null);
     $recipes->findBySlug("screwdriver")->willReturn($recipe);
     $this->beConstructedWith($recipes);
 }
 /**
  * @Then the recipe should not be published
  */
 public function theRecipeShouldNotBePublished()
 {
     $recipe = $this->recipes->findRecipeByName($this->currentRecipeName);
     expect($recipe->isPublished())->toBe(false);
 }