-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
11 changed files
with
336 additions
and
127 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,67 @@ | ||
<?php | ||
|
||
namespace Elastic\TimeInterval\Database\Type; | ||
|
||
use Cake\Database\Driver; | ||
use Cake\Database\Type; | ||
use Elastic\TimeInterval\ValueObject\TimeInterval; | ||
use Exception; | ||
use PDO; | ||
use UnexpectedValueException; | ||
|
||
/** | ||
* TimeInterval custom type for INTEGER column | ||
* | ||
* @link http://book.cakephp.org/3.0/en/orm/database-basics.html#adding-custom-database-types | ||
*/ | ||
class TimeIntervalAsIntType extends Type | ||
{ | ||
use TimeIntervalMarshalTrait; | ||
|
||
/** | ||
* @param mixed $value the value from database | ||
* @param Driver $driver db driver | ||
* @return mixed|null | ||
* @throws Exception | ||
*/ | ||
public function toPHP($value, Driver $driver) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
return TimeInterval::createFromSeconds($value); | ||
} | ||
|
||
/** | ||
* @param mixed $value the value to database | ||
* @param Driver $driver db driver | ||
* @return false|mixed|string | ||
* @throws UnexpectedValueException | ||
* @throws Exception | ||
*/ | ||
public function toDatabase($value, Driver $driver) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof TimeInterval) { | ||
$value = $this->marshal($value); | ||
} | ||
|
||
return $value->toSeconds(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function toStatement($value, Driver $driver) | ||
{ | ||
if ($value === null) { | ||
return PDO::PARAM_NULL; | ||
} | ||
|
||
return PDO::PARAM_INT; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Elastic\TimeInterval\Database\Type; | ||
|
||
use DateInterval; | ||
use Elastic\TimeInterval\ValueObject\TimeInterval; | ||
use Exception; | ||
use UnexpectedValueException; | ||
|
||
trait TimeIntervalMarshalTrait | ||
{ | ||
/** | ||
* @param mixed $value the value | ||
* @return mixed | ||
* @throws UnexpectedValueException | ||
* @throws Exception | ||
*/ | ||
public function marshal($value) | ||
{ | ||
if ($value === null || $value instanceof TimeInterval) { | ||
return $value; | ||
} | ||
|
||
if ($value instanceof DateInterval) { | ||
return TimeInterval::createFromDateInterval($value); | ||
} | ||
|
||
if (is_numeric($value)) { | ||
return TimeInterval::createFromSeconds($value); | ||
} | ||
|
||
if (is_string($value)) { | ||
return TimeInterval::createFromString($value); | ||
} | ||
|
||
throw new UnexpectedValueException('Invalid interval 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
120 changes: 120 additions & 0 deletions
120
tests/TestCase/Database/Type/BaseTimeIntervalTypeTest.php
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,120 @@ | ||
<?php | ||
|
||
namespace Elastic\TimeInterval\Test\TestCase\Database\Type; | ||
|
||
use Cake\Database\Driver; | ||
use Cake\Database\Type; | ||
use Cake\I18n\FrozenTime; | ||
use Cake\TestSuite\TestCase; | ||
use Elastic\TimeInterval\Database\Type\TimeIntervalAsIntType; | ||
use Elastic\TimeInterval\Database\Type\TimeIntervalType; | ||
use Elastic\TimeInterval\ValueObject\TimeInterval; | ||
|
||
abstract class BaseTimeIntervalTypeTest extends TestCase | ||
{ | ||
/** | ||
* @var Type|TimeIntervalType|TimeIntervalAsIntType | ||
*/ | ||
protected $type; | ||
|
||
/** | ||
* @var Driver|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $driver; | ||
|
||
public function tearDown() | ||
{ | ||
unset($this->type, $this->driver); | ||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* test convert DB to PHP | ||
* | ||
* @dataProvider dataToPHP | ||
*/ | ||
public function testToPHP($database, $expected) | ||
{ | ||
$result = $this->type->toPHP($database, $this->driver); | ||
$this->assertInstanceOf(TimeInterval::class, $result); | ||
$this->assertSame($expected, (string)$result); | ||
} | ||
|
||
/** | ||
* data for testToPHP | ||
* | ||
* @return array | ||
*/ | ||
abstract public function dataToPHP(); | ||
|
||
/** | ||
* test convert null value to PHP | ||
*/ | ||
public function testToPHPWithNull() | ||
{ | ||
$this->assertNull($this->type->toPHP(null, $this->driver)); | ||
} | ||
|
||
/** | ||
* test convert value | ||
* | ||
* @dataProvider dataMarshal | ||
*/ | ||
public function testMarshal($value, $expected) | ||
{ | ||
$result = $this->type->marshal($value); | ||
$this->assertInstanceOf(TimeInterval::class, $result); | ||
$this->assertSame($expected, (string)$result); | ||
} | ||
|
||
/** | ||
* data for testMarshal | ||
* | ||
* @return array | ||
*/ | ||
public function dataMarshal() | ||
{ | ||
$a = new FrozenTime('2019-01-01 00:00:00'); | ||
$b = new FrozenTime('2019-01-02 02:15:01'); | ||
$c = new FrozenTime('2019-01-31 00:00:00'); | ||
$d = new FrozenTime('2019-03-01 02:15:01'); | ||
|
||
return [ | ||
['00:00:01', '00:00:01'], | ||
['00:15:01', '00:15:01'], | ||
['25:15:01', '25:15:01'], | ||
['-25:15:01', '-25:15:01'], | ||
['25:15', '25:15:00'], | ||
['-25:15', '-25:15:00'], | ||
[new \DateInterval('PT25H15M1S'), '25:15:01'], | ||
[new \DateInterval('P1DT2H15M1S'), '26:15:01'], | ||
[$a->diff($b), '26:15:01'], | ||
[$c->diff($d), '698:15:01'], | ||
[0, '00:00:00'], | ||
[1, '00:00:01'], | ||
[-1, '-00:00:01'], | ||
[3599, '00:59:59'], | ||
[-3599, '-00:59:59'], | ||
[86401, '24:00:01'], | ||
[-86401, '-24:00:01'], | ||
]; | ||
} | ||
|
||
/** | ||
* test convert null value | ||
*/ | ||
public function testMarshalWithNull() | ||
{ | ||
$this->assertNull($this->type->marshal(null)); | ||
} | ||
|
||
/** | ||
* test convert PHP to DB | ||
*/ | ||
abstract public function testToDatabase(); | ||
|
||
/** | ||
* test get statement | ||
*/ | ||
abstract public function testToStatement(); | ||
} |
57 changes: 57 additions & 0 deletions
57
tests/TestCase/Database/Type/TimeIntervalAsIntTypeTest.php
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,57 @@ | ||
<?php | ||
|
||
namespace Elastic\TimeInterval\Test\TestCase\Database\Type; | ||
|
||
use Cake\Database\Driver; | ||
use Elastic\TimeInterval\Database\Type\TimeIntervalAsIntType; | ||
use Elastic\TimeInterval\ValueObject\TimeInterval; | ||
use PDO; | ||
|
||
class TimeIntervalAsIntTypeTest extends BaseTimeIntervalTypeTest | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->type = new TimeIntervalAsIntType(); | ||
$this->driver = $this->getMockForAbstractClass(Driver::class); | ||
} | ||
|
||
/** | ||
* data for testToPHP | ||
* | ||
* @return array | ||
*/ | ||
public function dataToPHP() | ||
{ | ||
return [ | ||
[0, '00:00:00'], | ||
[1, '00:00:01'], | ||
[901, '00:15:01'], | ||
[90901, '25:15:01'], | ||
[-90901, '-25:15:01'], | ||
]; | ||
} | ||
|
||
/** | ||
* test convert PHP to DB | ||
*/ | ||
public function testToDatabase() | ||
{ | ||
$this->assertNull($this->type->toDatabase(null, $this->driver)); | ||
$this->assertSame(1, $this->type->toDatabase(TimeInterval::createFromString('00:00:01'), $this->driver)); | ||
$this->assertSame(1, $this->type->toDatabase(TimeInterval::createFromSeconds(1), $this->driver)); | ||
$this->assertSame(86400, $this->type->toDatabase(TimeInterval::createFromSeconds(86400), $this->driver)); | ||
$this->assertSame(86401, $this->type->toDatabase(TimeInterval::createFromSeconds(86401), $this->driver)); | ||
$this->assertSame(0, $this->type->toDatabase(TimeInterval::createFromSeconds(0), $this->driver)); | ||
$this->assertSame(-86401, $this->type->toDatabase(TimeInterval::createFromSeconds(-86401), $this->driver)); | ||
} | ||
|
||
/** | ||
* test get statement | ||
*/ | ||
public function testToStatement() | ||
{ | ||
$this->assertSame(PDO::PARAM_NULL, $this->type->toStatement(null, $this->driver)); | ||
$this->assertSame(PDO::PARAM_INT, $this->type->toStatement(TimeInterval::createFromSeconds(1), $this->driver)); | ||
} | ||
} |
Oops, something went wrong.