PortabilityTest.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Functional;
  3. use Doctrine\DBAL\Types\Type;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\DriverManager;
  6. use PDO;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. /**
  9. * @group DBAL-56
  10. */
  11. class PortabilityTest extends \Doctrine\Tests\DbalFunctionalTestCase
  12. {
  13. static private $hasTable = false;
  14. private $portableConnection;
  15. public function tearDown()
  16. {
  17. if ($this->portableConnection) {
  18. $this->portableConnection->close();
  19. }
  20. }
  21. private function getPortableConnection($portabilityMode = \Doctrine\DBAL\Portability\Connection::PORTABILITY_ALL, $case = \PDO::CASE_LOWER)
  22. {
  23. if (!$this->portableConnection) {
  24. $params = $this->_conn->getParams();
  25. $params['wrapperClass'] = 'Doctrine\DBAL\Portability\Connection';
  26. $params['portability'] = $portabilityMode;
  27. $params['fetch_case'] = $case;
  28. $this->portableConnection = DriverManager::getConnection($params, $this->_conn->getConfiguration(), $this->_conn->getEventManager());
  29. try {
  30. /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
  31. $table = new \Doctrine\DBAL\Schema\Table("portability_table");
  32. $table->addColumn('Test_Int', 'integer');
  33. $table->addColumn('Test_String', 'string', array('fixed' => true, 'length' => 32));
  34. $table->addColumn('Test_Null', 'string', array('notnull' => false));
  35. $sm = $this->portableConnection->getSchemaManager();
  36. $sm->createTable($table);
  37. $this->portableConnection->insert('portability_table', array('Test_Int' => 1, 'Test_String' => 'foo', 'Test_Null' => ''));
  38. $this->portableConnection->insert('portability_table', array('Test_Int' => 1, 'Test_String' => 'foo ', 'Test_Null' => null));
  39. } catch(\Exception $e) {
  40. }
  41. }
  42. return $this->portableConnection;
  43. }
  44. public function testFullFetchMode()
  45. {
  46. $rows = $this->getPortableConnection()->fetchAll('SELECT * FROM portability_table');
  47. $this->assertFetchResultRows($rows);
  48. $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
  49. while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  50. $this->assertFetchResultRow($row);
  51. }
  52. $stmt = $this->getPortableConnection()->prepare('SELECT * FROM portability_table');
  53. $stmt->execute();
  54. while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  55. $this->assertFetchResultRow($row);
  56. }
  57. }
  58. public function assertFetchResultRows($rows)
  59. {
  60. $this->assertEquals(2, count($rows));
  61. foreach ($rows AS $row) {
  62. $this->assertFetchResultRow($row);
  63. }
  64. }
  65. public function assertFetchResultRow($row)
  66. {
  67. $this->assertEquals(1, $row['test_int']);
  68. $this->assertArrayHasKey('test_string', $row, "Case should be lowered.");
  69. $this->assertEquals(3, strlen($row['test_string']), "test_string should be rtrimed to length of three for CHAR(32) column.");
  70. $this->assertNull($row['test_null']);
  71. }
  72. }