Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Functional / WriteTest.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Functional;
4 use Doctrine\DBAL\Types\Type;
5 use PDO;
6
7 class WriteTest extends \Doctrine\Tests\DbalFunctionalTestCase
8 {
9     public function setUp()
10     {
11         parent::setUp();
12
13         try {
14             /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
15             $table = new \Doctrine\DBAL\Schema\Table("write_table");
16             $table->addColumn('id', 'integer', array('autoincrement' => true));
17             $table->addColumn('test_int', 'integer');
18             $table->addColumn('test_string', 'string', array('notnull' => false));
19             $table->setPrimaryKey(array('id'));
20
21             foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
22                 $this->_conn->executeQuery($sql);
23             }
24         } catch(\Exception $e) {
25
26         }
27         $this->_conn->executeUpdate('DELETE FROM write_table');
28     }
29
30     /**
31      * @group DBAL-80
32      */
33     public function testExecuteUpdateFirstTypeIsNull()
34     {
35         $sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)";
36         $this->_conn->executeUpdate($sql, array("text", 1111), array(null, PDO::PARAM_INT));
37
38         $sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?";
39         $this->assertTrue((bool)$this->_conn->fetchColumn($sql, array("text", 1111)));
40     }
41
42     public function testExecuteUpdate()
43     {
44         $sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")";
45         $affected = $this->_conn->executeUpdate($sql);
46
47         $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
48     }
49
50     public function testExecuteUpdateWithTypes()
51     {
52         $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
53         $affected = $this->_conn->executeUpdate($sql, array(1, 'foo'), array(\PDO::PARAM_INT, \PDO::PARAM_STR));
54
55         $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
56     }
57
58     public function testPrepareRowCountReturnsAffectedRows()
59     {
60         $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
61         $stmt = $this->_conn->prepare($sql);
62
63         $stmt->bindValue(1, 1);
64         $stmt->bindValue(2, "foo");
65         $stmt->execute();
66
67         $this->assertEquals(1, $stmt->rowCount());
68     }
69
70     public function testPrepareWithPdoTypes()
71     {
72         $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
73         $stmt = $this->_conn->prepare($sql);
74
75         $stmt->bindValue(1, 1, \PDO::PARAM_INT);
76         $stmt->bindValue(2, "foo", \PDO::PARAM_STR);
77         $stmt->execute();
78
79         $this->assertEquals(1, $stmt->rowCount());
80     }
81
82     public function testPrepareWithDbalTypes()
83     {
84         $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
85         $stmt = $this->_conn->prepare($sql);
86
87         $stmt->bindValue(1, 1, Type::getType('integer'));
88         $stmt->bindValue(2, "foo", Type::getType('string'));
89         $stmt->execute();
90
91         $this->assertEquals(1, $stmt->rowCount());
92     }
93
94     public function testPrepareWithDbalTypeNames()
95     {
96         $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
97         $stmt = $this->_conn->prepare($sql);
98
99         $stmt->bindValue(1, 1, 'integer');
100         $stmt->bindValue(2, "foo", 'string');
101         $stmt->execute();
102
103         $this->assertEquals(1, $stmt->rowCount());
104     }
105
106     public function insertRows()
107     {
108         $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1, 'test_string' => 'foo')));
109         $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
110     }
111
112     public function testInsert()
113     {
114         $this->insertRows();
115     }
116
117     public function testDelete()
118     {
119         $this->insertRows();
120
121         $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2)));
122         $this->assertEquals(1, count($this->_conn->fetchAll('SELECT * FROM write_table')));
123
124         $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1)));
125         $this->assertEquals(0, count($this->_conn->fetchAll('SELECT * FROM write_table')));
126     }
127
128     public function testUpdate()
129     {
130         $this->insertRows();
131
132         $this->assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo')));
133         $this->assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
134         $this->assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
135     }
136
137     public function testLastInsertId()
138     {
139         if ( ! $this->_conn->getDatabasePlatform()->prefersIdentityColumns()) {
140             $this->markTestSkipped('Test only works on platforms with identity columns.');
141         }
142
143         $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
144         $num = $this->_conn->lastInsertId();
145
146         $this->assertNotNull($num, "LastInsertId() should not be null.");
147         $this->assertTrue($num > 0, "LastInsertId() should be non-negative number.");
148     }
149
150     public function testLastInsertIdSequence()
151     {
152         if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
153             $this->markTestSkipped('Test only works on platforms with sequences.');
154         }
155
156         $sequence = new \Doctrine\DBAL\Schema\Sequence('write_table_id_seq');
157         try {
158             $this->_conn->getSchemaManager()->createSequence($sequence);
159         } catch(\Exception $e) {
160         }
161
162         $sequences = $this->_conn->getSchemaManager()->listSequences();
163         $this->assertEquals(1, count(array_filter($sequences, function($sequence) {
164             return $sequence->getName() === 'write_table_id_seq';
165         })));
166
167         $stmt = $this->_conn->query($this->_conn->getDatabasePlatform()->getSequenceNextValSQL('write_table_id_seq'));
168         $nextSequenceVal = $stmt->fetchColumn();
169
170         $lastInsertId = $this->_conn->lastInsertId('write_table_id_seq');
171
172         $this->assertTrue($lastInsertId > 0);
173         $this->assertEquals($nextSequenceVal, $lastInsertId);
174     }
175
176     public function testLastInsertIdNoSequenceGiven()
177     {
178         if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
179             $this->markTestSkipped('Test only works on platforms with sequences.');
180         }
181
182         $this->assertFalse($this->_conn->lastInsertId( null ));
183
184     }
185 }