public function testWrongInterval() { $wrongIntervals = array(array('2012-01-01', '2011-01-01'), array('2012-03-01', '2012-02-01'), array('2012-03-11', '2012-03-10'), array('2012-03-10', '2012-03-10')); foreach ($wrongIntervals as $format) { $this->context->setCheckInDate($format[0]); $this->context->setCheckOutDate($format[1]); $result = $this->calculator->calculate($this->context); $this->assertTrue(isset($result['errors']), 'No interval failure for values ' . $format[0] . '/' . $format[1]); $this->assertNotEmpty($result['errors'], 'No interval failure for values ' . $format[0] . '/' . $format[1]); $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; }