Skip to content

Commit b435390

Browse files
committed
of last singular sticky to exclusive
1 parent 22516ec commit b435390

File tree

7 files changed

+148
-22
lines changed

7 files changed

+148
-22
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ phpstan.neon
99
testbench.yaml
1010
vendor
1111
node_modules
12+
.env

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class Transaction extends Model
115115

116116
```php
117117
// query by SECONDS
118+
Transaction::ofJustNow(); // query transactions created just now
118119
Transaction::ofLastSecond(); // query transactions created during the last second
119120
Transaction::ofLast15Seconds(); // query transactions created during the last 15 seconds
120121
Transaction::ofLast30Seconds(); // query transactions created during the last 30 seconds
@@ -221,7 +222,7 @@ Transaction::ofLastMillenniums(2); // query transactions created during the last
221222

222223
```php
223224
// query by toNow/toDate
224-
Transaction::secondToNow(); // query transactions created during the start of the current second to now (not really usefull I guess)
225+
Transaction::secondToNow(); // query transactions created during the start of the current second to now (equivalent of just now)
225226
Transaction::minuteToNow(); // query transactions created during the start of the current minute to now
226227
Transaction::hourToNow(); // query transactions created during the start of the current hour to now
227228
Transaction::dayToNow(); // query transactions created during the start of the current day to now

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"require": {
1919
"php": "^8.1",
2020
"spatie/laravel-package-tools": "^1.13.0",
21-
"illuminate/contracts": "^9.0 || ^10.0"
21+
"illuminate/contracts": "^9.0 || ^10.0",
22+
"illuminate/database": "^9.0 || ^10.0",
23+
"illuminate/support": "^9.0 || ^10.0"
2224
},
2325
"require-dev": {
2426
"nunomaduro/larastan": "^2.0.1",

src/DateScopes.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function scopeOfLastUnit(Builder $query, string $dateUnit, int $value, Da
4343
$subFunc = 'sub'.ucfirst($dateUnit).'s'.$applyNoOverflow;
4444

4545
$sub = ($dateUnit === 'second') ? 0 : 1;
46+
// $sub = 1;
4647

4748
if ($dateRange === DateRange::EXCLUSIVE) {
4849
$range = [
@@ -56,13 +57,14 @@ public function scopeOfLastUnit(Builder $query, string $dateUnit, int $value, Da
5657
];
5758
}
5859

59-
// dd(collect($range)->transform(fn ($item) => $item->format('Y-m-d H:i:s'))->toArray());
60+
// dump(collect($range)->transform(fn ($item) => $item->format('Y-m-d H:i:s'))->toArray());
6061

6162
return $query->whereBetween(config('date-scopes.created_column'), $range);
6263
}
6364

6465
// START SECONDS
65-
public function scopeOfLastSecond(Builder $query): Builder {return $query->ofLastSeconds(1);}
66+
public function scopeOfJustNow(Builder $query): Builder {return $query->ofLastSeconds(1, DateRange::INCLUSIVE);}
67+
public function scopeOfLastSecond(Builder $query): Builder {return $query->ofLastSeconds(1, DateRange::EXCLUSIVE);}
6668
public function scopeOfLast15Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(15, $customRange);}
6769
public function scopeOfLast30Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(30, $customRange);}
6870
public function scopeOfLast45Seconds(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastSeconds(45, $customRange);}
@@ -74,7 +76,7 @@ public function scopeOfLastSeconds(Builder $query, int $seconds, DateRange $cust
7476
}
7577

7678
// START MINUTES
77-
public function scopeOfLastMinute(Builder $query): Builder {return $query->ofLastMinutes(1);}
79+
public function scopeOfLastMinute(Builder $query): Builder {return $query->ofLastMinutes(1, DateRange::EXCLUSIVE);}
7880
public function scopeOfLast15Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(15, $customRange);}
7981
public function scopeOfLast30Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(30, $customRange);}
8082
public function scopeOfLast45Minutes(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMinutes(45, $customRange);}
@@ -86,7 +88,7 @@ public function scopeOfLastMinutes(Builder $query, int $minutes, DateRange $cust
8688
}
8789

8890
// START HOURS
89-
public function scopeOfLastHour(Builder $query): Builder {return $query->ofLastHours(1);}
91+
public function scopeOfLastHour(Builder $query): Builder {return $query->ofLastHours(1, DateRange::EXCLUSIVE);}
9092
public function scopeOfLast6Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(6, $customRange);}
9193
public function scopeOfLast12Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(12, $customRange);}
9294
public function scopeOfLast18Hours(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastHours(18, $customRange);}
@@ -111,7 +113,7 @@ public function scopeOfLastDays(Builder $query, int $days, DateRange $customRang
111113
}
112114

