CharacterEncodingTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once 'tools/helpers/bookstore/BookstoreTestBase.php';
  10. /**
  11. * Tests the character encoding support of the adapter.
  12. *
  13. * This test assumes that the created database supports UTF-8. For this to work,
  14. * this file also has to be UTF-8.
  15. *
  16. * The database is relaoded before every test and flushed after every test. This
  17. * means that you can always rely on the contents of the databases being the same
  18. * for each test method in this class. See the BookstoreDataPopulator::populate()
  19. * method for the exact contents of the database.
  20. *
  21. * @see BookstoreDataPopulator
  22. * @author Hans Lellelid <hans@xmpl.org>
  23. * @package misc
  24. */
  25. class CharacterEncodingTest extends BookstoreTestBase
  26. {
  27. /**
  28. * Database adapter.
  29. * @var DBAdapter
  30. */
  31. private $adapter;
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. if (!extension_loaded('iconv')) {
  36. throw new Exception("Character-encoding tests require iconv extension to be loaded.");
  37. }
  38. }
  39. public function testUtf8()
  40. {
  41. $this->markTestSkipped();
  42. $db = Propel::getDB(BookPeer::DATABASE_NAME);
  43. $title = "Смерть на брудершафт. Младенец и черт";
  44. // 1234567890123456789012345678901234567
  45. // 1 2 3
  46. $a = new Author();
  47. $a->setFirstName("Б.");
  48. $a->setLastName("АКУНИН");
  49. $p = new Publisher();
  50. $p->setName("Детектив российский, остросюжетная проза");
  51. $b = new Book();
  52. $b->setTitle($title);
  53. $b->setISBN("B-59246");
  54. $b->setAuthor($a);
  55. $b->setPublisher($p);
  56. $b->save();
  57. $b->reload();
  58. $this->assertEquals(37, iconv_strlen($b->getTitle(), 'utf-8'), "Expected 37 characters (not bytes) in title.");
  59. $this->assertTrue(strlen($b->getTitle()) > iconv_strlen($b->getTitle(), 'utf-8'), "Expected more bytes than characters in title.");
  60. }
  61. public function testInvalidCharset()
  62. {
  63. $this->markTestSkipped();
  64. $db = Propel::getDB(BookPeer::DATABASE_NAME);
  65. if ($db instanceof DBSQLite) {
  66. $this->markTestSkipped();
  67. }
  68. $a = new Author();
  69. $a->setFirstName("Б.");
  70. $a->setLastName("АКУНИН");
  71. $a->save();
  72. $authorNameWindows1251 = iconv("utf-8", "windows-1251", $a->getLastName());
  73. $a->setLastName($authorNameWindows1251);
  74. // Different databases seem to handle invalid data differently (no surprise, I guess...)
  75. if ($db instanceof DBPostgres) {
  76. try {
  77. $a->save();
  78. $this->fail("Expected an exception when saving non-UTF8 data to database.");
  79. } catch (Exception $x) {
  80. print $x;
  81. }
  82. } else {
  83. // No exception is thrown by MySQL ... (others need to be tested still)
  84. $a->save();
  85. $a->reload();
  86. $this->assertEquals("",$a->getLastName(), "Expected last_name to be empty (after inserting invalid charset data)");
  87. }
  88. }
  89. }