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