Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / TypeTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\Generic\BooleanModel;
6 use Doctrine\Tests\Models\Generic\DateTimeModel;
7 use Doctrine\Tests\Models\Generic\DecimalModel;
8 use Doctrine\Tests\Models\Generic\SerializationModel;
9
10 use Doctrine\ORM\Mapping\AssociationMapping;
11 use Doctrine\DBAL\Types\Type as DBALType;
12
13 require_once __DIR__ . '/../../TestInit.php';
14
15 class TypeTest extends \Doctrine\Tests\OrmFunctionalTestCase
16 {
17     protected function setUp()
18     {
19         $this->useModelSet('generic');
20         parent::setUp();
21     }
22
23     public function testDecimal()
24     {
25         $decimal = new DecimalModel();
26         $decimal->decimal = 0.15;
27         $decimal->highScale = 0.1515;
28
29         $this->_em->persist($decimal);
30         $this->_em->flush();
31         $this->_em->clear();
32
33         $dql = "SELECT d FROM Doctrine\Tests\Models\Generic\DecimalModel d";
34         $decimal = $this->_em->createQuery($dql)->getSingleResult();
35
36         $this->assertEquals(0.15, $decimal->decimal);
37         $this->assertEquals(0.1515, $decimal->highScale);
38     }
39
40     /**
41      * @group DDC-1394
42      * @return void
43      */
44     public function testBoolean()
45     {
46         $bool = new BooleanModel();
47         $bool->booleanField = true;
48
49         $this->_em->persist($bool);
50         $this->_em->flush();
51         $this->_em->clear();
52
53         $dql = "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true";
54         $bool = $this->_em->createQuery($dql)->getSingleResult();
55
56         $this->assertTrue($bool->booleanField);
57
58         $bool->booleanField = false;
59
60         $this->_em->flush();
61         $this->_em->clear();
62
63         $dql = "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = false";
64         $bool = $this->_em->createQuery($dql)->getSingleResult();
65
66         $this->assertFalse($bool->booleanField);
67     }
68
69     public function testArray()
70     {
71         $serialize = new SerializationModel();
72         $serialize->array["foo"] = "bar";
73         $serialize->array["bar"] = "baz";
74
75         $this->_em->persist($serialize);
76         $this->_em->flush();
77         $this->_em->clear();
78
79         $dql = "SELECT s FROM Doctrine\Tests\Models\Generic\SerializationModel s";
80         $serialize = $this->_em->createQuery($dql)->getSingleResult();
81
82         $this->assertEquals(array("foo" => "bar", "bar" => "baz"), $serialize->array);
83     }
84
85     public function testObject()
86     {
87         $serialize = new SerializationModel();
88         $serialize->object = new \stdClass();
89
90         $this->_em->persist($serialize);
91         $this->_em->flush();
92         $this->_em->clear();
93
94         $dql = "SELECT s FROM Doctrine\Tests\Models\Generic\SerializationModel s";
95         $serialize = $this->_em->createQuery($dql)->getSingleResult();
96
97         $this->assertInstanceOf('stdClass', $serialize->object);
98     }
99
100     public function testDate()
101     {
102         $dateTime = new DateTimeModel();
103         $dateTime->date = new \DateTime('2009-10-01', new \DateTimeZone('Europe/Berlin'));
104
105         $this->_em->persist($dateTime);
106         $this->_em->flush();
107         $this->_em->clear();
108
109         $dateTimeDb = $this->_em->find('Doctrine\Tests\Models\Generic\DateTimeModel', $dateTime->id);
110
111         $this->assertInstanceOf('DateTime', $dateTimeDb->date);
112         $this->assertEquals('2009-10-01', $dateTimeDb->date->format('Y-m-d'));
113     }
114
115     public function testDateTime()
116     {
117         $dateTime = new DateTimeModel();
118         $dateTime->datetime = new \DateTime('2009-10-02 20:10:52', new \DateTimeZone('Europe/Berlin'));
119
120         $this->_em->persist($dateTime);
121         $this->_em->flush();
122         $this->_em->clear();
123
124         $dateTimeDb = $this->_em->find('Doctrine\Tests\Models\Generic\DateTimeModel', $dateTime->id);
125
126         $this->assertInstanceOf('DateTime', $dateTimeDb->datetime);
127         $this->assertEquals('2009-10-02 20:10:52', $dateTimeDb->datetime->format('Y-m-d H:i:s'));
128
129         $articles = $this->_em->getRepository( 'Doctrine\Tests\Models\Generic\DateTimeModel' )->findBy( array( 'datetime' => new \DateTime( "now" ) ) );
130         $this->assertEquals( 0, count( $articles ) );
131     }
132
133     public function testDqlQueryBindDateTimeInstance()
134     {
135         $date = new \DateTime('2009-10-02 20:10:52', new \DateTimeZone('Europe/Berlin'));
136
137         $dateTime = new DateTimeModel();
138         $dateTime->datetime = $date;
139
140         $this->_em->persist($dateTime);
141         $this->_em->flush();
142         $this->_em->clear();
143
144         $dateTimeDb = $this->_em->createQuery('SELECT d FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime = ?1')
145                                 ->setParameter(1, $date, DBALType::DATETIME)
146                                 ->getSingleResult();
147     }
148
149     public function testDqlQueryBuilderBindDateTimeInstance()
150     {
151         $date = new \DateTime('2009-10-02 20:10:52', new \DateTimeZone('Europe/Berlin'));
152
153         $dateTime = new DateTimeModel();
154         $dateTime->datetime = $date;
155
156         $this->_em->persist($dateTime);
157         $this->_em->flush();
158         $this->_em->clear();
159
160         $dateTimeDb = $this->_em->createQueryBuilder()
161                                  ->select('d')
162                                  ->from('Doctrine\Tests\Models\Generic\DateTimeModel', 'd')
163                                  ->where('d.datetime = ?1')
164                                  ->setParameter(1, $date, DBALType::DATETIME)
165                                  ->getQuery()->getSingleResult();
166     }
167
168     public function testTime()
169     {
170         $dateTime = new DateTimeModel();
171         $dateTime->time = new \DateTime('2010-01-01 19:27:20');
172
173         $this->_em->persist($dateTime);
174         $this->_em->flush();
175         $this->_em->clear();
176
177         $dateTimeDb = $this->_em->find('Doctrine\Tests\Models\Generic\DateTimeModel', $dateTime->id);
178
179         $this->assertInstanceOf('DateTime', $dateTime->time);
180         $this->assertEquals('19:27:20', $dateTime->time->format('H:i:s'));
181     }
182 }