113115
// START WEEKS
114-
public function scopeOfLastWeek(Builder $query): Builder {return $query->ofLastWeeks(1);}
116+
public function scopeOfLastWeek(Builder $query): Builder {return $query->ofLastWeeks(1, DateRange::EXCLUSIVE);}
115117
public function scopeOfLast2Weeks(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastWeeks(2, $customRange);}
116118
public function scopeOfLast3Weeks(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastWeeks(3, $customRange);}
117119
public function scopeOfLast4Weeks(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastWeeks(4, $customRange);}
@@ -122,7 +124,7 @@ public function scopeOfLastWeeks(Builder $query, int $weeks, DateRange $customRa
122124
}
123125

124126
// START MONTHS
125-
public function scopeOfLastMonth(Builder $query): Builder {return $query->ofLastMonths(1);}
127+
public function scopeOfLastMonth(Builder $query): Builder {return $query->ofLastMonths(1, DateRange::EXCLUSIVE);}
126128
public function scopeOfLast3Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(3, $customRange);}
127129
public function scopeOfLast6Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(6, $customRange);}
128130
public function scopeOfLast9Months(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastMonths(9, $customRange);}
@@ -134,7 +136,7 @@ public function scopeOfLastMonths(Builder $query, int $months, DateRange $custom
134136
}
135137

136138
// START QUARTER
137-
public function scopeOfLastQuarter(Builder $query): Builder {return $query->ofLastQuarters(1);}
139+
public function scopeOfLastQuarter(Builder $query): Builder {return $query->ofLastQuarters(1, DateRange::EXCLUSIVE);}
138140
public function scopeOfLast2Quarters(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastQuarters(2, $customRange);}
139141
public function scopeOfLast3Quarters(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastQuarters(3, $customRange);}
140142
public function scopeOfLast4Quarters(Builder $query, DateRange $customRange = null): Builder {return $query->ofLastQuarters(4, $customRange);}
@@ -145,23 +147,23 @@ public function scopeOfLastQuarters(Builder $query, int $quarters, DateRange $cu
145147
}
146148

147149
// START YEARS
148-
public function scopeOfLastYear(Builder $query): Builder {return $query->ofLastYears(1);}
150+
public function scopeOfLastYear(Builder $query): Builder {return $query->ofLastYears(1, DateRange::EXCLUSIVE);}
149151

150152
public function scopeOfLastYears(Builder $query, int $years, DateRange $customRange = null): Builder
151153
{
152154
return $query->ofLastUnit('year', $years, $customRange);
153155
}
154156

155157
// START DECADE
156-
public function scopeOfLastDecade(Builder $query): Builder {return $query->ofLastDecades(1);}
158+
public function scopeOfLastDecade(Builder $query): Builder {return $query->ofLastDecades(1, DateRange::EXCLUSIVE);}
157159

158160
public function scopeOfLastDecades(Builder $query, int $decades, DateRange $customRange = null): Builder
159161
{
160162
return $query->ofLastUnit('decade', $decades, $customRange);
161163
}
162164

163165
// START MILLENNIUM
164-
public function scopeOfLastMillennium(Builder $query): Builder {return $query->ofLastMillenniums(1);}
166+
public function scopeOfLastMillennium(Builder $query): Builder {return $query->ofLastMillenniums(1, DateRange::EXCLUSIVE);}
165167

