|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TimothePearce\TimeSeries\Tests; |
| 4 | + |
| 5 | +use Illuminate\Support\Carbon; |
| 6 | +use TimothePearce\TimeSeries\Collections\ProjectionCollection; |
| 7 | +use TimothePearce\TimeSeries\Exceptions\MissingProjectionNameException; |
| 8 | +use TimothePearce\TimeSeries\Exceptions\MissingProjectionPeriodException; |
| 9 | +use TimothePearce\TimeSeries\Models\Projection; |
| 10 | +use TimothePearce\TimeSeries\Tests\Models\Projections\TableReservationPerDiningDayProjection; |
| 11 | +use TimothePearce\TimeSeries\Tests\Models\Projections\TableReservationPerMadeDayProjection; |
| 12 | +use TimothePearce\TimeSeries\Tests\Models\TableReservation; |
| 13 | + |
| 14 | +class ProjectionWithConfigurableDateTest extends TestCase |
| 15 | +{ |
| 16 | + use ProjectableFactory; |
| 17 | + |
| 18 | + public function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + $this->travelTo(Carbon::today()); |
| 23 | + } |
| 24 | + |
| 25 | + /** @test */ |
| 26 | + public function it_gets_a_custom_collection() |
| 27 | + { |
| 28 | + TableReservation::factory()->count(2)->create(); |
| 29 | + |
| 30 | + $collection = Projection::all(); |
| 31 | + |
| 32 | + $this->assertInstanceOf(ProjectionCollection::class, $collection); |
| 33 | + } |
| 34 | + |
| 35 | + /** @test */ |
| 36 | + public function it_has_a_relationship_with_the_model() |
| 37 | + { |
| 38 | + TableReservation::factory()->create(); |
| 39 | + $projection = Projection::first(); |
| 40 | + |
| 41 | + $this->assertNotEmpty($projection->from(TableReservation::class)->get()); |
| 42 | + } |
| 43 | + |
| 44 | + /** @test */ |
| 45 | + public function it_gets_the_projections_from_projection_name() |
| 46 | + { |
| 47 | + $this->createModelWithProjections(TableReservation::class, [TableReservationPerDiningDayProjection::class]); |
| 48 | + $this->createModelWithProjections(TableReservation::class, [TableReservationPerMadeDayProjection::class]); |
| 49 | + |
| 50 | + $numberOfProjections = Projection::name(TableReservationPerDiningDayProjection::class)->count(); |
| 51 | + |
| 52 | + $this->assertEquals(1, $numberOfProjections); |
| 53 | + } |
| 54 | + |
| 55 | + /** @test */ |
| 56 | + public function it_gets_the_projections_from_a_single_period() |
| 57 | + { |
| 58 | + $this->createModelWithProjections(TableReservation::class, [TableReservationPerDiningDayProjection::class]); // 1 |
| 59 | + $this->createModelWithProjections(TableReservation::class, [TableReservationPerDiningDayProjection::class]); // 1 |
| 60 | + $this->travel(5)->days(); |
| 61 | + $this->createModelWithProjections(TableReservation::class, [TableReservationPerDiningDayProjection::class]); // 2 |
| 62 | + |
| 63 | + $numberOfProjections = Projection::period('1 day')->count(); |
| 64 | + |
| 65 | + $this->assertEquals(2, $numberOfProjections); |
| 66 | + } |
| 67 | + |
| 68 | + /** @test */ |
| 69 | + public function it_raises_an_exception_when_using_the_between_scope_without_a_period() |
| 70 | + { |
| 71 | + $this->expectException(MissingProjectionNameException::class); |
| 72 | + |
| 73 | + Projection::between(now()->subMinute(), now()); |
| 74 | + } |
| 75 | + |
| 76 | + /** @test */ |
| 77 | + public function it_raises_an_exception_when_using_the_between_scope_without_the_projection_name() |
| 78 | + { |
| 79 | + $this->expectException(MissingProjectionPeriodException::class); |
| 80 | + |
| 81 | + Projection::name(TableReservationPerDiningDayProjection::class)->between(now()->subMinute(), now()); |
| 82 | + } |
| 83 | + |
| 84 | + /** @test */ |
| 85 | + public function it_gets_the_projections_between_the_given_dates_for_made_date() |
| 86 | + { |
| 87 | + $this->createModelWithProjections(TableReservation::class, [TableReservationPerMadeDayProjection::class]); // 1 // Should be excluded |
| 88 | + $this->travel(5)->days(); |
| 89 | + $tablereservation = $this->createModelWithProjections(TableReservation::class, [TableReservationPerMadeDayProjection::class]); // 1 // Should be included |
| 90 | + $this->travel(5)->days(); |
| 91 | + $this->createModelWithProjections(TableReservation::class, [TableReservationPerMadeDayProjection::class]); // 1 // Should be excluded |
| 92 | + |
| 93 | + $this->travelBack(); |
| 94 | + |
| 95 | + $betweenProjections = Projection::name(TableReservationPerMadeDayProjection::class) |
| 96 | + ->period('1 day') |
| 97 | + ->between( |
| 98 | + Carbon::today()->addDays(5), |
| 99 | + Carbon::today()->addDays(10) |
| 100 | + )->get(); |
| 101 | + $this->assertCount(1, $betweenProjections); |
| 102 | + $this->assertEquals($betweenProjections->first()->id, $tablereservation->firstProjection(TableReservationPerMadeDayProjection::class)->id); |
| 103 | + $this->assertEquals($betweenProjections->first()->start_date, Carbon::today()->addDays(5)); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + /** @test */ |
| 108 | + public function it_gets_the_projections_between_the_given_dates_for_dining_date() |
| 109 | + { |
| 110 | + $this->createModelWithProjections(TableReservation::class, [TableReservationPerDiningDayProjection::class]); // 1 // Should be excluded |
| 111 | + $this->travel(5)->days(); |
| 112 | + $tablereservation = $this->createModelWithProjections(TableReservation::class, [TableReservationPerDiningDayProjection::class]); // 1 // Should be included |
| 113 | + $this->travel(5)->days(); |
| 114 | + $this->createModelWithProjections(TableReservation::class, [TableReservationPerDiningDayProjection::class]); // 1 // Should be excluded |
| 115 | + |
| 116 | + $this->travelBack(); |
| 117 | + |
| 118 | + /* Here we test based on the dining date of the reservation and it should be made_date + 10 days */ |
| 119 | + $betweenProjections = Projection::name(TableReservationPerDiningDayProjection::class) |
| 120 | + ->period('1 day') |
| 121 | + ->between( |
| 122 | + Carbon::today()->addDays(15), |
| 123 | + Carbon::today()->addDays(20) |
| 124 | + )->get(); |
| 125 | + |
| 126 | + $this->assertCount(1, $betweenProjections); |
| 127 | + $this->assertEquals($betweenProjections->first()->id, $tablereservation->firstProjection(TableReservationPerDiningDayProjection::class)->id); |
| 128 | + $this->assertEquals($betweenProjections->first()->start_date, Carbon::today()->addDays(15)); // dining_date is set 10 day from creation date |
| 129 | + } |
| 130 | + |
| 131 | + /** @test */ |
| 132 | + public function it_gets_the_projections_between_the_given_dates_for_all_projections() |
| 133 | + { |
| 134 | + $tablereservation1 = TableReservation::factory()->create(); |
| 135 | + // 1 made_date = created_at = now(), reservation_date = now()+10 days, total_people = 2, number_reservation = 1 |
| 136 | + $this->travel(15)->minutes(); |
| 137 | + $tablereservation2 = TableReservation::factory()->create(); |
| 138 | + // 2 made_date = created_at = now(), reservation_date = now()+10, total_people = 2+2, number_reservation = 1+1 |
| 139 | + $this->travel(2)->days(); |
| 140 | + $tablereservation3 = TableReservation::factory()->create(); |
| 141 | + // 3 made_date = created_at = now()+2, reservation_date = now()+2+10, total_people = 2, number_reservation = 1 |
| 142 | + |
| 143 | + $this->travelBack(); // reset the Carbon:date back to today |
| 144 | + |
| 145 | + /* Here we test based on the dining date of the reservation and it should be made_date + 10 days */ |
| 146 | + $betweenProjections = Projection::name(TableReservationPerDiningDayProjection::class) |
| 147 | + ->period('1 day') |
| 148 | + ->between( |
| 149 | + Carbon::today()->addDays(10), |
| 150 | + Carbon::today()->addDays(11) |
| 151 | + )->get(); |
| 152 | + $this->assertCount(1, $betweenProjections); |
| 153 | + $this->assertEquals(4, $betweenProjections->first()->content['total_people']); |
| 154 | + $this->assertEquals(2, $betweenProjections->first()->content['number_reservations']); |
| 155 | + |
| 156 | + $betweenProjections = Projection::name(TableReservationPerMadeDayProjection::class) |
| 157 | + ->period('1 day') |
| 158 | + ->between( |
| 159 | + Carbon::today(), |
| 160 | + Carbon::today()->addDays(2) |
| 161 | + )->get(); |
| 162 | + $this->assertCount(1, $betweenProjections); |
| 163 | + $this->assertEquals(4, $betweenProjections->first()->content['total_people']); |
| 164 | + $this->assertEquals(2, $betweenProjections->first()->content['number_reservations']); |
| 165 | + |
| 166 | + $betweenProjections = Projection::name(TableReservationPerDiningDayProjection::class) |
| 167 | + ->period('1 day') |
| 168 | + ->between( |
| 169 | + Carbon::today()->addDays(12), |
| 170 | + Carbon::today()->addDays(13) |
| 171 | + )->get(); |
| 172 | + $this->assertCount(1, $betweenProjections); |
| 173 | + $this->assertEquals(2, $betweenProjections->first()->content['total_people']); |
| 174 | + $this->assertEquals(1, $betweenProjections->first()->content['number_reservations']); |
| 175 | + |
| 176 | + $betweenProjections = Projection::name(TableReservationPerMadeDayProjection::class) |
| 177 | + ->period('1 day') |
| 178 | + ->between( |
| 179 | + Carbon::today()->addDays(2), |
| 180 | + Carbon::today()->addDays(3) |
| 181 | + )->get(); |
| 182 | + $this->assertCount(1, $betweenProjections); |
| 183 | + $this->assertEquals(2, $betweenProjections->first()->content['total_people']); |
| 184 | + $this->assertEquals(1, $betweenProjections->first()->content['number_reservations']); |
| 185 | + } |
| 186 | +} |
0 commit comments