-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DDC-3863 - Add a proper and unflawed JsonType
- Loading branch information
Showing
4 changed files
with
199 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
|
||
/** | ||
* Type generating json objects values | ||
* | ||
* @since 2.6 | ||
* @author Baptiste Clavié <clavie.b@gmail.com> | ||
*/ | ||
class JsonType extends Type | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) | ||
{ | ||
return $platform->getJsonTypeDeclarationSQL($fieldDeclaration); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform) | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
return json_encode($value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform) | ||
{ | ||
if ($value === null || $value === '') { | ||
return null; | ||
} | ||
|
||
if (is_resource($value)) { | ||
$value = stream_get_contents($value); | ||
} | ||
|
||
$val = json_decode($value, true); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw ConversionException::conversionFailed($value, $this->getName()); | ||
} | ||
|
||
return $val; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return Type::JSON_UNFLAWED; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function requiresSQLCommentHint(AbstractPlatform $platform) | ||
{ | ||
return ! $platform->hasNativeJsonType(); | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Types\Type; | ||
use Doctrine\Tests\DBAL\Mocks\MockPlatform; | ||
|
||
class JsonTest extends \Doctrine\Tests\DbalTestCase | ||
{ | ||
/** | ||
* @var \Doctrine\Tests\DBAL\Mocks\MockPlatform | ||
*/ | ||
protected $platform; | ||
|
||
/** | ||
* @var \Doctrine\DBAL\Types\JsonType | ||
*/ | ||
protected $type; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->platform = new MockPlatform(); | ||
$this->type = Type::getType('json'); | ||
} | ||
|
||
public function testReturnsBindingType() | ||
{ | ||
$this->assertSame(\PDO::PARAM_STR, $this->type->getBindingType()); | ||
} | ||
|
||
public function testReturnsName() | ||
{ | ||
$this->assertSame(Type::JSON_UNFLAWED, $this->type->getName()); | ||
} | ||
|
||
public function testReturnsSQLDeclaration() | ||
{ | ||
$this->assertSame('DUMMYJSON', $this->type->getSQLDeclaration(array(), $this->platform)); | ||
} | ||
|
||
public function testJsonNullConvertsToPHPValue() | ||
{ | ||
$this->assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
|
||
public function testJsonEmptyStringConvertsToPHPValue() | ||
{ | ||
$this->assertNull($this->type->convertToPHPValue('', $this->platform)); | ||
} | ||
|
||
public function testJsonStringConvertsToPHPValue() | ||
{ | ||
$value = array('foo' => 'bar', 'bar' => 'foo'); | ||
$databaseValue = json_encode($value); | ||
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform); | ||
|
||
$this->assertEquals($value, $phpValue); | ||
} | ||
|
||
/** @dataProvider providerFailure */ | ||
public function testConversionFailure($data) | ||
{ | ||
error_reporting( (E_ALL | E_STRICT) - \E_NOTICE ); | ||
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); | ||
$this->type->convertToPHPValue($data, $this->platform); | ||
} | ||
|
||
public function providerFailure() | ||
{ | ||
return array(array('a'), array('{')); | ||
} | ||
|
||
public function testJsonResourceConvertsToPHPValue() | ||
{ | ||
$value = array('foo' => 'bar', 'bar' => 'foo'); | ||
$databaseValue = fopen('data://text/plain;base64,' . base64_encode(json_encode($value)), 'r'); | ||
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform); | ||
|
||
$this->assertSame($value, $phpValue); | ||
} | ||
|
||
public function testRequiresSQLCommentHint() | ||
{ | ||
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform)); | ||
} | ||
} |