Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Sep 23, 2024
1 parent b82df10 commit 9b5f53d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/Unit/Models/ModelAttributeCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use LdapRecord\Models\Model;
use LdapRecord\Query\Collection;
use LdapRecord\Tests\TestCase;
use RuntimeException;

class ModelAttributeCastTest extends TestCase
{
Expand Down Expand Up @@ -200,6 +201,27 @@ public function test_windows_int_datetime_attributes_are_casted()
$model->windowsIntDateTime = new \DateTime('2020-10-02 02:22:09');
$this->assertEquals($model->getRawAttribute('windowsIntDateTime'), ['132460789290000000']);
}

public function test_get_dates()
{
$dates = (new ModelCastStub)->getDates();

$this->assertEquals([
'createtimestamp' => 'ldap',
'modifytimestamp' => 'ldap',
'ldapdatetime' => 'ldap',
'windowsdatetime' => 'windows',
'windowsintdatetime' => 'windows-int',
], $dates);
}

public function test_get_dates_throws_exception_when_no_format_is_provided()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("Invalid date cast [datetime]. A date cast must be in the format 'datetime:format'.");

(new ModelDateCastWithNoFormatStub)->getDates();
}
}

class ModelCastStub extends Model
Expand All @@ -223,3 +245,10 @@ class ModelCastStub extends Model
'windowsIntDateTime' => 'datetime:windows-int',
];
}

class ModelDateCastWithNoFormatStub extends Model
{
protected array $casts = [
'attribute' => 'datetime',
];
}

0 comments on commit 9b5f53d

Please sign in to comment.