3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\Tests\Models\CustomType\CustomTypeChild;
6 use Doctrine\Tests\Models\CustomType\CustomTypeParent;
7 use Doctrine\Tests\Models\CustomType\CustomTypeUpperCase;
8 use Doctrine\DBAL\Types\Type as DBALType;
10 require_once __DIR__ . '/../../TestInit.php';
12 class TypeValueSqlTest extends \Doctrine\Tests\OrmFunctionalTestCase
14 protected function setUp()
16 if (DBALType::hasType('upper_case_string')) {
17 DBALType::overrideType('upper_case_string', '\Doctrine\Tests\DbalTypes\UpperCaseStringType');
19 DBALType::addType('upper_case_string', '\Doctrine\Tests\DbalTypes\UpperCaseStringType');
22 if (DBALType::hasType('negative_to_positive')) {
23 DBALType::overrideType('negative_to_positive', '\Doctrine\Tests\DbalTypes\NegativeToPositiveType');
25 DBALType::addType('negative_to_positive', '\Doctrine\Tests\DbalTypes\NegativeToPositiveType');
28 $this->useModelSet('customtype');
32 public function testUpperCaseStringType()
34 $entity = new CustomTypeUpperCase();
35 $entity->lowerCaseString = 'foo';
37 $this->_em->persist($entity);
44 $entity = $this->_em->find('\Doctrine\Tests\Models\CustomType\CustomTypeUpperCase', $id);
46 $this->assertEquals('foo', $entity->lowerCaseString, 'Entity holds lowercase string');
47 $this->assertEquals('FOO', $this->_em->getConnection()->fetchColumn("select lowerCaseString from customtype_uppercases where id=".$entity->id.""), 'Database holds uppercase string');
53 public function testUpperCaseStringTypeWhenColumnNameIsDefined()
56 $entity = new CustomTypeUpperCase();
57 $entity->lowerCaseString = 'Some Value';
58 $entity->namedLowerCaseString = 'foo';
60 $this->_em->persist($entity);
67 $entity = $this->_em->find('\Doctrine\Tests\Models\CustomType\CustomTypeUpperCase', $id);
68 $this->assertEquals('foo', $entity->namedLowerCaseString, 'Entity holds lowercase string');
69 $this->assertEquals('FOO', $this->_em->getConnection()->fetchColumn("select named_lower_case_string from customtype_uppercases where id=".$entity->id.""), 'Database holds uppercase string');
72 $entity->namedLowerCaseString = 'bar';
74 $this->_em->persist($entity);
82 $entity = $this->_em->find('\Doctrine\Tests\Models\CustomType\CustomTypeUpperCase', $id);
83 $this->assertEquals('bar', $entity->namedLowerCaseString, 'Entity holds lowercase string');
84 $this->assertEquals('BAR', $this->_em->getConnection()->fetchColumn("select named_lower_case_string from customtype_uppercases where id=".$entity->id.""), 'Database holds uppercase string');
87 public function testTypeValueSqlWithAssociations()
89 $parent = new CustomTypeParent();
90 $parent->customInteger = -1;
91 $parent->child = new CustomTypeChild();
93 $friend1 = new CustomTypeParent();
94 $friend2 = new CustomTypeParent();
96 $parent->addMyFriend($friend1);
97 $parent->addMyFriend($friend2);
99 $this->_em->persist($parent);
100 $this->_em->persist($friend1);
101 $this->_em->persist($friend2);
104 $parentId = $parent->id;
108 $entity = $this->_em->find('Doctrine\Tests\Models\CustomType\CustomTypeParent', $parentId);
110 $this->assertTrue($entity->customInteger < 0, 'Fetched customInteger negative');
111 $this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select customInteger from customtype_parents where id=".$entity->id.""), 'Database has stored customInteger positive');
113 $this->assertNotNull($parent->child, 'Child attached');
114 $this->assertCount(2, $entity->getMyFriends(), '2 friends attached');
117 public function testSelectDQL()
119 $parent = new CustomTypeParent();
120 $parent->customInteger = -1;
121 $parent->child = new CustomTypeChild();
123 $this->_em->persist($parent);
126 $parentId = $parent->id;
130 $query = $this->_em->createQuery("SELECT p, p.customInteger, c from Doctrine\Tests\Models\CustomType\CustomTypeParent p JOIN p.child c where p.id = " . $parentId);
132 $result = $query->getResult();
134 $this->assertEquals(1, count($result));
135 $this->assertInstanceOf('Doctrine\Tests\Models\CustomType\CustomTypeParent', $result[0][0]);
136 $this->assertEquals(-1, $result[0][0]->customInteger);
138 $this->assertEquals(-1, $result[0]['customInteger']);
140 $this->assertEquals('foo', $result[0][0]->child->lowerCaseString);