-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#869 cleanups and hardening of tests around date-related types #902
Merged
billschaller
merged 22 commits into
doctrine:master
from
Ocramius:hotfix/#869-fixes-for-type-conversion-excessive-smartness
Sep 6, 2015
Merged
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9f72045
#869 - DBAL-1293 - simplified conversion logic for readability
Ocramius 35a4dfb
#869 - DBAL-1293 - simplified conversion logic for readability (TimeT…
Ocramius 7e80ad8
#869 - DBAL-1293 - Correcting tests around the invalid date/time form…
Ocramius e5202c0
#869 - DBAL-1293 - Using a data provider for invalid type conversions
Ocramius b6d34a6
#869 - DBAL-1293 - Adding tests for array values
Ocramius 4e42e4c
#869 - DBAL-1293 - converting all tests to use a data provider
Ocramius 00595fc
#869 - DBAL-1293 - `assertInternalType` over `assertTrue(is_string())`
Ocramius bc0a4a4
#869 - DBAL-1293 - restoring logic around summer time tests
Ocramius 1a6a796
#869 - DBAL-1293 - abstract test case for date-based types
Ocramius ef4bd5d
#869 - DBAL-1293 - removing unused imports
Ocramius dcc18f0
#869 - DBAL-1293 - cleaning up error message generation
Ocramius ae0a205
#869 - DBAL-1293 - aligning datetime types to the new conversion exce…
Ocramius f0534f3
#869 - DBAL-1293 - basic coverage for the conversion exception
Ocramius beb2c7c
#869 - DBAL-1293 - covering remaining conversion exception logic
Ocramius ae67ace
#869 - DBAL-1293 - testing `DateIntervalType` logic with invalid type…
Ocramius 6c867a0
#869 - DBAL-1293 - `DateIntervalType` should throw on invalid types
Ocramius 52fe0e4
#869 - DBAL-1293 - Removing whitespace
Ocramius a348e33
#869 - DBAL-1293 - Previous exceptions should be preserved by the `Co…
Ocramius 5eb3e04
#869 - DBAL-1293 - Preserving exceptions for invalid conversions
Ocramius 73f3d2c
#869 - DBAL-1293 - Removing unused assignment
Ocramius 8bfa9bd
#869 - DBAL-1293 - Correcting class names caused by incorrect refacto…
Ocramius 155f5d7
#869 - DBAL-1293 - Making sure `Type::getType('dateinterval')` retrie…
Ocramius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,94 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\DBAL\Types; | ||
|
||
use Doctrine\Tests\DBAL\Mocks\MockPlatform; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
abstract class BaseDateTypeTestCase extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var MockPlatform | ||
*/ | ||
protected $platform; | ||
|
||
/** | ||
* @var \Doctrine\DBAL\Types\Type | ||
*/ | ||
protected $type; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $currentTimezone; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->platform = new MockPlatform(); | ||
$this->currentTimezone = date_default_timezone_get(); | ||
|
||
$this->assertInstanceOf('Doctrine\DBAL\Types\Type', $this->type); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function tearDown() | ||
{ | ||
date_default_timezone_set($this->currentTimezone); | ||
} | ||
|
||
public function testDateConvertsToDatabaseValue() | ||
{ | ||
$this->assertInternalType('string', $this->type->convertToDatabaseValue(new \DateTime(), $this->platform)); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidPHPValuesProvider | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function testInvalidTypeConversionToDatabaseValue($value) | ||
{ | ||
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); | ||
|
||
$this->type->convertToDatabaseValue($value, $this->platform); | ||
} | ||
|
||
public function testNullConversion() | ||
{ | ||
$this->assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
|
||
public function testConvertDateTimeToPHPValue() | ||
{ | ||
$date = new \DateTime('now'); | ||
|
||
$this->assertSame($date, $this->type->convertToPHPValue($date, $this->platform)); | ||
} | ||
|
||
/** | ||
* @return mixed[][] | ||
*/ | ||
public function invalidPHPValuesProvider() | ||
{ | ||
return [ | ||
[0], | ||
[''], | ||
['foo'], | ||
['10:11:12'], | ||
['2015-01-31'], | ||
['2015-01-31 10:11:12'], | ||
[new \stdClass()], | ||
[$this], | ||
[27], | ||
[-1], | ||
[1.2], | ||
[[]], | ||
[['an array']], | ||
]; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
tests/Doctrine/Tests/DBAL/Types/ConversionExceptionTest.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,83 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Types\ConversionException; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
class ConversionExceptionTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider scalarsProvider | ||
* | ||
* @param mixed $scalarValue | ||
*/ | ||
public function testConversionFailedInvalidTypeWithScalar($scalarValue) | ||
{ | ||
$exception = ConversionException::conversionFailedInvalidType($scalarValue, 'foo', ['bar', 'baz']); | ||
|
||
$this->assertInstanceOf('Doctrine\DBAL\Types\ConversionException', $exception); | ||
$this->assertRegExp( | ||
'/^Could not convert PHP value \'.*\' of type \'(string|boolean|float|double|integer)\' to type \'foo\'. ' | ||
. 'Expected one of the following types: bar, baz$/', | ||
$exception->getMessage() | ||
); | ||
} | ||
/** | ||
* @dataProvider nonScalarsProvider | ||
* | ||
* @param mixed $nonScalar | ||
*/ | ||
public function testConversionFailedInvalidTypeWithNonScalar($nonScalar) | ||
{ | ||
$exception = ConversionException::conversionFailedInvalidType($nonScalar, 'foo', ['bar', 'baz']); | ||
|
||
$this->assertInstanceOf('Doctrine\DBAL\Types\ConversionException', $exception); | ||
$this->assertRegExp( | ||
'/^Could not convert PHP value of type \'(.*)\' to type \'foo\'. ' | ||
. 'Expected one of the following types: bar, baz$/', | ||
$exception->getMessage() | ||
); | ||
} | ||
|
||
public function testConversionFailedFormatPreservesPreviousException() | ||
{ | ||
$previous = new \Exception(); | ||
|
||
$exception = ConversionException::conversionFailedFormat('foo', 'bar', 'baz', $previous); | ||
|
||
$this->assertInstanceOf('Doctrine\DBAL\Types\ConversionException', $exception); | ||
$this->assertSame($previous, $exception->getPrevious()); | ||
} | ||
|
||
/** | ||
* @return mixed[][] | ||
*/ | ||
public function nonScalarsProvider() | ||
{ | ||
return [ | ||
[[]], | ||
[['foo']], | ||
[null], | ||
[$this], | ||
[new \stdClass()], | ||
[tmpfile()], | ||
]; | ||
} | ||
|
||
/** | ||
* @return mixed[][] | ||
*/ | ||
public function scalarsProvider() | ||
{ | ||
return [ | ||
[''], | ||
['foo'], | ||
[123], | ||
[-123], | ||
[12.34], | ||
[true], | ||
[false], | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes vastly more sense than the nonsense I wrote.