/** * Tests getting a new wrapper instance * * Tests WP_Stream::new_wrapper_instance() */ public function test_new_wrapper_instance() { $uri = 'test://example/path1/path2/hello_world.txt'; $wrapper_object = WP_Stream::new_wrapper_instance($uri); $this->assertTrue(is_object($wrapper_object)); unset($wrapper_object); // Call should fail if only a scheme is provided $this->assertFalse(WP_Stream::new_wrapper_instance('test')); // Call should fail if no registered wrapper is available $this->assertFalse(WP_Stream::new_wrapper_instance('hullabloothiswrapperdoesnotexist')); }
/** * Tests setting and getting URI * * Tests the $uri attribute of */ public function test_uri_attribute() { $uri = 'test://testfile.txt'; $scheme = WP_Stream::scheme($uri); $class_name = WP_Stream::wrapper_class_name($scheme); $instance = WP_Stream::new_wrapper_instance($uri); $this->assertEquals($uri, $instance->get_uri()); $new_uri = 'test://testfile2.txt'; $instance->set_uri($new_uri); $this->assertEquals($new_uri, $instance->get_uri()); }
/** * Setup this test case */ function setUp() { // Register test stream wrapper wp_test_stream_wrapper_register(); /** * Initialize instance variables */ $wrapper = WP_Stream::new_wrapper_instance('test://'); $this->test_dir = $wrapper->get_wrapper_path(); $this->filename = 'testfile.txt'; $this->uri = 'test://' . $this->filename; $this->path = $this->test_dir . '/' . $this->filename; $this->expected_contents = 'The miracle is this - the more we share, the more we have. -- Leonard Nimoy'; /** * Setup and assert starting evironment */ if (file_exists($this->uri)) { unlink($this->uri); } $this->assertFileNotExists($this->uri); $this->assertFileNotExists($this->path); // Open and write to the file $fh = fopen($this->uri, 'w'); fwrite($fh, $this->expected_contents); fclose($fh); $this->assertFileExists($this->path); $this->assertEquals(filesize($this->uri), filesize($this->path)); }
/** * Returns directory name component of path * * PHP's dirname() does not support stream wrappers. This helper function * properly adds this missing support. * * This function is fully compatible with PHP's dirname() function and may be * called in the same way. For example, both a URI and a normal filepath * can be provided for the $uri parameter. * * @param string $uri * the URI or path to file. * * @return string * the new temporary filename, or false on failure. * * @link http://us2.php.net/manual/en/function.dirname.php * @see dirname() * @since 1.0.0 */ function wp_dirname($uri) { $scheme = WP_Stream::scheme($uri); if ($scheme && WP_Stream::scheme_valid($scheme)) { return WP_Stream::new_wrapper_instance($scheme . '://')->dirname($uri); } else { return dirname($uri); } }