Skip to content

Commit

Permalink
tests for custom created_at column name
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairahcaz committed May 2, 2023
1 parent 7e5c06b commit 854767b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions database/factories/CustomTransactionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace LaracraftTech\LaravelDateScopes\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use LaracraftTech\LaravelDateScopes\Tests\Models\CustomTransaction;

class CustomTransactionFactory extends Factory
{
protected $model = CustomTransaction::class;

public function definition(): array
{
return [
'col1' => fake()->sentence(),
'col2' => fake()->randomNumber(),
'custom_created_at' => fake()->date(),
];
}
}
28 changes: 28 additions & 0 deletions tests/DataScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use LaracraftTech\LaravelDateScopes\DateRange;
use LaracraftTech\LaravelDateScopes\Tests\Models\CustomTransaction;
use LaracraftTech\LaravelDateScopes\Tests\Models\Transaction;

function getCreatedAtValues(): array
Expand Down Expand Up @@ -183,3 +184,30 @@ function getCreatedAtValues(): array
->and(Transaction::ofLast12Months(DateRange::INCLUSIVE)->get())->toHaveCount(23)
->and(Transaction::ofLastQuarters(8, DateRange::INCLUSIVE)->get())->toHaveCount(24);
});

it('also works with a custom created_at column name', function() {
Schema::create('custom_transactions', function (Blueprint $blueprint) {
$blueprint->id();
$blueprint->string('col1');
$blueprint->integer('col2');
$blueprint->timestamp('custom_created_at')->nullable();
});

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

$createdAtValues = [
['custom_created_at' => '2023-03-31 13:05:14'],
['custom_created_at' => '2022-03-31 13:15:00'],
['custom_created_at' => '2013-03-31 13:13:15']
];

CustomTransaction::factory()
->count(count($createdAtValues))
->state(new Sequence(...$createdAtValues))
->create();

expect(CustomTransaction::ofLast15Minutes()->get())->toHaveCount(1)
->and(CustomTransaction::ofLastYear()->get())->toHaveCount(1)
->and(CustomTransaction::ofLastDecade()->get())->toHaveCount(1);
});
16 changes: 16 additions & 0 deletions tests/Models/CustomTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace LaracraftTech\LaravelDateScopes\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use LaracraftTech\LaravelDateScopes\DateScopes;

class CustomTransaction extends Model
{
use HasFactory, DateScopes;

public $timestamps = false;

const CREATED_AT = 'custom_created_at';
}

0 comments on commit 854767b

Please sign in to comment.