Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / TypeValueSqlTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
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;
9
10 require_once __DIR__ . '/../../TestInit.php';
11
12 class TypeValueSqlTest extends \Doctrine\Tests\OrmFunctionalTestCase
13 {
14     protected function setUp()
15     {
16         if (DBALType::hasType('upper_case_string')) {
17             DBALType::overrideType('upper_case_string', '\Doctrine\Tests\DbalTypes\UpperCaseStringType');
18         } else {
19             DBALType::addType('upper_case_string', '\Doctrine\Tests\DbalTypes\UpperCaseStringType');
20         }
21
22         if (DBALType::hasType('negative_to_positive')) {
23             DBALType::overrideType('negative_to_positive', '\Doctrine\Tests\DbalTypes\NegativeToPositiveType');
24         } else {
25             DBALType::addType('negative_to_positive', '\Doctrine\Tests\DbalTypes\NegativeToPositiveType');
26         }
27
28         $this->useModelSet('customtype');
29         parent::setUp();
30     }
31
32     public function testUpperCaseStringType()
33     {
34         $entity = new CustomTypeUpperCase();
35         $entity->lowerCaseString = 'foo';
36
37         $this->_em->persist($entity);
38         $this->_em->flush();
39
40         $id = $entity->id;
41
42         $this->_em->clear();
43
44         $entity = $this->_em->find('\Doctrine\Tests\Models\CustomType\CustomTypeUpperCase', $id);
45
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');
48     }
49
50     /**
51      * @group DDC-1642
52      */
53     public function testUpperCaseStringTypeWhenColumnNameIsDefined()
54     {
55  
56         $entity = new CustomTypeUpperCase();
57         $entity->lowerCaseString        = 'Some Value';
58         $entity->namedLowerCaseString   = 'foo';
59
60         $this->_em->persist($entity);
61         $this->_em->flush();
62
63         $id = $entity->id;
64
65         $this->_em->clear();
66
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');
70
71
72         $entity->namedLowerCaseString   = 'bar';
73
74         $this->_em->persist($entity);
75         $this->_em->flush();
76
77         $id = $entity->id;
78
79         $this->_em->clear();
80
81
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');
85     }
86
87     public function testTypeValueSqlWithAssociations()
88     {
89         $parent = new CustomTypeParent();
90         $parent->customInteger = -1;
91         $parent->child = new CustomTypeChild();
92
93         $friend1 = new CustomTypeParent();
94         $friend2 = new CustomTypeParent();
95
96         $parent->addMyFriend($friend1);
97         $parent->addMyFriend($friend2);
98
99         $this->_em->persist($parent);
100         $this->_em->persist($friend1);
101         $this->_em->persist($friend2);
102         $this->_em->flush();
103
104         $parentId = $parent->id;
105
106         $this->_em->clear();
107
108         $entity = $this->_em->find('Doctrine\Tests\Models\CustomType\CustomTypeParent', $parentId);
109
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');
112
113         $this->assertNotNull($parent->child, 'Child attached');
114         $this->assertCount(2, $entity->getMyFriends(), '2 friends attached');
115     }
116
117     public function testSelectDQL()
118     {
119         $parent = new CustomTypeParent();
120         $parent->customInteger = -1;
121         $parent->child = new CustomTypeChild();
122
123         $this->_em->persist($parent);
124         $this->_em->flush();
125
126         $parentId = $parent->id;
127
128         $this->_em->clear();
129
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);
131
132         $result = $query->getResult();
133
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);
137
138         $this->assertEquals(-1, $result[0]['customInteger']);
139
140         $this->assertEquals('foo', $result[0][0]->child->lowerCaseString);
141     }
142 }