3 namespace Doctrine\Tests\ORM\Functional;
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;
10 use Doctrine\ORM\Mapping\AssociationMapping;
11 use Doctrine\DBAL\Types\Type as DBALType;
13 require_once __DIR__ . '/../../TestInit.php';
15 class TypeTest extends \Doctrine\Tests\OrmFunctionalTestCase
17 protected function setUp()
19 $this->useModelSet('generic');
23 public function testDecimal()
25 $decimal = new DecimalModel();
26 $decimal->decimal = 0.15;
27 $decimal->highScale = 0.1515;
29 $this->_em->persist($decimal);
33 $dql = "SELECT d FROM Doctrine\Tests\Models\Generic\DecimalModel d";
34 $decimal = $this->_em->createQuery($dql)->getSingleResult();
36 $this->assertEquals(0.15, $decimal->decimal);
37 $this->assertEquals(0.1515, $decimal->highScale);
44 public function testBoolean()
46 $bool = new BooleanModel();
47 $bool->booleanField = true;
49 $this->_em->persist($bool);
53 $dql = "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true";
54 $bool = $this->_em->createQuery($dql)->getSingleResult();
56 $this->assertTrue($bool->booleanField);
58 $bool->booleanField = false;
63 $dql = "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = false";
64 $bool = $this->_em->createQuery($dql)->getSingleResult();
66 $this->assertFalse($bool->booleanField);
69 public function testArray()
71 $serialize = new SerializationModel();
72 $serialize->array["foo"] = "bar";
73 $serialize->array["bar"] = "baz";
75 $this->_em->persist($serialize);
79 $dql = "SELECT s FROM Doctrine\Tests\Models\Generic\SerializationModel s";
80 $serialize = $this->_em->createQuery($dql)->getSingleResult();
82 $this->assertEquals(array("foo" => "bar", "bar" => "baz"), $serialize->array);
85 public function testObject()
87 $serialize = new SerializationModel();
88 $serialize->object = new \stdClass();
90 $this->_em->persist($serialize);
94 $dql = "SELECT s FROM Doctrine\Tests\Models\Generic\SerializationModel s";
95 $serialize = $this->_em->createQuery($dql)->getSingleResult();
97 $this->assertInstanceOf('stdClass', $serialize->object);
100 public function testDate()
102 $dateTime = new DateTimeModel();
103 $dateTime->date = new \DateTime('2009-10-01', new \DateTimeZone('Europe/Berlin'));
105 $this->_em->persist($dateTime);
109 $dateTimeDb = $this->_em->find('Doctrine\Tests\Models\Generic\DateTimeModel', $dateTime->id);
111 $this->assertInstanceOf('DateTime', $dateTimeDb->date);
112 $this->assertEquals('2009-10-01', $dateTimeDb->date->format('Y-m-d'));
115 public function testDateTime()
117 $dateTime = new DateTimeModel();
118 $dateTime->datetime = new \DateTime('2009-10-02 20:10:52', new \DateTimeZone('Europe/Berlin'));
120 $this->_em->persist($dateTime);
124 $dateTimeDb = $this->_em->find('Doctrine\Tests\Models\Generic\DateTimeModel', $dateTime->id);
126 $this->assertInstanceOf('DateTime', $dateTimeDb->datetime);
127 $this->assertEquals('2009-10-02 20:10:52', $dateTimeDb->datetime->format('Y-m-d H:i:s'));
129 $articles = $this->_em->getRepository( 'Doctrine\Tests\Models\Generic\DateTimeModel' )->findBy( array( 'datetime' => new \DateTime( "now" ) ) );
130 $this->assertEquals( 0, count( $articles ) );
133 public function testDqlQueryBindDateTimeInstance()
135 $date = new \DateTime('2009-10-02 20:10:52', new \DateTimeZone('Europe/Berlin'));
137 $dateTime = new DateTimeModel();
138 $dateTime->datetime = $date;
140 $this->_em->persist($dateTime);
144 $dateTimeDb = $this->_em->createQuery('SELECT d FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime = ?1')
145 ->setParameter(1, $date, DBALType::DATETIME)
149 public function testDqlQueryBuilderBindDateTimeInstance()
151 $date = new \DateTime('2009-10-02 20:10:52', new \DateTimeZone('Europe/Berlin'));
153 $dateTime = new DateTimeModel();
154 $dateTime->datetime = $date;
156 $this->_em->persist($dateTime);
160 $dateTimeDb = $this->_em->createQueryBuilder()
162 ->from('Doctrine\Tests\Models\Generic\DateTimeModel', 'd')
163 ->where('d.datetime = ?1')
164 ->setParameter(1, $date, DBALType::DATETIME)
165 ->getQuery()->getSingleResult();
168 public function testTime()
170 $dateTime = new DateTimeModel();
171 $dateTime->time = new \DateTime('2010-01-01 19:27:20');
173 $this->_em->persist($dateTime);
177 $dateTimeDb = $this->_em->find('Doctrine\Tests\Models\Generic\DateTimeModel', $dateTime->id);
179 $this->assertInstanceOf('DateTime', $dateTime->time);
180 $this->assertEquals('19:27:20', $dateTime->time->format('H:i:s'));