示例#1
0
 public function testWrongCurrency()
 {
     $wrongFormats = array('lalla', '0000', '0', '', null, '----');
     foreach ($wrongFormats as $value) {
         $this->context->setPricePerNight($value);
         $result = $this->calculator->calculate($this->context);
         $this->assertTrue(isset($result['errors']), 'No price format failure ' . $value);
         $this->assertNotEmpty($result['errors']);
         $this->assertCount(1, $result['errors']);
     }
 }
 /**
  * @Route("/add-booking/{pageId}")
  * @Method("POST")
  * @param Request $request
  * @throws \Exception
  * @return JsonResponse
  */
 public function addBookingAction($pageId, Request $request)
 {
     /**
      * @var Pages $page
      */
     $page = $this->getDoctrine()->getRepository('AppBundle:Pages')->find($pageId);
     if (!$page) {
         throw $this->createNotFoundException('No page found for request param ' . $pageId);
     }
     $response = new JsonResponse();
     $bookingEntity = new Booking();
     $bookingEntity->setPage($page);
     $form = $this->createForm(new BookingForm('/add-booking/' . $page->getId()), $bookingEntity);
     $form->handleRequest($request);
     $validatorBuilder = new BookingValidatorChainBuilder();
     $validatorChain = $validatorBuilder->buildChain();
     $values = $request->request->all();
     $bookingEntity->setPriceString($page->getPrice());
     if (isset($values['appbundle_booking'])) {
         $values = $values['appbundle_booking'];
     } else {
         $values = array();
     }
     $values['priceString'] = $page->getPrice();
     if ($validatorChain->validateContext($values) === null) {
         $calculator = new Calculator();
         $calcRequest = new CalculationContext();
         $calcRequest->setPricePerNight($values['priceString']);
         $calcRequest->setGuestsCount($values['guestsCount']);
         $calcRequest->setCheckOutDate($values['checkoutDate']);
         $calcRequest->setCheckInDate($values['checkinDate']);
         $calcResult = $calculator->calculate($calcRequest);
         $bookingEntity->setNightsCount($calcResult['nightsCount']);
         $bookingEntity->setTotal($calcResult['total']);
         $em = $this->getDoctrine()->getManager();
         $em->persist($bookingEntity);
         $em->flush();
         try {
             $this->sendBookingEmail($bookingEntity);
         } catch (\Exception $e) {
         }
         $response->setData(array('success' => true));
     } else {
         $response->setData(array('errors' => $validatorChain->getFlatenErrors()));
     }
     return $response;
 }