-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from nickrupert7/master
Added support for Eloquent toArray()
- Loading branch information
Showing
3 changed files
with
159 additions
and
0 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,129 @@ | ||
<?php | ||
/** | ||
* Contains the EnumAccessorTest class. | ||
* | ||
* @copyright Copyright (c) 2017 Attila Fulop | ||
* @author Attila Fulop | ||
* @license MIT | ||
* @since 2017-10-05 | ||
* | ||
*/ | ||
|
||
namespace Konekt\Enum\Eloquent\Tests; | ||
|
||
use Konekt\Enum\Eloquent\Tests\Models\Order; | ||
use Konekt\Enum\Eloquent\Tests\Models\OrderV2; | ||
use Konekt\Enum\Eloquent\Tests\Models\OrderStatus; | ||
|
||
class EnumToArrayTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function returns_enum_string_value() | ||
{ | ||
$order = new Order([ | ||
'number' => 'abc123', | ||
'status' => OrderStatus::SUBMITTED | ||
]); | ||
|
||
$array = $order->attributesToArray(); | ||
|
||
$this->assertArrayHasKey('status', $array); | ||
$this->assertIsString($array['status']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function still_returns_other_attributes() | ||
{ | ||
$order = new Order([ | ||
'number' => 'abc123', | ||
'status' => OrderStatus::SUBMITTED | ||
]); | ||
|
||
$array = $order->attributesToArray(); | ||
|
||
$this->assertArrayHasKey('number', $array); | ||
$this->assertEquals($array['number'], $order->number); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function to_array_still_works() | ||
{ | ||
$order = new Order([ | ||
'number' => 'abc123', | ||
'status' => OrderStatus::SUBMITTED | ||
]); | ||
|
||
$attributesArray = $order->attributesToArray(); | ||
$array = $order->toArray(); | ||
|
||
$this->assertEquals($array, $attributesArray); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function returns_enum_default_string_value_when_attribute_is_null() | ||
{ | ||
// don't test if major version is lower than 3 | ||
if ($this->getEnumVersionMajor() < 3) { | ||
$this->assertTrue(true); | ||
|
||
return; | ||
} | ||
|
||
$order = new Order([ | ||
'number' => 'abc123' | ||
]); | ||
|
||
$array = $order->attributesToArray(); | ||
|
||
$this->assertArrayHasKey('status', $array); | ||
$this->assertIsString($array['status']); | ||
$this->assertEquals($array['status'], OrderStatus::__DEFAULT); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function returns_enum_v2_default_string_value_when_attribute_is_null() | ||
{ | ||
// don't test if major version is 3 or higher | ||
if ($this->getEnumVersionMajor() >= 3) { | ||
$this->assertTrue(true); | ||
|
||
return; | ||
} | ||
|
||
$order = new OrderV2([ | ||
'number' => 'abc123' | ||
]); | ||
|
||
$array = $order->attributesToArray(); | ||
|
||
$this->assertArrayHasKey('status', $array); | ||
$this->assertIsString($array['status']); | ||
$this->assertEquals($array['status'], OrderStatus::__DEFAULT); | ||
} | ||
|
||
private function getEnumVersion() | ||
{ | ||
$raw_version = \PackageVersions\Versions::getVersion('konekt/enum'); | ||
|
||
$parts = explode('@', $raw_version); | ||
|
||
return $parts[0]; | ||
} | ||
|
||
private function getEnumVersionMajor() | ||
{ | ||
$parts = explode('.', $this->getEnumVersion()); | ||
|
||
return $parts[0]; | ||
} | ||
} |