Completed
Push — master ( 1cdf46...d8c9d1 )
by Adrien
07:48
created

BookingTest::testGetPeriodicPriceWithoutSharing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Model;
6
7
use Application\Model\Bookable;
8
use Application\Model\Booking;
9
use Application\Model\User;
10
use Money\Money;
11
use PHPUnit\Framework\TestCase;
12
use Prophecy\Argument;
13
14
class BookingTest extends TestCase
15
{
16
    public function testOwnerRelation(): void
17
    {
18
        $booking = new Booking();
19
        self::assertNull($booking->getOwner(), 'booking should have no owner');
20
21
        $user = new User();
22
        self::assertCount(0, $user->getBookings(), 'user should have no bookings');
23
24
        $booking->setOwner($user);
25
        self::assertCount(1, $user->getBookings(), 'user should have the added booking');
26
        self::assertSame($booking, $user->getBookings()->first(), 'user should have the same booking');
27
        self::assertSame($user, $booking->getOwner(), 'booking should have owner');
28
    }
29
30
    public function testBookableRelation(): void
31
    {
32
        $booking = new Booking();
33
34
        $bookable = new Bookable();
35
        self::assertCount(0, $bookable->getBookings(), 'bookable should have no bookings');
36
37
        $booking->setBookable($bookable);
38
        self::assertCount(1, $bookable->getBookings(), 'bookable should have the added booking');
39
        self::assertSame($booking, $bookable->getBookings()->first(), 'bookable should have the same booking');
40
        self::assertSame($bookable, $booking->getBookable(), 'booking should be able to retrieve added bookable');
41
42
        $booking->setBookable($bookable);
43
        self::assertCount(1, $bookable->getBookings(), 'bookable should still have exactly 1 booking');
44
        self::assertSame($booking, $bookable->getBookings()->first(), 'bookable should have the same booking');
45
        self::assertSame($bookable, $booking->getBookable(), 'booking should still have the same unique bookable');
46
47
        $bookable2 = new Bookable();
48
        $booking->setBookable($bookable2);
49
        self::assertCount(0, $bookable->getBookings(), 'bookable should not have any booking anymore');
50
        self::assertCount(1, $bookable2->getBookings(), 'bookable2 should have a new booking');
51
        self::assertSame($bookable2, $booking->getBookable(), 'booking should have only the second bookable left');
52
    }
53
54
    public function testGetPeriodicPriceWithoutSharing(): void
55
    {
56
        $booking = new Booking();
57
58
        $prophecy1 = $this->prophesize(Bookable::class);
59
        $prophecy1->getSharedBookings()->willReturn([]);
60
        $prophecy1->getPeriodicPrice()->willReturn(Money::CHF(500));
61
        $prophecy1->bookingAdded(Argument::is($booking));
62
63
        $booking->setBookable($prophecy1->reveal());
64
        self::assertEquals(Money::CHF(500), $booking->getPeriodicPrice(), 'price should be exactly the bookable price');
65
    }
66
67
    public function testGetPeriodicPriceWithSharing(): void
68
    {
69
        $booking = new Booking();
70
71
        $prophecy1 = $this->prophesize(Bookable::class);
72
        $prophecy1->getSharedBookings()->willReturn([1, 2, 3]);
73
        $prophecy1->getPeriodicPrice()->willReturn(Money::CHF(500));
74
        $prophecy1->bookingAdded(Argument::is($booking));
75
76
        $booking->setBookable($prophecy1->reveal());
77
        self::assertEquals(Money::CHF(167), $booking->getPeriodicPrice(), 'price should be divided by the number of shared booking');
78
    }
79
}
80