Просмотр исходного кода

Protocol-based test with a protocol other than "file://"

Olav Schettler 9 лет назад
Родитель
Сommit
eeee916433
2 измененных файлов с 71 добавлено и 1 удалено
  1. 1 1
      test/Mustache/Test/Loader/FilesystemLoaderTest.php
  2. 70 0
      test/bootstrap.php

+ 1 - 1
test/Mustache/Test/Loader/FilesystemLoaderTest.php

@@ -34,7 +34,7 @@ class Mustache_Test_Loader_FilesystemLoaderTest extends PHPUnit_Framework_TestCa
     {
         $baseDir = realpath(dirname(__FILE__) . '/../../../fixtures/templates');
 
-        $loader = new Mustache_Loader_FilesystemLoader('file://' . $baseDir, array('extension' => '.ms'));
+        $loader = new Mustache_Loader_FilesystemLoader('test://' . $baseDir, array('extension' => '.ms'));
         $this->assertEquals('alpha contents', $loader->load('alpha'));
         $this->assertEquals('beta contents', $loader->load('beta.ms'));
     }

+ 70 - 0
test/bootstrap.php

@@ -15,3 +15,73 @@ Mustache_Autoloader::register();
 Mustache_Autoloader::register(dirname(__FILE__) . '/../test');
 
 require dirname(__FILE__) . '/../vendor/yaml/lib/sfYamlParser.php';
+
+/**
+ * Minimal stream wrapper to test protocol-based access to templates
+ */
+class TestStream
+{
+    private $filehandle;
+
+    /**
+     * Always returns false
+     *
+     * @param string $path
+     * @param int $flags
+     * @return array
+     */
+    public function url_stat($path, $flags)
+    {
+        return false;
+    }
+
+    /**
+     * Open the file
+     *
+     * @param string $path
+     * @param string $mode
+     * @return bool
+     */
+    public function stream_open($path, $mode)
+    {
+        $path = preg_replace('-^test://-', '', $path);
+        $this->filehandle = fopen($path, $mode);
+        return $this->filehandle !== false;
+    }
+
+    /**
+     * @return array
+     */
+    public function stream_stat()
+    {
+        return [];
+    }
+
+    /**
+     * @param int $count
+     * @return string
+     */
+    public function stream_read($count)
+    {
+        return fgets($this->filehandle, $count);
+    }
+
+    /**
+     * @return bool
+     */
+    public function stream_eof()
+    {
+        return feof($this->filehandle);
+    }
+
+    /**
+     * @return bool
+     */
+    public function stream_close()
+    {
+        return fclose($this->filehandle);
+    }
+}
+
+stream_wrapper_register('test', TestStream::class)
+    || die('Failed to register protocol');