Skip to content

Commit

Permalink
unit tests and century scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairahcaz committed Apr 15, 2023
1 parent b756e3d commit acb0aee
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 90 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ Transaction::ofLastDecade(); // query transactions created during the last decad
Transaction::ofLastDecades(2); // query transactions created during the last N decades
```

### Centuries

```php
// query by CENTURIES
Transaction::ofLastCentury(); // query transactions created during the last century
Transaction::ofLastCenturies(2); // query transactions created during the last N centuries
```

### Millenniums

```php
Expand All @@ -222,16 +230,17 @@ Transaction::ofLastMillenniums(2); // query transactions created during the last

```php
// query by toNow/toDate
Transaction::secondToNow(); // query transactions created during the start of the current second to now (equivalent of just now)
Transaction::minuteToNow(); // query transactions created during the start of the current minute to now
Transaction::hourToNow(); // query transactions created during the start of the current hour to now
Transaction::dayToNow(); // query transactions created during the start of the current day to now
Transaction::weekToDate(); // query transactions created during the start of the current week to now
Transaction::monthToDate(); // query transactions created during the start of the current month to now
Transaction::quarterToDate(); // query transactions created during the start of the current quarter to now
Transaction::yearToDate(); // query transactions created during the start of the current year to now
Transaction::decadeToDate(); // query transactions created during the start of the current decade to now
Transaction::millenniumToDate(); // query transactions created during the start of the current millennium to now
Transaction::secondToNow(); // query transactions created during the start of the current second till now (equivalent of just now)
Transaction::minuteToNow(); // query transactions created during the start of the current minute till now
Transaction::hourToNow(); // query transactions created during the start of the current hour till now
Transaction::dayToNow(); // query transactions created during the start of the current day till now
Transaction::weekToDate(); // query transactions created during the start of the current week till now
Transaction::monthToDate(); // query transactions created during the start of the current month till now
Transaction::quarterToDate(); // query transactions created during the start of the current quarter till now
Transaction::yearToDate(); // query transactions created during the start of the current year till now
Transaction::decadeToDate(); // query transactions created during the start of the current decade till now
Transaction::decadeToCentury(); // query transactions created during the start of the current century till now
Transaction::millenniumToDate(); // query transactions created during the start of the current millennium till now
```

## Testing
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
],
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.13.0",
"illuminate/contracts": "^9.0 || ^10.0",
"illuminate/database": "^9.0 || ^10.0",
"illuminate/support": "^9.0 || ^10.0"
"illuminate/support": "^9.0 || ^10.0",
"nesbot/carbon": "^2.66",
"spatie/laravel-package-tools": "^1.13.0"
},
"require-dev": {
"nunomaduro/larastan": "^2.0.1",
Expand Down
15 changes: 13 additions & 2 deletions src/DateScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ trait DateScopes
'second',
'minute',
'hour',
'day'
'day',
'week'
];

