Advertisement
Guest User

Untitled

a guest
Dec 28th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.64 KB | None | 0 0
  1. <?php
  2.  
  3. namespace spec\Reservation;
  4.  
  5. use Core\CompanyId;
  6. use Core\RoomId;
  7. use Core\RoomMasterId;
  8. use DateTime;
  9. use PhpSpec\ObjectBehavior;
  10. use Prophecy\Argument;
  11. use Reservation\BlockedTermWasCanceled;
  12. use Reservation\CustomerReservationInformation;
  13. use Reservation\Room;
  14. use Reservation\SourcesOfReservations\ReservationSourceId;
  15. use Reservation\Term;
  16. use Reservation\TermCannotBeBlocked;
  17. use Reservation\TermCannotBeEdited;
  18. use Reservation\ReservationCannotUnfulfilled;
  19. use Reservation\TermCannotBeUnblocked;
  20. use Reservation\TermWasBlocked;
  21. use Reservation\TermWasReserved;
  22. use Reservation\TermWasCanceled;
  23. use Reservation\TermWasReservedByGameMaster;
  24. use Reservation\TimeFrameWasAdded;
  25.  
  26. /**
  27. * Class RoomSpec
  28. * @package spec\Reservation
  29. * @mixin Room
  30. */
  31. class RoomSpec extends ObjectBehavior
  32. {
  33. public function it_is_initializable()
  34. {
  35. $this->shouldHaveType('Reservation\Room');
  36. }
  37.  
  38. public function let()
  39. {
  40. $this->beConstructedWith(new RoomId('123'), new CompanyId('987'));
  41. }
  42.  
  43. public function it_allows_to_reserve_new_term()
  44. {
  45. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  46. $this->addAvailableTimeFrame($timeFrame);
  47.  
  48. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  49. $customerReservationInformation = new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com');
  50. $reservationSourceId = new ReservationSourceId('sourceId');
  51.  
  52. $this->addNewReservation($term, $customerReservationInformation, $reservationSourceId);
  53.  
  54. $this->getRecordedEvents()[1]->shouldBeLike(TermWasReserved::fromRoomTerm(
  55. new RoomId('123'),
  56. new CompanyId('987'),
  57. $term,
  58. $customerReservationInformation,
  59. $reservationSourceId
  60. ));
  61. }
  62.  
  63. public function it_allows_to_reserve_by_game_master()
  64. {
  65. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  66. $this->addAvailableTimeFrame($timeFrame);
  67.  
  68. $roomMasterId = new RoomMasterId('id');
  69. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  70. $this->reserveByGameMaster($term, new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com'), $roomMasterId);
  71.  
  72. $this->getRecordedEvents()[1]->shouldBeLike(
  73. TermWasReservedByGameMaster::fromRoomTerm(new RoomId('123'), $term, new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com'), $roomMasterId)
  74. );
  75. }
  76.  
  77. public function it_cannot_reserve_room_in_same_time()
  78. {
  79. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  80. $this->addAvailableTimeFrame($timeFrame);
  81.  
  82. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  83. $this->reserveByCustomer($term, new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com'));
  84.  
  85. $this->shouldThrow(\LogicException::class)->duringReserveByCustomer($term, new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com'));
  86. }
  87.  
  88. public function it_cannot_cancel_not_reserved_term()
  89. {
  90. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  91. $this->addAvailableTimeFrame($timeFrame);
  92.  
  93. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  94.  
  95. $this->shouldThrow(\LogicException::class)->duringCancelReservation($term);
  96. }
  97.  
  98. public function it_cancel_reservation()
  99. {
  100. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  101. $this->addAvailableTimeFrame($timeFrame);
  102.  
  103. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  104. $customerReservationInformation = new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com');
  105. $reservationSourceId = new ReservationSourceId('sourceId');
  106.  
  107. $this->addNewReservation($term, $customerReservationInformation, $reservationSourceId);
  108. $this->cancelReservation($term);
  109.  
  110. $events = $this->getRecordedEvents();
  111.  
  112. $events->shouldHaveCount(3);
  113. $events[1]->shouldBeLike(TermWasReserved::fromRoomTerm(
  114. new RoomId('123'),
  115. new CompanyId('987'),
  116. $term,
  117. $customerReservationInformation,
  118. $reservationSourceId
  119. ));
  120.  
  121. $events[2]->shouldBeLike(TermWasCanceled::fromRoomTerm(new RoomId('123'), new CompanyId('987'), $term));
  122. }
  123.  
  124. public function it_allows_to_reserve_term_after_cancel()
  125. {
  126. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  127. $this->addAvailableTimeFrame($timeFrame);
  128.  
  129. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  130. $customerReservationInformation = new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com');
  131. $reservationSourceId = new ReservationSourceId('sourceId');
  132.  
  133. $this->addNewReservation($term, $customerReservationInformation, $reservationSourceId);
  134. $this->cancelReservation($term);
  135. $this->addNewReservation($term, $customerReservationInformation, $reservationSourceId);
  136.  
  137. $events = $this->getRecordedEvents();
  138. $events->shouldHaveCount(4);
  139. $events[3]->shouldBeLike(TermWasReserved::fromRoomTerm(
  140. new RoomId('123'),
  141. new CompanyId('987'),
  142. $term,
  143. $customerReservationInformation,
  144. $reservationSourceId
  145. ));
  146. }
  147.  
  148. public function it_allows_to_add_new_open_hour()
  149. {
  150. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::MON, Room\OpenHour::fromString('17:00'));
  151. $this->addAvailableTimeFrame($timeFrame);
  152.  
  153. $events = $this->getRecordedEvents();
  154. $events->shouldHaveCount(1);
  155. $events[0]->shouldBeLike(
  156. TimeFrameWasAdded::fromRoomSingleTimeFrame(new RoomId('123'), $timeFrame, new DateTime())
  157. );
  158. }
  159.  
  160. public function it_clear_recoreded_events_after_getting_them()
  161. {
  162. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  163. $this->addAvailableTimeFrame($timeFrame);
  164.  
  165. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  166.  
  167. $this->reserveByCustomer($term, new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com'));
  168.  
  169. $events = $this->getRecordedEvents();
  170. $events->shouldHaveCount(2);
  171.  
  172. $this->getRecordedEvents()->shouldHaveCount(0);
  173. }
  174.  
  175. public function it_can_be_reconstructed_from_events()
  176. {
  177. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  178. $term2 = Term::fromDateTime(new \DateTimeImmutable('08.01.2016 15:40'));
  179. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  180.  
  181. $roomId = new RoomId('123');
  182. $companyId = new CompanyId('987');
  183. $customerReservationInformation = new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com');
  184. $reservationSourceId = new ReservationSourceId('sourceId');
  185.  
  186. $events = [
  187. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime()),
  188. TermWasReserved::fromRoomTerm(
  189. $roomId,
  190. $companyId,
  191. $term,
  192. $customerReservationInformation,
  193. $reservationSourceId
  194. ),
  195. TermWasReserved::fromRoomTerm(
  196. $roomId,
  197. $companyId,
  198. $term2,
  199. $customerReservationInformation,
  200. $reservationSourceId
  201. ),
  202. TermWasCanceled::fromRoomTerm($roomId, $companyId, $term),
  203.  
  204. ];
  205. $this->beConstructedThrough('fromEvents', [$roomId, $companyId, $events]);
  206.  
  207. $this->getAggregateRootId()->shouldBeLike($roomId);
  208. }
  209.  
  210. public function it_allows_to_block_term()
  211. {
  212. $roomId = new RoomId('123');
  213. $companyId = new CompanyId('987');
  214. $reasonOfBlockade = 'dobry powód';
  215. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  216. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  217. $existingEvents = [
  218. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime())
  219. ];
  220. $this->beConstructedThrough('fromEvents', [$roomId, $companyId, $existingEvents]);
  221.  
  222. $this->block($term, new RoomMasterId('master-id'), $reasonOfBlockade);
  223.  
  224. $events = $this->getRecordedEvents();
  225. $events->shouldHaveCount(1);
  226.  
  227. $events[0]->shouldBeLike(
  228. TermWasBlocked::fromRoomTerm($roomId, $companyId, $term, $reasonOfBlockade, new RoomMasterId('master-id'))
  229. );
  230. }
  231.  
  232. public function it_does_not_allow_to_block_term_when_given_term_is_reserved_already()
  233. {
  234. $roomId = new RoomId('123');
  235. $companyId = new CompanyId('987');
  236. $reasonOfBlockade = 'dobry powód';
  237. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  238. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  239. $existingEvents = [
  240. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime()),
  241. TermWasReserved::fromRoomTerm(
  242. $roomId,
  243. $companyId,
  244. $term,
  245. new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com'),
  246. new ReservationSourceId('sourceId')
  247. )
  248. ];
  249. $this->beConstructedThrough('fromEvents', [$roomId, $companyId, $existingEvents]);
  250.  
  251. $this->shouldThrow(TermCannotBeBlocked::class)->duringBlock($term, new RoomMasterId('master-id'), $reasonOfBlockade);
  252.  
  253. $events = $this->getRecordedEvents();
  254. $events->shouldHaveCount(0);
  255. }
  256.  
  257. public function it_allows_to_block_term_after_cancelling_reservation_for_that_term()
  258. {
  259. $roomId = new RoomId('123');
  260. $companyId = new CompanyId('987');
  261. $reasonOfBlockade = 'dobry powód';
  262. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  263. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  264. $existingEvents = [
  265. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime()),
  266. TermWasReserved::fromRoomTerm(
  267. $roomId,
  268. $companyId,
  269. $term,
  270. new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com'),
  271. new ReservationSourceId('sourceId')
  272. ),
  273. TermWasCanceled::fromRoomTerm($roomId, $companyId, $term)
  274. ];
  275. $this->beConstructedThrough('fromEvents', [$roomId, $companyId, $existingEvents]);
  276.  
  277. $this->block($term, new RoomMasterId('master-id'), $reasonOfBlockade);
  278.  
  279. $events = $this->getRecordedEvents();
  280. $events->shouldHaveCount(1);
  281.  
  282. $events[0]->shouldBeLike(
  283. TermWasBlocked::fromRoomTerm(new RoomId('123'), $companyId, $term, $reasonOfBlockade, new RoomMasterId('master-id'))
  284. );
  285. }
  286.  
  287. public function it_not_allows_to_reservate_for_other_term_that_is_not_predefinate()
  288. {
  289. $roomId = new RoomId('123');
  290. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  291. $existingEvents = [
  292. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime())
  293. ];
  294. $this->beConstructedThrough('fromEvents', [$roomId, new CompanyId('987'), $existingEvents]);
  295.  
  296. $term = Term::fromDateTime(new \DateTimeImmutable('02.01.2016 15:40'));
  297.  
  298. $this->shouldThrow(\LogicException::class)->during(
  299. 'addNewReservation',
  300. [
  301. $term,
  302. new CustomerReservationInformation('test', 'test', 'email@email.pl'),
  303. new ReservationSourceId('sourceId')
  304. ]);
  305. }
  306.  
  307. public function it_can_delete_all_predefinated_terms()
  308. {
  309. $roomId = new RoomId('123');
  310. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  311. $existingEvents = [
  312. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime())
  313. ];
  314.  
  315. $this->beConstructedThrough('fromEvents', [$roomId, new CompanyId('987'), $existingEvents]);
  316. $this->clearAvailableTimeFrames();
  317.  
  318. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  319.  
  320. $this->shouldThrow(\LogicException::class)->during(
  321. 'addNewReservation',
  322. [
  323. $term,
  324. new CustomerReservationInformation('test', 'test', 'email@email.pl'),
  325. new ReservationSourceId('sourceId')
  326. ]);
  327. }
  328.  
  329. public function it_can_cancel_blocked_term()
  330. {
  331. $roomId = new RoomId('123');
  332. $companyId = new CompanyId('987');
  333. $reasonOfBlockade = 'dobry powód';
  334. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  335. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  336. $existingEvents = [
  337. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime()),
  338. ];
  339. $this->beConstructedThrough('fromEvents', [$roomId, $companyId, $existingEvents]);
  340.  
  341. $this->block($term, new RoomMasterId('master-id'), $reasonOfBlockade);
  342. $this->unblock($term, new RoomMasterId('master-id'));
  343.  
  344. $events = $this->getRecordedEvents();
  345. $events->shouldHaveCount(2);
  346.  
  347. $events[1]->shouldBeLike(
  348. BlockedTermWasCanceled::fromRoomTerm($roomId, $companyId, $term, new RoomMasterId('master-id'))
  349. );
  350. }
  351.  
  352. public function it_cannot_cancel_block_that_not_exist()
  353. {
  354. $roomId = new RoomId('123');
  355.  
  356. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  357. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  358. $existingEvents = [
  359. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime()),
  360. ];
  361. $this->beConstructedThrough('fromEvents', [$roomId, new CompanyId('987'), $existingEvents]);
  362.  
  363. $this->shouldThrow(TermCannotBeUnblocked::class)->during('unblock', [$term, new RoomMasterId('master-id')]);
  364. }
  365.  
  366. public function it_cant_block_term_twice()
  367. {
  368. $roomId = new RoomId('123');
  369. $reasonOfBlockade = 'dobry powód';
  370. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  371. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  372. $existingEvents = [
  373. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime())
  374. ];
  375. $this->beConstructedThrough('fromEvents', [$roomId, new CompanyId('987'), $existingEvents]);
  376.  
  377. $this->block($term, new RoomMasterId('master-id'), $reasonOfBlockade);
  378.  
  379. $this->shouldThrow(TermCannotBeBlocked::class)->during('block', [$term, new RoomMasterId('master-id'), $reasonOfBlockade]);
  380. }
  381.  
  382. public function it_can_block_term_after_unblock()
  383. {
  384. $roomId = new RoomId('123');
  385. $companyId = new CompanyId('987');
  386. $reasonOfBlockade = 'dobry powód';
  387. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  388. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  389. $existingEvents = [
  390. TimeFrameWasAdded::fromRoomSingleTimeFrame($roomId, $timeFrame, new DateTime())
  391. ];
  392. $this->beConstructedThrough('fromEvents', [$roomId, $companyId, $existingEvents]);
  393.  
  394. $this->block($term, new RoomMasterId('master-id'), $reasonOfBlockade);
  395. $this->unblock($term, new RoomMasterId('master-id'));
  396. $this->block($term, new RoomMasterId('master-id'), $reasonOfBlockade);
  397.  
  398. $events = $this->getRecordedEvents();
  399. $events->shouldHaveCount(3);
  400.  
  401. $events[2]->shouldBeLike(
  402. TermWasBlocked::fromRoomTerm($roomId, $companyId, $term, $reasonOfBlockade, new RoomMasterId('master-id'))
  403. );
  404. }
  405.  
  406. public function it_can_edit_only_when_there_is_reservation()
  407. {
  408. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  409. $this->addAvailableTimeFrame($timeFrame);
  410. $roomMasterId = new RoomMasterId('id');
  411. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  412. $this->reserveByGameMaster($term, new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com'), $roomMasterId);
  413.  
  414. $this->editReservation($term, new CustomerReservationInformation('name', 'phone'), $roomMasterId);
  415. }
  416.  
  417. public function it_throws_cannot_edit_without_reservation()
  418. {
  419. $this->addAvailableTimeFrame(new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40')));
  420. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  421.  
  422. $this->shouldThrow(TermCannotBeEdited::class)->during('editReservation', [$term, new CustomerReservationInformation('name', 'phone'), new RoomMasterId('id')]);
  423. }
  424.  
  425. public function it_can_mark_as_unfulfilled_only_when_there_is_reservation()
  426. {
  427. $timeFrame = new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40'));
  428. $this->addAvailableTimeFrame($timeFrame);
  429. $roomMasterId = new RoomMasterId('id');
  430. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  431. $this->reserveByGameMaster($term, new CustomerReservationInformation('customer name', '661200032', 'szymon.skowronski@gmail.com'), $roomMasterId);
  432.  
  433. $this->groupAreNotComing($term, $roomMasterId);
  434. }
  435.  
  436. public function it_throws_cannot_mark_as_unfulfilled_without_reservation()
  437. {
  438. $this->addAvailableTimeFrame(new Room\SingleTimeFrame(Room\SingleTimeFrame::FRI, Room\OpenHour::fromString('15:40')));
  439. $term = Term::fromDateTime(new \DateTimeImmutable('01.01.2016 15:40'));
  440.  
  441. $this->shouldThrow(ReservationCannotUnfulfilled::class)->during('groupAreNotComing', [$term, new RoomMasterId('id')]);
  442. }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement