Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
bd9931ea
Commit
bd9931ea
authored
May 09, 2015
by
Valentinas Bartusevičius
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Tests to DateInterval Type
parent
c27dd7bf
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
7 deletions
+88
-7
DateIntervalType.php
lib/Doctrine/DBAL/Types/DateIntervalType.php
+46
-2
Type.php
lib/Doctrine/DBAL/Types/Type.php
+2
-0
DateIntervalTest.php
tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php
+40
-5
No files found.
lib/Doctrine/DBAL/Types/DateIntervalType.php
View file @
bd9931ea
...
...
@@ -3,10 +3,54 @@
namespace
Doctrine\DBAL\Types
;
use
Doctrine\DBAL\Platforms\AbstractPlatform
;
/**
*
*
Type that maps interval string to a PHP DateInterval Object.
*/
class
DateIntervalType
class
DateIntervalType
extends
Type
{
/**
* {@inheritdoc}
*/
public
function
getName
()
{
return
Type
::
DATEINTERVAL
;
}
/**
* {@inheritdoc}
*/
public
function
getSQLDeclaration
(
array
$fieldDeclaration
,
AbstractPlatform
$platform
)
{
return
$platform
->
getVarcharTypeDeclarationSQL
(
$fieldDeclaration
);
}
/**
* {@inheritdoc}
*/
public
function
convertToDatabaseValue
(
$value
,
AbstractPlatform
$platform
)
{
return
(
$value
!==
null
)
?
$value
->
format
(
'P%yY%mM%dDT%hH%iM%sS'
)
:
null
;
}
/**
* {@inheritdoc}
*/
public
function
convertToPHPValue
(
$value
,
AbstractPlatform
$platform
)
{
if
(
$value
===
null
||
$value
instanceof
\DateInterval
)
{
return
$value
;
}
try
{
$interval
=
new
\DateInterval
(
$value
);
}
catch
(
\Exception
$e
)
{
throw
ConversionException
::
conversionFailedFormat
(
$value
,
$this
->
getName
(),
'PxYxMxDTxHxMxS'
);
}
return
$interval
;
}
}
lib/Doctrine/DBAL/Types/Type.php
View file @
bd9931ea
...
...
@@ -52,6 +52,7 @@ abstract class Type
const
BLOB
=
'blob'
;
const
FLOAT
=
'float'
;
const
GUID
=
'guid'
;
const
DATEINTERVAL
=
'dateinterval'
;
/**
* Map of already instantiated type objects. One instance per type (flyweight).
...
...
@@ -85,6 +86,7 @@ abstract class Type
self
::
BINARY
=>
'Doctrine\DBAL\Types\BinaryType'
,
self
::
BLOB
=>
'Doctrine\DBAL\Types\BlobType'
,
self
::
GUID
=>
'Doctrine\DBAL\Types\GuidType'
,
self
::
DATEINTERVAL
=>
'Doctrine\DBAL\Types\DateIntervalType'
,
);
/**
...
...
tests/Doctrine/Tests/DBAL/Types/DateIntervalTest.php
View file @
bd9931ea
<?php
namespace
Doctrine\Tests\DBAL\Types
;
/**
*
*/
class
DateIntervalTest
use
Doctrine\DBAL\Types\Type
;
use
Doctrine\Tests\DBAL\Mocks\MockPlatform
;
class
DateIntervalTest
extends
\Doctrine\Tests\DbalTestCase
{
protected
$_platform
,
$_type
;
protected
function
setUp
()
{
$this
->
_platform
=
new
MockPlatform
();
$this
->
_type
=
Type
::
getType
(
'dateinterval'
);
}
public
function
testDateIntervalConvertsToDatabaseValue
()
{
$interval
=
new
\DateInterval
(
'P1DT1H2M3S'
);
$expected
=
$interval
->
format
(
'P%yY%mM%dDT%hH%iM%sS'
);
$actual
=
$this
->
_type
->
convertToDatabaseValue
(
$interval
,
$this
->
_platform
);
$this
->
assertEquals
(
$expected
,
$actual
);
}
public
function
testDateIntervalConvertsToPHPValue
()
{
$date
=
$this
->
_type
->
convertToPHPValue
(
'P1DT1H2M3S'
,
$this
->
_platform
);
$this
->
assertInstanceOf
(
'DateInterval'
,
$date
);
$this
->
assertEquals
(
'P0Y0M1DT1H2M3S'
,
$date
->
format
(
'P%yY%mM%dDT%hH%iM%sS'
));
}
public
function
testInvalidDateIntervalFormatConversion
()
{
$this
->
setExpectedException
(
'Doctrine\DBAL\Types\ConversionException'
);
$this
->
_type
->
convertToPHPValue
(
'abcdefg'
,
$this
->
_platform
);
}
public
function
testDateIntervalNullConversion
()
{
$this
->
assertNull
(
$this
->
_type
->
convertToPHPValue
(
null
,
$this
->
_platform
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment