Skip to content

Commit 17970f6

Browse files
committed
Merge pull request #12 from bendavies/sqlite-support
support SQLite for functional testing purposes
2 parents d02bb6f + 89b5e8c commit 17970f6

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

DBAL/Types/AbstractEnumType.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Fresh\Bundle\DoctrineEnumBundle\DBAL\Types;
1212

1313
use Doctrine\DBAL\Platforms\AbstractPlatform;
14+
use Doctrine\DBAL\Platforms\SqlitePlatform;
1415
use Doctrine\DBAL\Types\Type;
1516

1617
/**
@@ -19,6 +20,7 @@
1920
* Provides support of MySQL ENUM type for Doctrine in Symfony applications
2021
*
2122
* @author Artem Genvald <genvaldartem@gmail.com>
23+
* @author Ben Davies <ben.davies@gmail.com>
2224
*/
2325
abstract class AbstractEnumType extends Type
2426
{
@@ -54,14 +56,18 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
5456
*/
5557
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
5658
{
57-
$values = array_map(
59+
$values = implode(', ', array_map(
5860
function ($value) {
5961
return "'{$value}'";
6062
},
6163
$this->getValues()
62-
);
64+
));
6365

64-
return 'ENUM(' . implode(', ', $values) . ')';
66+
if ($platform instanceof SqlitePlatform) {
67+
return sprintf('TEXT CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
68+
}
69+
70+
return sprintf('ENUM(%s)', $values);
6571
}
6672

6773
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/*
3+
* This file is part of the FreshDoctrineEnumBundle
4+
*
5+
* (c) Artem Genvald <genvaldartem@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Fresh\Bundle\DoctrineEnumBundle\Tests\DBAL\Types;
11+
12+
use Doctrine\DBAL\Platforms\AbstractPlatform;
13+
use Doctrine\DBAL\Platforms\MySqlPlatform;
14+
use Doctrine\DBAL\Platforms\SqlitePlatform;
15+
16+
/**
17+
* AbstractEnumTypeTest
18+
*
19+
* @author Ben Davies <ben.davies@gmail.com>
20+
*
21+
* @coversDefaultClass \Fresh\Bundle\DoctrineEnumBundle\DBAL\Types\AbstractEnumType
22+
*/
23+
class AbstractEnumTypeTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @var \Fresh\Bundle\DoctrineEnumBundle\DBAL\Types\AbstractEnumType
27+
*/
28+
private $type;
29+
30+
/**
31+
* Set up EnumType
32+
*/
33+
public function setUp()
34+
{
35+
$this->type = $this->getMockBuilder('Fresh\Bundle\DoctrineEnumBundle\DBAL\Types\AbstractEnumType')
36+
->disableOriginalConstructor()
37+
->setMethods(array('getValues'))
38+
->getMockForAbstractClass();
39+
40+
$this->type->staticExpects($this->any())
41+
->method('getValues')
42+
->will($this->returnValue(array('M', 'F')));
43+
}
44+
45+
/**
46+
* Test that the SQL declaration is the correct for the platform
47+
*
48+
* @param array $fieldDeclaration The Field Declaration
49+
* @param AbstractPlatform $platform The DBAL Platform
50+
* @param string $expected Expected Sql Declaration
51+
*
52+
* @test
53+
* @covers ::getSqlDeclaration
54+
* @dataProvider platformProvider
55+
*/
56+
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform, $expected)
57+
{
58+
$this->assertEquals($expected, $this->type->getSqlDeclaration($fieldDeclaration, $platform));
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function platformProvider()
65+
{
66+
return array(
67+
array(array('name' => 'sex'), new MySqlPlatform(), "ENUM('M', 'F')"),
68+
array(array('name' => 'sex'), new SqlitePlatform(), "TEXT CHECK(sex IN ('M', 'F'))")
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)