166168
public function scopeOfLastMillenniums(Builder $query, int $millennium, DateRange $customRange = null): Builder
167169
{

tests/DataScopesTest.php

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Eloquent\Factories\Sequence;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Carbon;
7+
use LaracraftTech\LaravelDateScopes\DateRange;
8+
use LaracraftTech\LaravelDateScopes\Tests\Models\Transaction;
9+
10+
function getCreatedAtValues(): array
11+
{
12+
return [
13+
// SECONDS
14+
["created_at" => "2023-03-31 13:15:15"], // query transactions created just now
15+
["created_at" => "2023-03-31 13:16:15"], // query transactions created during the last second
16+
["created_at" => "2023-03-31 13:16:00"], // query transactions created during the last 15 seconds
17+
["created_at" => "2023-03-24 00:00:00"], // query transactions created during the last 30 seconds
18+
["created_at" => "2023-03-17 00:00:00"], // query transactions created during the last 45 seconds
19+
["created_at" => "2023-01-13 00:00:00"], // query transactions created during the last 60 seconds
20+
["created_at" => "2022-04-06 00:00:00"], // query transactions created during the last N seconds
21+
// MINUTES
22+
["created_at" => "2022-06-12 00:00:00"], // query transactions created during the last minute
23+
["created_at" => "1970-12-07 00:00:00"], // query transactions created during the last 15 minutes
24+
["created_at" => "2002-03-18 00:00:00"], // query transactions created during the last 30 minutes
25+
["created_at" => "2005-05-12 00:00:00"], // query transactions created during the last 45 minutes
26+
["created_at" => "1995-03-26 00:00:00"], // query transactions created during the last 60 minutes
27+
["created_at" => "1980-08-19 00:00:00"], // query transactions created during the last N minutes
28+
// HOURS
29+
["created_at" => "2002-02-27 00:00:00"], // query transactions created during the last hour
30+
["created_at" => "1993-04-29 00:00:00"], // query transactions created during the last 6 hours
31+
["created_at" => "2000-12-12 00:00:00"], // query transactions created during the last 12 hours
32+
["created_at" => "1978-01-28 00:00:00"], // query transactions created during the last 18 hours
33+
["created_at" => "2001-02-11 00:00:00"], // query transactions created during the last 24 hours
34+
["created_at" => "1977-04-19 00:00:00"], // query transactions created during the last N hours
35+
// DAYS
36+
["created_at" => "1982-01-30 00:00:00"], // query transactions created today
37+
["created_at" => "1985-08-28 00:00:00"], // query transactions created yesterday
38+
["created_at" => "1973-10-15 00:00:00"], // query transactions created during the last 7 days
39+
["created_at" => "2008-11-25 00:00:00"], // query transactions created during the last 21 days
40+
["created_at" => "2020-09-20 00:00:00"], // query transactions created during the last 30 days
41+
["created_at" => "1988-03-18 00:00:00"], // query transactions created during the last N days
42+
// WEEKS
43+
["created_at" => "2004-06-19 00:00:00"], // query transactions created during the last week
44+
["created_at" => "1978-12-01 00:00:00"], // query transactions created during the last 2 weeks
45+
["created_at" => "1983-07-09 00:00:00"], // query transactions created during the last 3 weeks
46+
["created_at" => "2006-05-25 00:00:00"], // query transactions created during the last 4 weeks
47+
["created_at" => "2006-06-21 00:00:00"], // query transactions created during the last N weeks
48+
//MONTHS
49+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last month
50+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last 3 months
51+
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the last 6 months
52+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last 9 months
53+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last 12 months
54+
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the last N months
55+
//QUARTERS
56+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last quarter
57+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last 2 quarters
58+
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the last 3 quarters
59+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last 4 quarters
60+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last N quarters
61+
//YEARS
62+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last year
63+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last N years
64+
//DECADES
65+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last year
66+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last N years
67+
//MILLENNIUMS
68+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last year
69+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last N years
70+
//toNow/toDate
71+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the start of the current second to now (not really usefull I guess)
72+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the start of the current minute to now
73+
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the start of the current hour to now
74+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the start of the current day to now
75+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the start of the current week to now
76+
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the start of the current month to now
77+
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the start of the current quarter to now
78+
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the start of the current year to now
79+
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the start of the current decade to now
80+
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the start of the current millennium to
81+
];
82+
}
83+
84+
beforeEach(function () {
85+
// Set a fixed date and time for our tests (start on overflow date!)
86+
Carbon::setTestNow(Carbon::create(2023, 3, 31, 13, 15, 15));
87+
88+
Schema::create('transactions', function (Blueprint $blueprint) {
89+
$blueprint->id();
90+
$blueprint->string('col1');
91+
$blueprint->integer('col2');
92+
$blueprint->timestamps();
93+
});
94+
95+
$createdAtValues = getCreatedAtValues();
96+
97+
Transaction::factory()
98+
->count(count($createdAtValues))
99+
->state(new Sequence(...$createdAtValues))
100+
->create();
101+
102+
Transaction::ofJustNow()->get();
103+
Transaction::ofLast15Seconds(DateRange::INCLUSIVE)->get();
104+
Transaction::ofLast15Seconds(DateRange::EXCLUSIVE)->get();
105+
dd();
106+
});
107+
108+
it('retrieves transactions of last x seconds', function () {
109+
// expect(Transaction::ofJustNow()->get())->toHaveCount(1);
110+
// expect(Transaction::ofLastSecond()->get())->toHaveCount(1);
111+
// expect(Transaction::ofLast15Seconds(DateRange::INCLUSIVE)->get())->toHaveCount(2);
112+
// expect(Transaction::ofLast15Seconds(DateRange::EXCLUSIVE)->get())->toHaveCount(2);
113+
114+
115+
// Transaction::ofToday()->get();
116+
// Transaction::ofYesterday()->get();
117+
// Transaction::ofLast7Days()->get();
118+
});
119+
//
120+
it('covers all cases', function () {
121+
//TODO
122+
// Write tests for all kind of scopes
123+
// Create a db with fixed date values
124+
// and test with: Carbon::setTestNow('2023-01-01');
125+
expect(true)->toBeTrue();
126+
});

tests/ExampleTest.php

-9
This file was deleted.

tests/Pest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
use Illuminate\Foundation\Testing\RefreshDatabase;
34
use LaracraftTech\LaravelDateScopes\Tests\TestCase;
45

5-
uses(TestCase::class)->in(__DIR__);
6+
uses(TestCase::class, RefreshDatabase::class)->beforeEach(function () {
7+
ray()->clearAll();
8+
})->in(__DIR__);

0 commit comments

Comments
 (0)