-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60406ab
commit 7fde7a1
Showing
10 changed files
with
269 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\DateTime; | ||
|
||
use DateTime; | ||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\Model\Casts\CastInterface; | ||
use Mongolid\Model\Casts\Exceptions\InvalidTypeException; | ||
use Mongolid\Util\LocalDateTime; | ||
|
||
class BackedEnumCast implements CastInterface | ||
{ | ||
/** | ||
* @param class-string<\BackedEnum> $backedEnum | ||
*/ | ||
public function __construct( | ||
private string $backedEnum | ||
) { | ||
} | ||
|
||
public function get(mixed $value): mixed | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
return ($this->backedEnum)::from($value); | ||
} | ||
|
||
public function set(mixed $value): mixed | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof $this->backedEnum) { | ||
throw new InvalidTypeException("$this->backedEnum|null", $value); | ||
} | ||
|
||
return $value->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
use Mongolid\Model\Casts\CastResolver; | ||
|
||
class InvalidTypeException extends InvalidArgumentException | ||
{ | ||
public function __construct(string $expectedType, mixed $value) | ||
{ | ||
$invalidType = is_object($value) | ||
? $value::class | ||
: gettype($value); | ||
|
||
parent::__construct("Value expected type $expectedType, given $invalidType"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Integration; | ||
|
||
use Mongolid\Tests\Stubs\Box; | ||
use Mongolid\Tests\Stubs\Legacy\Box as LegacyBox; | ||
use Mongolid\Tests\Stubs\Size; | ||
|
||
/** | ||
* @requires PHP >= 8.1 | ||
*/ | ||
class BackedEnumCastTest extends IntegrationTestCase | ||
{ | ||
public function testShouldCreateAndSaveBoxWithCastedAttributes(): void | ||
{ | ||
// Set | ||
$box = new Box(); | ||
$box->box_size = Size::Big; | ||
|
||
// Actions | ||
$box->save(); | ||
|
||
// Assertions | ||
$this->assertEquals( Size::Big, $box->boxSize); | ||
$this->assertEquals( 'big', $box->getOriginalDocumentAttributes()['box_size']); | ||
} | ||
|
||
public function testShouldUpdateBoxWithCastedAttributes(): void | ||
{ | ||
// Set | ||
$box = new Box(); | ||
$box->box_size = Size::Small; | ||
|
||
// Actions | ||
$box->update(); | ||
|
||
// Assertions | ||
$this->assertEquals( Size::Small, $box->boxSize); | ||
$this->assertEquals( 'small', $box->getOriginalDocumentAttributes()['box_size']); | ||
} | ||
|
||
public function testShouldCreateAndSaveLegacyBoxWithCastedAttributes(): void | ||
{ | ||
// Set | ||
$legacyBox = new LegacyBox(); | ||
$legacyBox->box_size = Size::Big; | ||
|
||
// Actions | ||
$legacyBox->save(); | ||
|
||
// Assertions | ||
$this->assertEquals( Size::Big, $legacyBox->boxSize); | ||
$this->assertEquals( 'big', $legacyBox->getOriginalDocumentAttributes()['box_size']); | ||
} | ||
|
||
public function testShouldUpdateLegacyBoxWithCastedAttributes(): void | ||
{ | ||
// Set | ||
$legacyBox = new LegacyBox(); | ||
$legacyBox->box_size = Size::Small; | ||
|
||
// Actions | ||
$legacyBox->save(); | ||
|
||
// Assertions | ||
$this->assertEquals( Size::Small, $legacyBox->boxSize); | ||
$this->assertEquals( 'small', $legacyBox->getOriginalDocumentAttributes()['box_size']); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Stubs; | ||
|
||
use Mongolid\Model\AbstractModel; | ||
|
||
class Box extends AbstractModel | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $collection = 'boxes'; | ||
|
||
protected $casts = [ | ||
'box_size' => Size::class, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Stubs\Legacy; | ||
|
||
use Mongolid\LegacyRecord; | ||
|
||
class Box extends LegacyRecord | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $collection = 'boxes'; | ||
|
||
protected $casts = [ | ||
'box_size' => Size::class, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Stubs; | ||
|
||
enum Size: string | ||
{ | ||
case Small = 'small'; | ||
case Big = 'big'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts; | ||
|
||
use Mongolid\Model\Casts\DateTime\BackedEnumCast; | ||
use Mongolid\Model\Casts\Exceptions\InvalidTypeException; | ||
use Mongolid\TestCase; | ||
use Mongolid\Tests\Stubs\Size; | ||
use ValueError; | ||
|
||
/** | ||
* @requires PHP >= 8.1 | ||
*/ | ||
class BackedEnumCastTest extends TestCase | ||
{ | ||
public function testShouldGetValue(): void | ||
{ | ||
// Set | ||
$cast = new BackedEnumCast(Size::class); | ||
|
||
// Actions | ||
$result = $cast->get('small'); | ||
|
||
// Asserts | ||
$this->assertEquals(Size::Small, $result); | ||
} | ||
|
||
public function testShouldGetNull(): void | ||
{ | ||
// Set | ||
$cast = new BackedEnumCast(Size::class); | ||
|
||
// Actions | ||
$result = $cast->get(null); | ||
|
||
// Asserts | ||
$this->assertNull($result); | ||
} | ||
|
||
public function testShoulThrowErrorWhenGetWithUnknownValue(): void | ||
{ | ||
// Set | ||
$cast = new BackedEnumCast(Size::class); | ||
|
||
// Expects | ||
$this->expectException(ValueError::class); | ||
|
||
// Actions | ||
$cast->get('unknow value'); | ||
} | ||
|
||
public function testShouldSet(): void | ||
{ | ||
// Set | ||
$cast = new BackedEnumCast(Size::class); | ||
|
||
// Actions | ||
$result = $cast->set(Size::Big); | ||
|
||
// Asserts | ||
$this->assertEquals('big', $result); | ||
} | ||
|
||
public function testShouldSetNull(): void | ||
{ | ||
// Set | ||
$cast = new BackedEnumCast(Size::class); | ||
|
||
// Actions | ||
$result = $cast->set(null); | ||
|
||
// Asserts | ||
$this->assertNull($result); | ||
} | ||
|
||
public function testShouldThrowErrorWhenSetWithUnknownValue(): void | ||
{ | ||
// Set | ||
$cast = new BackedEnumCast(Size::class); | ||
|
||
// Expectations | ||
$this->expectException(InvalidTypeException::class); | ||
$this->expectExceptionMessage('Value expected type ' . Size::class . '|null, given string'); | ||
|
||
// Actions | ||
$cast->set('unknow value'); | ||
} | ||
} |