|
| 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 | +}); |
0 commit comments