Annotation of embedaddon/php/ext/pdo_mysql/tests/pdo_mysql_stmt_fetch_serialize.phpt, revision 1.1.1.1

1.1       misho       1: --TEST--
                      2: MySQL PDOStatement->fetch(), PDO::FETCH_SERIALIZE
                      3: --SKIPIF--
                      4: <?php
                      5: require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
                      6: require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
                      7: MySQLPDOTest::skip();
                      8: if (version_compare(PHP_VERSION, '5.1.0', '<'))
                      9:        die("skip Needs 5.1.0 and Interface Serializable");
                     10: ?>
                     11: --FILE--
                     12: <?php
                     13:        require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
                     14:        $db = MySQLPDOTest::factory();
                     15: 
                     16:        try {
                     17: 
                     18:                class myclass implements Serializable {
                     19: 
                     20:                        private static $instance = null;
                     21:                        protected $myprotected = 'a protected propery';
                     22: 
                     23:                        // Good old magic stuff
                     24:                        private function __construct($caller = NULL) {
                     25:                                printf("%s(%s)\n", __METHOD__, $caller);
                     26:                        }
                     27: 
                     28: 
                     29:                        public function __destruct() {
                     30:                                // printf("%s()\n", __METHOD__);
                     31:                        }
                     32: 
                     33:                        public function __sleep() {
                     34:                                printf("%s()\n", __METHOD__);
                     35:                        }
                     36: 
                     37:                        public function __wakeup() {
                     38:                                printf("%s()\n", __METHOD__);
                     39:                        }
                     40: 
                     41:                        public function __call($method, $params) {
                     42:                                printf("%s(%s, %s)\n", __METHOD__, $method, var_export($params, true));
                     43:                        }
                     44: 
                     45:                        public function __set($prop, $value) {
                     46:                                printf("%s(%s, %s)\n", __METHOD__, $prop, var_export($value, true));
                     47:                                $this->{$prop} = $value;
                     48:                        }
                     49: 
                     50:                        public function __get($prop) {
                     51:                                printf("%s(%s)\n", __METHOD__, $prop);
                     52:                                return NULL;
                     53:                        }
                     54: 
                     55:                        // Singleton
                     56:                        public static function singleton($caller) {
                     57:                                printf("%s(%s)\n", __METHOD__, $caller);
                     58: 
                     59:                                if (!self::$instance) {
                     60:                                        $c = __CLASS__;
                     61:                                        self::$instance = new $c($caller);
                     62:                                }
                     63:                                return self::$instance;
                     64:                        }
                     65: 
                     66:                        // Serializable
                     67:                        public function serialize() {
                     68:                                printf("%s()\n", __METHOD__);
                     69:                                return 'Data from serialize';
                     70:                        }
                     71: 
                     72:                        public function unserialize($data) {
                     73:                                printf("%s(%s)\n", __METHOD__, var_export($data, true));
                     74:                        }
                     75: 
                     76:                }
                     77: 
                     78:                $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
                     79:                if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
                     80:                        printf("[002] Unable to turn off emulated prepared statements\n");
                     81: 
                     82:                $db->exec('DROP TABLE IF EXISTS test');
                     83:                $db->exec(sprintf('CREATE TABLE test(id INT, myobj BLOB) ENGINE=%s',
                     84:                        MySQLPDOTest::getTableEngine()));
                     85: 
                     86:                printf("Creating an object, serializing it and writing it to DB...\n");
                     87:                $id = 1;
                     88:                $obj = myclass::singleton('Creating object');
                     89:                $myobj = serialize($obj);
                     90:                $stmt = $db->prepare('INSERT INTO test(id, myobj) VALUES (?, ?)');
                     91:                $stmt->bindValue(1, $id);
                     92:                $stmt->bindValue(2, $myobj);
                     93:                $stmt->execute();
                     94: 
                     95:                printf("\nUnserializing the previously serialized object...\n");
                     96:                var_dump(unserialize($myobj));
                     97: 
                     98:                printf("\nUsing PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE to fetch the object from DB and unserialize it...\n");
                     99:                $stmt = $db->prepare('SELECT myobj FROM test');
                    100:                $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('PDO shall not call __construct()'));
                    101:                $stmt->execute();
                    102:                var_dump($stmt->fetch());
                    103: 
                    104:                printf("\nUsing PDO::FETCH_CLASS to fetch the object from DB and unserialize it...\n");
                    105:                $stmt = $db->prepare('SELECT myobj FROM test');
                    106:                $stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass', array('PDO shall call __construct()'));
                    107:                $stmt->execute();
                    108:                var_dump($stmt->fetch());
                    109: 
                    110: 
                    111:        } catch (PDOException $e) {
                    112:                printf("[001] %s [%s] %s\n",
                    113:                        $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
                    114:        }
                    115: 
                    116:        print "done!\n";
                    117: ?>
                    118: --CLEAN--
                    119: <?php
                    120: require dirname(__FILE__) . '/mysql_pdo_test.inc';
                    121: $db = MySQLPDOTest::factory();
                    122: $db->exec('DROP TABLE IF EXISTS test');
                    123: ?>
                    124: --EXPECTF--
                    125: Creating an object, serializing it and writing it to DB...
                    126: myclass::singleton(Creating object)
                    127: myclass::__construct(Creating object)
                    128: myclass::serialize()
                    129: 
                    130: Unserializing the previously serialized object...
                    131: myclass::unserialize('Data from serialize')
                    132: object(myclass)#4 (1) {
                    133:   [%u|b%"myprotected":protected]=>
                    134:   %unicode|string%(19) "a protected propery"
                    135: }
                    136: 
                    137: Using PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE to fetch the object from DB and unserialize it...
                    138: myclass::unserialize('C:7:"myclass":19:{Data from serialize}')
                    139: object(myclass)#%d (1) {
                    140:   [%u|b%"myprotected":protected]=>
                    141:   %unicode|string%(19) "a protected propery"
                    142: }
                    143: 
                    144: Using PDO::FETCH_CLASS to fetch the object from DB and unserialize it...
                    145: myclass::__set(myobj, 'C:7:"myclass":19:{Data from serialize}')
                    146: myclass::__construct(PDO shall call __construct())
                    147: object(myclass)#%d (2) {
                    148:   [%u|b%"myprotected":protected]=>
                    149:   %unicode|string%(19) "a protected propery"
                    150:   [%u|b%"myobj"]=>
                    151:   %unicode|string%(38) "C:7:"myclass":19:{Data from serialize}"
                    152: }
                    153: done!

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>