{"id":54,"date":"2011-07-14T16:44:45","date_gmt":"2011-07-14T15:44:45","guid":{"rendered":"http:\/\/blogs.kent.ac.uk\/webdev\/?p=54"},"modified":"2011-10-24T15:50:16","modified_gmt":"2011-10-24T14:50:16","slug":"phpunit-and-unserialized-pdo-instances","status":"publish","type":"post","link":"https:\/\/blogs.kent.ac.uk\/webdev\/2011\/07\/14\/phpunit-and-unserialized-pdo-instances\/","title":{"rendered":"PHPUnit and (un)serialized PDO instances"},"content":{"rendered":"<p>Do you ever get the following confusing message when you run your PHPUnit tests?<\/p>\n<blockquote><p>PDOException: You cannot serialize or unserialize PDO instances<\/p><\/blockquote>\n<p><strong>The fix<br \/>\n<\/strong>You can fix this by adding the following comment block to the top of your unit test class:<\/p>\n<pre class=\"brush:php\">\/**\r\n* @backupGlobals disabled\r\n* @backupStaticAttributes disabled\r\n*\/<\/pre>\n<p><strong><br \/>\nThe explanation<br \/>\n<\/strong>If you&#8217;re interested in what&#8217;s happening: by default PHPUnit tries to keep all global and superglobal variables in your test contained to prevent them spreading into other tests. It does this using serialize() and unserialize().<\/p>\n<p>In the case of the limonade microframework, anything using option() is stored in the $GLOBALS array. That includes the PDO object, which is generally stored in $GLOBALS because it can be used all over the place.<\/p>\n<p>Unfortunately, and as the error message makes clear, PDO objects can&#8217;t be serialized or unserialized&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you ever get the following confusing message when you run your PHPUnit tests? PDOException: You cannot serialize or unserialize PDO instances The fix You &hellip; <a href=\"https:\/\/blogs.kent.ac.uk\/webdev\/2011\/07\/14\/phpunit-and-unserialized-pdo-instances\/\">Read&nbsp;more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1464],"tags":[8964,8965],"_links":{"self":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts\/54"}],"collection":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":2,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":227,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/posts\/54\/revisions\/227"}],"wp:attachment":[{"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.kent.ac.uk\/webdev\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}