Skip to content

Commit 854767b

Browse files
committed
tests for custom created_at column name
1 parent 7e5c06b commit 854767b

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace LaracraftTech\LaravelDateScopes\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use LaracraftTech\LaravelDateScopes\Tests\Models\CustomTransaction;
7+
8+
class CustomTransactionFactory extends Factory
9+
{
10+
protected $model = CustomTransaction::class;
11+
12+
public function definition(): array
13+
{
14+
return [
15+
'col1' => fake()->sentence(),
16+
'col2' => fake()->randomNumber(),
17+
'custom_created_at' => fake()->date(),
18+
];
19+
}
20+
}

tests/DataScopesTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Schema\Blueprint;
66
use Illuminate\Support\Carbon;
77
use LaracraftTech\LaravelDateScopes\DateRange;
8+
use LaracraftTech\LaravelDateScopes\Tests\Models\CustomTransaction;
89
use LaracraftTech\LaravelDateScopes\Tests\Models\Transaction;
910

1011
function getCreatedAtValues(): array
@@ -183,3 +184,30 @@ function getCreatedAtValues(): array
183184
->and(Transaction::ofLast12Months(DateRange::INCLUSIVE)->get())->toHaveCount(23)
184185
->and(Transaction::ofLastQuarters(8, DateRange::INCLUSIVE)->get())->toHaveCount(24);
185186
});
187+
188+
it('also works with a custom created_at column name', function() {
189+
Schema::create('custom_transactions', function (Blueprint $blueprint) {
190+
$blueprint->id();
191+
$blueprint->string('col1');
192+
$blueprint->integer('col2');
193+
$blueprint->timestamp('custom_created_at')->nullable();
194+
});
195+
196+
$start = '2023-03-31 13:15:15';
197+
Carbon::setTestNow(Carbon::parse($start));
198+
199+
$createdAtValues = [
200+
['custom_created_at' => '2023-03-31 13:05:14'],
201+
['custom_created_at' => '2022-03-31 13:15:00'],
202+
['custom_created_at' => '2013-03-31 13:13:15']
203+
];
204+
205+
CustomTransaction::factory()
206+
->count(count($createdAtValues))
207+
->state(new Sequence(...$createdAtValues))
208+
->create();
209+
210+
expect(CustomTransaction::ofLast15Minutes()->get())->toHaveCount(1)
211+
->and(CustomTransaction::ofLastYear()->get())->toHaveCount(1)
212+
->and(CustomTransaction::ofLastDecade()->get())->toHaveCount(1);
213+
});

tests/Models/CustomTransaction.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace LaracraftTech\LaravelDateScopes\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use LaracraftTech\LaravelDateScopes\DateScopes;
8+
9+
class CustomTransaction extends Model
10+
{
11+
use HasFactory, DateScopes;
12+
13+
public $timestamps = false;
14+
15+
const CREATED_AT = 'custom_created_at';
16+
}

0 commit comments

Comments
 (0)