root/trunk/generator/test/classes/propel/CharacterEncodingTest.php

Revision 1082, 3.8 kB (checked in by tony, 3 months ago)

Refs #683: Added support for PHP 5.3 namespaces

Line 
1 <?php
2
3 use bookstore::Peer as PropelPeer;
4 use bookstore::Model as PropelModel;
5
6 /*
7  *  $Id: GeneratedPeerTest.php 842 2007-12-02 16:28:20Z heltem $
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
12  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
13  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
14  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
16  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
17  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  *
21  * This software consists of voluntary contributions made by many individuals
22  * and is licensed under the LGPL. For more information please see
23  * <http://propel.phpdb.org>.
24  */
25
26 require_once 'bookstore/BookstoreTestBase.php';
27
28 /**
29  * Tests the character encoding support of the adapter.
30  *
31  * This test assumes that the created database supports UTF-8.  For this to work,
32  * this file also has to be UTF-8.
33  *
34  * The database is relaoded before every test and flushed after every test.  This
35  * means that you can always rely on the contents of the databases being the same
36  * for each test method in this class.  See the BookstoreDataPopulator::populate()
37  * method for the exact contents of the database.
38  *
39  * @see        BookstoreDataPopulator
40  * @author     Hans Lellelid <hans@xmpl.org>
41  */
42 class CharacterEncodingTest extends BookstoreTestBase {
43
44     /**
45      * Database adapter.
46      * @var        DBAdapter
47      */
48     private $adapter;
49
50     public function setUp()
51     {
52         parent::setUp();
53         if (!extension_loaded('iconv')) {
54             throw new Exception("Character-encoding tests require iconv extension to be loaded.");
55         }
56     }
57
58     public function testUtf8()
59     {
60         $db = ::Propel::getDB(PropelPeer::BookPeer::DATABASE_NAME);
61
62         $title = "СЌерть Ма бруЎершафт. МлаЎеМец О черт";
63         //        1234567890123456789012345678901234567
64         //                 1         2         3
65
66         $a = new PropelModel::Author();
67         $a->setFirstName("Б.");
68         $a->setLastName("АКУНИН");
69
70         $p = new PropelModel::Publisher();
71         $p->setName("ДетектОв рПссОйскОй, ПстрПсюжетМая прПза");
72
73         $b = new PropelModel::Book();
74         $b->setTitle($title);
75         $b->setISBN("B-59246");
76         $b->setAuthor($a);
77         $b->setPublisher($p);
78         $b->save();
79
80         $b->reload();
81
82         $this->assertEquals(37, iconv_strlen($b->getTitle(), 'utf-8'), "Expected 37 characters (not bytes) in title.");
83         $this->assertTrue(strlen($b->getTitle()) > iconv_strlen($b->getTitle(), 'utf-8'), "Expected more bytes than characters in title.");
84
85     }
86
87     public function testInvalidCharset()
88     {
89         $db = ::Propel::getDB(PropelPeer::BookPeer::DATABASE_NAME);
90         if ($db instanceof DBSQLite) {
91             $this->markTestSkipped();
92         }
93
94         $a = new PropelModel::Author();
95         $a->setFirstName("Б.");
96         $a->setLastName("АКУНИН");
97         $a->save();
98
99         $authorNameWindows1251 = iconv("utf-8", "windows-1251", $a->getLastName());
100         $a->setLastName($authorNameWindows1251);
101
102         // Different databases seem to handle invalid data differently (no surprise, I guess...)
103         if ($db instanceof DBPostgres) {
104             try {
105                 $a->save();
106                 $this->fail("Expected an exception when saving non-UTF8 data to database.");
107             } catch (Exception $x) {
108                 print $x;
109             }
110
111         } else {
112
113             // No exception is thrown by MySQL ... (others need to be tested still)
114             $a->save();
115             $a->reload();
116             print_r($a);
117             $this->assertEquals("",$a->getLastName(), "Expected last_name to be empty (after inserting invalid charset data)");
118         }
119
120     }
121
122 }
123
124 ?>
Note: See TracBrowser for help on using the browser.