/**
Expand Down Expand Up @@ -55,7 +56,8 @@ public function scopeOfLastUnit(Builder $query, string $dateUnit, int $value, Da
];
}

//dump(collect($range)->transform(fn ($item) => $item->format('Y-m-d H:i:s'))->toArray());
// if (defined('DATE_SCOPE_DEBUG'))
// dd(collect($range)->transform(fn ($item) => $item->format('Y-m-d H:i:s'))->toArray());

return $query->whereBetween(config('date-scopes.created_column'), $range);
}
Expand Down Expand Up @@ -160,6 +162,14 @@ public function scopeOfLastDecades(Builder $query, int $decades, DateRange $cust
return $query->ofLastUnit('decade', $decades, $customRange);
}

// START CENTURIES
public function scopeOfLastCentury(Builder $query): Builder {return $query->ofLastCenturies(1, DateRange::EXCLUSIVE);}

public function scopeOfLastCenturies(Builder $query, int $centuries, DateRange $customRange = null): Builder
{
return $query->ofLastUnit('century', $centuries, $customRange);
}

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

Expand All @@ -179,5 +189,6 @@ public function scopeMonthToDate(Builder $query): Builder {return $query->ofLast
public function scopeQuarterToDate(Builder $query): Builder {return $query->ofLastQuarters(1, DateRange::INCLUSIVE);}
public function scopeYearToDate(Builder $query): Builder {return $query->ofLastYears(1, DateRange::INCLUSIVE);}
public function scopeDecadeToDate(Builder $query): Builder {return $query->ofLastDecades(1, DateRange::INCLUSIVE);}
public function scopeCenturyToDate(Builder $query): Builder {return $query->ofLastCenturies(1, DateRange::INCLUSIVE);}
public function scopeMillenniumToDate(Builder $query): Builder {return $query->ofLastMillenniums(1, DateRange::INCLUSIVE);}
}
228 changes: 152 additions & 76 deletions tests/DataScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,44 @@

function getCreatedAtValues(): array
{
$start = '2023-03-31 13:15:15';
Carbon::setTestNow(Carbon::parse($start));

return [
// SECONDS
["created_at" => "2023-03-31 13:15:15"], // query transactions created just now
["created_at" => "2023-03-31 13:16:15"], // query transactions created during the last second
["created_at" => "2023-03-31 13:16:00"], // query transactions created during the last 15 seconds
["created_at" => "2023-03-24 00:00:00"], // query transactions created during the last 30 seconds
["created_at" => "2023-03-17 00:00:00"], // query transactions created during the last 45 seconds
["created_at" => "2023-01-13 00:00:00"], // query transactions created during the last 60 seconds
["created_at" => "2022-04-06 00:00:00"], // query transactions created during the last N seconds
// MINUTES
["created_at" => "2022-06-12 00:00:00"], // query transactions created during the last minute
["created_at" => "1970-12-07 00:00:00"], // query transactions created during the last 15 minutes
["created_at" => "2002-03-18 00:00:00"], // query transactions created during the last 30 minutes
["created_at" => "2005-05-12 00:00:00"], // query transactions created during the last 45 minutes
["created_at" => "1995-03-26 00:00:00"], // query transactions created during the last 60 minutes
["created_at" => "1980-08-19 00:00:00"], // query transactions created during the last N minutes
// HOURS
["created_at" => "2002-02-27 00:00:00"], // query transactions created during the last hour
["created_at" => "1993-04-29 00:00:00"], // query transactions created during the last 6 hours
["created_at" => "2000-12-12 00:00:00"], // query transactions created during the last 12 hours
["created_at" => "1978-01-28 00:00:00"], // query transactions created during the last 18 hours
["created_at" => "2001-02-11 00:00:00"], // query transactions created during the last 24 hours
["created_at" => "1977-04-19 00:00:00"], // query transactions created during the last N hours
// DAYS
["created_at" => "1982-01-30 00:00:00"], // query transactions created today
["created_at" => "1985-08-28 00:00:00"], // query transactions created yesterday
["created_at" => "1973-10-15 00:00:00"], // query transactions created during the last 7 days
["created_at" => "2008-11-25 00:00:00"], // query transactions created during the last 21 days
["created_at" => "2020-09-20 00:00:00"], // query transactions created during the last 30 days
["created_at" => "1988-03-18 00:00:00"], // query transactions created during the last N days
// WEEKS
["created_at" => "2004-06-19 00:00:00"], // query transactions created during the last week
["created_at" => "1978-12-01 00:00:00"], // query transactions created during the last 2 weeks
["created_at" => "1983-07-09 00:00:00"], // query transactions created during the last 3 weeks
["created_at" => "2006-05-25 00:00:00"], // query transactions created during the last 4 weeks
["created_at" => "2006-06-21 00:00:00"], // query transactions created during the last N weeks
//MONTHS
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last month
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last 3 months
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the last 6 months
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last 9 months
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last 12 months
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the last N months
//QUARTERS
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last quarter
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last 2 quarters
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the last 3 quarters
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last 4 quarters
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last N quarters
//YEARS
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last year
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last N years
//DECADES
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last year
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last N years
//MILLENNIUMS
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the last year
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the last N years
//toNow/toDate
["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)
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the start of the current minute to now
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the start of the current hour to now
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the start of the current day to now
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the start of the current week to now
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the start of the current month to now
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the start of the current quarter to now
["created_at" => "1987-06-07 00:00:00"], // query transactions created during the start of the current year to now
["created_at" => "2015-04-29 00:00:00"], // query transactions created during the start of the current decade to now
["created_at" => "1981-11-28 00:00:00"], // query transactions created during the start of the current millennium to
['created_at' => $start],
['created_at' => '2023-03-31 13:15:14'],
['created_at' => '2023-03-31 13:15:00'],
['created_at' => '2023-03-31 13:13:15'],
['created_at' => '2023-03-31 13:14:45'],
['created_at' => '2023-03-31 13:14:30'],
['created_at' => '2023-03-31 13:14:15'],
['created_at' => '2023-03-31 12:45:00'],
['created_at' => '2023-03-31 12:30:00'],
['created_at' => '2023-03-31 12:15:00'],
['created_at' => '2023-03-31 11:15:00'],
['created_at' => '2023-03-31 01:00:00'],
['created_at' => '2023-03-30 19:00:00'],
['created_at' => '2023-03-30 13:00:00'],
['created_at' => '2023-03-29 13:00:00'],
['created_at' => '2023-03-20 00:00:00'],
['created_at' => '2023-03-17 00:00:00'],
['created_at' => '2023-03-10 00:00:00'],
['created_at' => '2023-03-01 00:00:00'],
['created_at' => '2023-02-11 00:00:00'],
['created_at' => '2022-12-01 00:00:00'],
['created_at' => '2022-09-01 00:00:00'],
['created_at' => '2022-06-01 00:00:00'],
['created_at' => '2022-03-01 00:00:00'],
['created_at' => '2021-03-01 00:00:00'],
['created_at' => '2010-01-01 00:00:00'],
['created_at' => '2000-01-01 00:00:00'],
['created_at' => '1801-01-01 00:00:00'],
['created_at' => '0001-01-01 00:00:00'],
];
}

beforeEach(function () {
// Set a fixed date and time for our tests (start on overflow date!)
Carbon::setTestNow(Carbon::create(2023, 3, 31, 13, 15, 15));
config(['date-scopes.default_range' => DateRange::EXCLUSIVE->value]);

Schema::create('transactions', function (Blueprint $blueprint) {
$blueprint->id();
Expand All @@ -100,10 +63,123 @@ function getCreatedAtValues(): array
->create();
});

it('covers all cases', function () {
//TODO
// Write tests for all kind of scopes
// Create a db with fixed date values
// and test with: Carbon::setTestNow('2023-01-01');
expect(true)->toBeTrue();
it('retrieves transactions of last seconds', function () {
expect(Transaction::ofJustNow()->get())->toHaveCount(1);
expect(Transaction::ofLastSecond()->get())->toHaveCount(1);
expect(Transaction::ofLast15Seconds()->get())->toHaveCount(2);
expect(Transaction::ofLast30Seconds()->get())->toHaveCount(3);
expect(Transaction::ofLast45Seconds()->get())->toHaveCount(4);
expect(Transaction::ofLast60Seconds()->get())->toHaveCount(5);
expect(Transaction::ofLastSeconds(120)->get())->toHaveCount(6);
});

it('retrieves transactions of last minutes', function () {
expect(Transaction::ofLastMinute()->get())->toHaveCount(3);
expect(Transaction::ofLast15Minutes()->get())->toHaveCount(4);
expect(Transaction::ofLast30Minutes()->get())->toHaveCount(5);
expect(Transaction::ofLast45Minutes()->get())->toHaveCount(6);
expect(Transaction::ofLast60Minutes()->get())->toHaveCount(7);
expect(Transaction::ofLastMinutes(120)->get())->toHaveCount(8);
});

it('retrieves transactions of last hours', function () {
expect(Transaction::ofLastHour()->get())->toHaveCount(3);
expect(Transaction::ofLast6Hours()->get())->toHaveCount(4);
expect(Transaction::ofLast12Hours()->get())->toHaveCount(5);
expect(Transaction::ofLast18Hours()->get())->toHaveCount(6);
expect(Transaction::ofLast24Hours()->get())->toHaveCount(7);
expect(Transaction::ofLastHours(48)->get())->toHaveCount(8);
});

it('retrieves transactions of last days', function () {
expect(Transaction::ofToday()->get())->toHaveCount(12);
expect(Transaction::ofYesterday()->get())->toHaveCount(2);
expect(Transaction::ofLast7Days()->get())->toHaveCount(3);
expect(Transaction::ofLast14Days()->get())->toHaveCount(5);
expect(Transaction::ofLast21Days()->get())->toHaveCount(6);
expect(Transaction::ofLast30Days()->get())->toHaveCount(7);
expect(Transaction::ofLastDays(48)->get())->toHaveCount(8);
});

it('retrieves transactions of last weeks', function () {
expect(Transaction::ofLastWeek()->get())->toHaveCount(1);
expect(Transaction::ofLast2Weeks()->get())->toHaveCount(2);
expect(Transaction::ofLast3Weeks()->get())->toHaveCount(3);
expect(Transaction::ofLast4Weeks()->get())->toHaveCount(4);
expect(Transaction::ofLastWeeks(8)->get())->toHaveCount(5);
});

it('retrieves transactions of last months', function () {
expect(Transaction::ofLastMonth()->get())->toHaveCount(1);
expect(Transaction::ofLast3Months()->get())->toHaveCount(2);
expect(Transaction::ofLast6Months()->get())->toHaveCount(3);
expect(Transaction::ofLast9Months()->get())->toHaveCount(4);
expect(Transaction::ofLast12Months()->get())->toHaveCount(5);
expect(Transaction::ofLastMonths(24)->get())->toHaveCount(6);
});

it('retrieves transactions of last quarters', function () {
expect(Transaction::ofLastQuarter()->get())->toHaveCount(1);
expect(Transaction::ofLast2Quarters()->get())->toHaveCount(2);
expect(Transaction::ofLast3Quarters()->get())->toHaveCount(3);
expect(Transaction::ofLast4Quarters()->get())->toHaveCount(4);
expect(Transaction::ofLastQuarters(8)->get())->toHaveCount(5);
});

it('retrieves transactions of last years', function () {
expect(Transaction::ofLastYear()->get())->toHaveCount(4);
expect(Transaction::ofLastYears(2)->get())->toHaveCount(5);
});

it('retrieves transactions of last decades', function () {
expect(Transaction::ofLastDecade()->get())->toHaveCount(1);
expect(Transaction::ofLastDecades(2)->get())->toHaveCount(2);
});

it('retrieves transactions of last centuries', function () {
expect(Transaction::ofLastCentury()->get())->toHaveCount(1);
expect(Transaction::ofLastCenturies(2)->get())->toHaveCount(2);
});

it('retrieves transactions of last millenniums', function () {
expect(Transaction::ofLastMillennium()->get())->toHaveCount(2);
expect(Transaction::ofLastMillenniums(2)->get())->toHaveCount(3);
});

it('retrieves transactions of toNow/toDate', function () {
expect(Transaction::secondToNow()->get())->toHaveCount(1);
expect(Transaction::minuteToNow()->get())->toHaveCount(3);
expect(Transaction::hourToNow()->get())->toHaveCount(7);
expect(Transaction::dayToNow()->get())->toHaveCount(12);
expect(Transaction::weekToDate()->get())->toHaveCount(15);
expect(Transaction::monthToDate()->get())->toHaveCount(19);
expect(Transaction::quarterToDate()->get())->toHaveCount(20);
expect(Transaction::yearToDate()->get())->toHaveCount(20);
expect(Transaction::decadeToDate()->get())->toHaveCount(25);
expect(Transaction::centuryToDate()->get())->toHaveCount(26);
expect(Transaction::millenniumToDate()->get())->toHaveCount(26);
});

it('retrieves transactions of last x (inclusive config date range)', function () {
config(['date-scopes.default_range' => DateRange::INCLUSIVE->value]);

expect(Transaction::ofLast15Minutes()->get())->toHaveCount(7);
expect(Transaction::ofLast12Hours()->get())->toHaveCount(11);
expect(Transaction::ofLast21Days()->get())->toHaveCount(17);
expect(Transaction::ofLast3Weeks()->get())->toHaveCount(17);
expect(Transaction::ofLast12Months()->get())->toHaveCount(23);
expect(Transaction::ofLastQuarters(8)->get())->toHaveCount(24);
expect(Transaction::ofLastYear()->get())->toHaveCount(4);
expect(Transaction::ofLastDecade()->get())->toHaveCount(1);
expect(Transaction::ofLastCentury()->get())->toHaveCount(1);
expect(Transaction::ofLastMillennium()->get())->toHaveCount(2);
});

it('retrieves transactions of last x (inclusive fluent date range)', function () {
expect(Transaction::ofLast15Minutes(DateRange::INCLUSIVE)->get())->toHaveCount(7);
expect(Transaction::ofLast12Hours(DateRange::INCLUSIVE)->get())->toHaveCount(11);
expect(Transaction::ofLast21Days(DateRange::INCLUSIVE)->get())->toHaveCount(17);
expect(Transaction::ofLast3Weeks(DateRange::INCLUSIVE)->get())->toHaveCount(17);
expect(Transaction::ofLast12Months(DateRange::INCLUSIVE)->get())->toHaveCount(23);
expect(Transaction::ofLastQuarters(8, DateRange::INCLUSIVE)->get())->toHaveCount(24);
});

0 comments on commit acb0aee

Please sign in to comment.