MDL-57570 static store: Basic serialize tests with igbinary
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 18 Jan 2017 18:23:59 +0000 (19:23 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Wed, 18 Jan 2017 18:23:59 +0000 (19:23 +0100)
This unit test is not really verifying that internally igbinary
in being used but just igbinary availability and, by double
serializing, that it works for a semi-complex object.

The test will be skipped if igbinary is not available.

cache/stores/static/tests/static_test.php

index 0597e9b..f2efb42 100644 (file)
@@ -117,4 +117,39 @@ class cachestore_static_test extends cachestore_tests {
             'key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'
         )));
     }
-}
\ No newline at end of file
+
+    /**
+     * Simple test to verify igbinary availability and check basic serialization is working ok.
+     */
+    public function test_igbinary_serializer() {
+        // Skip if igbinary is not available.
+        if (!extension_loaded('igbinary')) {
+            $this->markTestSkipped('Cannot test igbinary serializer. Extension missing');
+        }
+        // Prepare the static instance.
+        $defid = 'phpunit/igbinary';
+        $config = cache_config_testing::instance();
+        $config->phpunit_add_definition($defid, array(
+            'mode' => cache_store::MODE_REQUEST,
+            'component' => 'phpunit',
+            'area' => 'testigbinary'
+        ));
+        $definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
+        $instance = cachestore_static::initialise_test_instance($definition);
+        // Prepare an object.
+        $obj = new stdClass();
+        $obj->someint = 9;
+        $obj->somestring = '99';
+        $obj->somearray = [9 => 999, '99' => '9999'];
+        // Serialize and set.
+        $objser = igbinary_serialize($obj);
+        $instance->set('testigbinary', $objser);
+        // Get and unserialize.
+        $res = $instance->get('testigbinary');
+        $resunser = igbinary_unserialize($res);
+        // Check expectations.
+        $this->assertSame($objser, $res);     // Ok from cache (ig-serialized, 100% same string).
+        $this->assertEquals($obj, $resunser); // Ok ig-unserialized (equal
+        $this->assertNotSame($obj, $resunser);// but different objects, obviously).
+    }
+}