Author: bschussek
Date: 2010-02-18 18:45:11 +0100 (Thu, 18 Feb 2010)
New Revision: 28113

Added:
   tools/lime/branches/2.0/lib/LimeCallback.php
   tools/lime/branches/2.0/lib/LimeDirectory.php
   tools/lime/branches/2.0/lib/LimeGlob.php
   tools/lime/branches/2.0/lib/LimeLoadable.php
Modified:
   tools/lime/branches/2.0/data/skeleton/lime.config.php
   tools/lime/branches/2.0/lib/LimeConfiguration.php
   tools/lime/branches/2.0/lib/LimeFile.php
   tools/lime/branches/2.0/lib/LimeLoader.php
   tools/lime/branches/2.0/lime.config.php
   tools/lime/branches/2.0/test/unit/LimeLabelTest.php
   tools/lime/branches/2.0/test/unit/output/LimeOutputSuiteTest.php
Log:
Simplified the LimeLoader logic and introduced new interface LimeLoadable. This 
interface is implemented by LimeFile, LimeDirectory, LimeGlob and LimeCallback

Modified: tools/lime/branches/2.0/data/skeleton/lime.config.php
===================================================================
--- tools/lime/branches/2.0/data/skeleton/lime.config.php       2010-02-18 
17:40:42 UTC (rev 28112)
+++ tools/lime/branches/2.0/data/skeleton/lime.config.php       2010-02-18 
17:45:11 UTC (rev 28113)
@@ -10,6 +10,11 @@
  * with this source code in the file LICENSE.
  */
 
+/*
+ * Sets the directory where the registered files are searched for.
+ */
+$config->setBaseDir(dirname(__FILE__));
+
 /**
  * Register your test files here. The file paths can be absolute or relative to
  * the configured base directory.
@@ -58,11 +63,6 @@
 //$config->registerDir('test', $lime);
 
 /*
- * Sets the directory where the registered files are searched for.
- */
-$config->setBaseDir(dirname(__FILE__));
-
-/*
  * If you set verbose to true, some test outputs will output additional
  * information. Only supported by some outputs.
  */

Added: tools/lime/branches/2.0/lib/LimeCallback.php
===================================================================
--- tools/lime/branches/2.0/lib/LimeCallback.php                                
(rev 0)
+++ tools/lime/branches/2.0/lib/LimeCallback.php        2010-02-18 17:45:11 UTC 
(rev 28113)
@@ -0,0 +1,53 @@
+<?php
+
+/*
+ * This file is part of the Lime framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ * (c) Bernhard Schussek <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * A callback that loads test files.
+ *
+ * The callback receives the executable and the labels passed to the 
constructor
+ * of this class as first two arguments.
+ *
+ * @author Bernhard Schussek <[email protected]>
+ */
+class LimeCallback implements LimeLoadable
+{
+  protected
+    $callback      = null,
+    $executable    = null,
+    $labels        = array();
+
+  /**
+   * Constructor.
+   *
+   * @param callable $callback          The callback which should return an
+   *                                    array of LimeFile instances
+   * @param LimeExecutable $executable  The executable that is passed to the
+   *                                    callable
+   * @param array $labels               The lables that are passed to the
+   *                                    callable
+   */
+  public function __construct($callback, LimeExecutable $executable, array 
$labels = array())
+  {
+    $this->callback = $callback;
+    $this->executable = $executable;
+    $this->labels = $labels;
+  }
+
+  /**
+   * (non-PHPdoc)
+   * @see LimeLoadable#loadFiles()
+   */
+  public function loadFiles()
+  {
+    return call_user_func($this->callback, $this->executable, $this->labels);
+  }
+}
\ No newline at end of file


Property changes on: tools/lime/branches/2.0/lib/LimeCallback.php
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: tools/lime/branches/2.0/lib/LimeConfiguration.php
===================================================================
--- tools/lime/branches/2.0/lib/LimeConfiguration.php   2010-02-18 17:40:42 UTC 
(rev 28112)
+++ tools/lime/branches/2.0/lib/LimeConfiguration.php   2010-02-18 17:45:11 UTC 
(rev 28113)
@@ -25,6 +25,7 @@
     $instances     = array();
 
   private
+    $loadables       = array(),
     $files           = array(),
     $dirs            = array(),
     $globs           = array(),
@@ -133,16 +134,6 @@
   }
 
   /**
-   * Returns the file pattern that test files should match.
-   *
-   * @return string
-   */
-  public function getFilePattern()
-  {
-    return $this->pattern;
-  }
-
-  /**
    * Sets the base directory for the registered files.
    *
    * @param string $baseDir
@@ -330,12 +321,7 @@
    */
   public function registerFile($path, LimeExecutable $executable, $labels = 
array())
   {
-    if (!is_file($path))
-    {
-      throw new InvalidArgumentException(sprintf('The file "%s" does not 
exist', $path));
-    }
-
-    $this->files[] = array($path, $executable, $labels);
+    $this->loadables[] = new LimeFile($this->getAbsolutePath($path), 
$executable, $labels);
   }
 
   /**
@@ -346,12 +332,7 @@
    */
   public function registerDir($path, LimeExecutable $executable, $labels = 
array())
   {
-    if (!is_dir($path))
-    {
-      throw new InvalidArgumentException(sprintf('The directory "%s" does not 
exist', $path));
-    }
-
-    $this->dirs[] = array($path, $executable, $labels);
+    $this->loadables[] = new LimeDirectory($this->getAbsolutePath($path), 
$this->pattern, $executable, $labels);
   }
 
   /**
@@ -362,7 +343,7 @@
    */
   public function registerGlob($glob, LimeExecutable $executable, $labels = 
array())
   {
-    $this->globs[] = array($glob, $executable, $labels);
+    $this->loadables[] = new LimeGlob($this->getAbsolutePath($glob), 
$executable, $labels);
   }
 
   /**
@@ -375,46 +356,37 @@
    */
   public function registerCallback($callback, LimeExecutable $executable, 
$labels = array())
   {
-    $this->callbacks[] = array($callback, $executable, $labels);
+    $this->loadables[] = new LimeCallback($callback, $executable, $labels);
   }
 
   /**
-   * Returns all registered files.
+   * Returns all registered loadables.
    *
    * @return array
    */
-  public function getRegisteredFiles()
+  public function getLoadables()
   {
-    return $this->files;
+    return $this->loadables;
   }
 
   /**
-   * Returns all registered directories.
+   * Returns the absolute version of the given path.
    *
-   * @return array
-   */
-  public function getRegisteredDirs()
-  {
-    return $this->dirs;
-  }
-
-  /**
-   * Returns all registered globs.
+   * If the path is not already absolute, the base directory of the
+   * configuration is prepended in front of the path.
    *
-   * @return array
+   * @param  string $path
+   * @return string
    */
-  public function getRegisteredGlobs()
+  protected function getAbsolutePath($path)
   {
-    return $this->globs;
+    if (realpath($path) != $path)
+    {
+      return $this->getBaseDir().DIRECTORY_SEPARATOR.$path;
+    }
+    else
+    {
+      return $path;
+    }
   }
-
-  /**
-   * Returns all registered callbacks.
-   *
-   * @return array
-   */
-  public function getRegisteredCallbacks()
-  {
-    return $this->callbacks;
-  }
 }
\ No newline at end of file

Added: tools/lime/branches/2.0/lib/LimeDirectory.php
===================================================================
--- tools/lime/branches/2.0/lib/LimeDirectory.php                               
(rev 0)
+++ tools/lime/branches/2.0/lib/LimeDirectory.php       2010-02-18 17:45:11 UTC 
(rev 28113)
@@ -0,0 +1,67 @@
+<?php
+
+/*
+ * This file is part of the Lime framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ * (c) Bernhard Schussek <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * A directory from which test files can be loaded.
+ *
+ * @author Bernhard Schussek <[email protected]>
+ */
+class LimeDirectory implements LimeLoadable
+{
+  protected
+    $path          = null,
+    $pattern       = null,
+    $executable    = null,
+    $labels        = array();
+
+  /**
+   * Constructor.
+   *
+   * @param string $path                The directory path
+   * @param string $pattern             The pattern that all loaded files must
+   *                                    match
+   * @param LimeExecutable $executable  The executable used to run the files
+   * @param array $labels               The labels of the files
+   */
+  public function __construct($path, $pattern, LimeExecutable $executable, 
array $labels = array())
+  {
+    if (!is_dir($path))
+    {
+      throw new InvalidArgumentException(sprintf('The directory "%s" does not 
exist', $path));
+    }
+
+    $this->path = $path;
+    $this->pattern = $pattern;
+    $this->executable = $executable;
+    $this->labels = $labels;
+  }
+
+  /**
+   * (non-PHPdoc)
+   * @see LimeLoadable#loadFiles()
+   */
+  public function loadFiles()
+  {
+    $directoryIterator = new RecursiveDirectoryIterator($path);
+    $recursiveIterator = new RecursiveIteratorIterator($iterator);
+    $filteredIterator = new RegexIterator($iterator, $this->pattern);
+
+    $files = array();
+
+    foreach (iterator_to_array($filteredIterator) as $path)
+    {
+      $files[] = new LimeFile($path, $this->executable, $this->labels);
+    }
+
+    return $files;
+  }
+}
\ No newline at end of file


Property changes on: tools/lime/branches/2.0/lib/LimeDirectory.php
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: tools/lime/branches/2.0/lib/LimeFile.php
===================================================================
--- tools/lime/branches/2.0/lib/LimeFile.php    2010-02-18 17:40:42 UTC (rev 
28112)
+++ tools/lime/branches/2.0/lib/LimeFile.php    2010-02-18 17:45:11 UTC (rev 
28113)
@@ -17,7 +17,7 @@
  *
  * @author Bernhard Schussek <[email protected]>
  */
-class LimeFile
+class LimeFile implements LimeLoadable
 {
   private
     $path          = null,
@@ -29,10 +29,16 @@
    *
    * @param string $path  The path to the file
    */
-  public function __construct($path, LimeExecutable $executable)
+  public function __construct($path, LimeExecutable $executable, array $labels 
= array())
   {
-    $this->path = $path;
+    if (!is_file($path))
+    {
+      throw new InvalidArgumentException(sprintf('The file "%s" does not 
exist', $path));
+    }
+
+    $this->path = realpath($path);
     $this->executable = $executable;
+    $this->labels = $labels;
   }
 
   /**
@@ -58,9 +64,9 @@
   /**
    * Adds the given labels to the file.
    *
-   * @param array $la bels
+   * @param array $labels
    */
-  public function addLabels($labels)
+  public function addLabels(array $labels)
   {
     $this->labels = array_merge($this->labels, $labels);
   }
@@ -74,4 +80,12 @@
   {
     return array_values(array_unique($this->labels));
   }
+
+  /**
+   * @see LimeLoadable#loadFiles()
+   */
+  public function loadFiles()
+  {
+    return array($this);
+  }
 }
\ No newline at end of file

Added: tools/lime/branches/2.0/lib/LimeGlob.php
===================================================================
--- tools/lime/branches/2.0/lib/LimeGlob.php                            (rev 0)
+++ tools/lime/branches/2.0/lib/LimeGlob.php    2010-02-18 17:45:11 UTC (rev 
28113)
@@ -0,0 +1,54 @@
+<?php
+
+/*
+ * This file is part of the Lime framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ * (c) Bernhard Schussek <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * A glob for loading test files.
+ *
+ * @author Bernhard Schussek <[email protected]>
+ */
+class LimeGlob implements LimeLoadable
+{
+  protected
+    $glob          = null,
+    $executable    = null,
+    $labels        = array();
+
+  /**
+   * Constructor.
+   *
+   * @param string $glob                A valid glob string
+   * @param LimeExecutable $executable  The executable used to run the files
+   * @param array $labels               The labels of the files
+   */
+  public function __construct($glob, LimeExecutable $executable, array $labels 
= array())
+  {
+    $this->glob = $glob;
+    $this->executable = $executable;
+    $this->labels = $labels;
+  }
+
+  /**
+   * (non-PHPdoc)
+   * @see LimeLoadable#loadFiles()
+   */
+  public function loadFiles()
+  {
+    $files = array();
+
+    foreach (glob($this->glob) as $path)
+    {
+      $files[] = new LimeFile($path, $this->executable, $this->labels);
+    }
+
+    return $files;
+  }
+}
\ No newline at end of file


Property changes on: tools/lime/branches/2.0/lib/LimeGlob.php
___________________________________________________________________
Added: svn:keywords
   + Id

Added: tools/lime/branches/2.0/lib/LimeLoadable.php
===================================================================
--- tools/lime/branches/2.0/lib/LimeLoadable.php                                
(rev 0)
+++ tools/lime/branches/2.0/lib/LimeLoadable.php        2010-02-18 17:45:11 UTC 
(rev 28113)
@@ -0,0 +1,26 @@
+<?php
+
+/*
+ * This file is part of the Lime framework.
+ *
+ * (c) Fabien Potencier <[email protected]>
+ * (c) Bernhard Schussek <[email protected]>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * Is able to load test files with their labels and executables.
+ *
+ * @author Bernhard Schussek <[email protected]>
+ */
+interface LimeLoadable
+{
+  /**
+   * Loads and returns the test files.
+   *
+   * @return array  An array of LimeFile instances
+   */
+  public function loadFiles();
+}
\ No newline at end of file


Property changes on: tools/lime/branches/2.0/lib/LimeLoadable.php
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: tools/lime/branches/2.0/lib/LimeLoader.php
===================================================================
--- tools/lime/branches/2.0/lib/LimeLoader.php  2010-02-18 17:40:42 UTC (rev 
28112)
+++ tools/lime/branches/2.0/lib/LimeLoader.php  2010-02-18 17:45:11 UTC (rev 
28113)
@@ -36,168 +36,61 @@
   {
     $this->configuration = $configuration;
 
-    foreach ($configuration->getRegisteredFiles() as $file)
+    foreach ($configuration->getLoadables() as $loadable)
     {
-      $this->loadFile($this->getAbsolutePath($file[0]), $file[1], $file[2]);
+      $this->load($loadable);
     }
-    foreach ($configuration->getRegisteredDirs() as $dir)
-    {
-      $this->loadDir($this->getAbsolutePath($dir[0]), $dir[1], $dir[2]);
-    }
-    foreach ($configuration->getRegisteredGlobs() as $glob)
-    {
-      $this->loadGlob($this->getAbsolutePath($glob[0]), $glob[1], $glob[2]);
-    }
-    foreach ($configuration->getRegisteredCallbacks() as $callback)
-    {
-      $this->loadFile($callback[0], $callback[1], $callback[2]);
-    }
   }
 
   /**
-   * Returns the absolute version of the given path.
-   *
-   * If the path is not already absolute, the base directory of the
-   * configuration is prepended in front of the path.
-   *
-   * @param  string $path
-   * @return string
-   */
-  protected function getAbsolutePath($path)
-  {
-    if (realpath($path) != $path)
-    {
-      return $this->configuration->getBaseDir().DIRECTORY_SEPARATOR.$path;
-    }
-    else
-    {
-      return $path;
-    }
-  }
-
-  /**
    * Registers a test file path in the test suite.
    *
    * @param string $path
    * @param array $labels
    */
-  protected function loadFile($path, LimeExecutable $executable, $labels = 
array())
+  public function load(LimeLoadable $loadable)
   {
-    if (!is_file($path))
+    foreach ($loadable->loadFiles() as $file)
     {
-      throw new InvalidArgumentException(sprintf('The file "%s" does not 
exist', $path));
-    }
+      $path = $file->getPath();
+      $name = basename($path, $this->configuration->getSuffix());
 
-    $path = realpath($path);
-    $name = basename($path, $this->configuration->getSuffix());
-
-    if (!isset($this->files[$path]))
-    {
-      $this->files[$path] = new LimeFile($path, $executable);
-
-      if (!isset($this->filesByName[$name]))
+      if (!isset($this->files[$path]))
       {
-        $this->filesByName[$name] = array();
-      }
+        $this->files[$path] = $file;
 
-      // allow multiple files with the same name
-      $this->filesByName[$name][] = $this->files[$path];
-    }
+        if (!isset($this->filesByName[$name]))
+        {
+          $this->filesByName[$name] = array();
+        }
 
-    $labels = (array)$labels;
-
-    $this->files[$path]->addLabels($labels);
-
-    foreach ($labels as $label)
-    {
-      if (!isset($this->labels[$label]))
-      {
-        $this->labels[$label] = new LimeLabel();
+        // allow multiple files with the same name
+        $this->filesByName[$name][] = $file;
       }
-
-      $this->labels[$label]->addFile($this->files[$path]);
-    }
-  }
-
-  /**
-   * Loads an array of test file paths in the test suite.
-   *
-   * @param array $paths
-   * @param array $labels
-   */
-  protected function loadFiles(array $paths, LimeExecutable $executable, 
$labels = array())
-  {
-    foreach ($paths as $path)
-    {
-      if (is_dir($path))
-      {
-        $this->loadDir($path, $executable, $labels);
-      }
       else
       {
-        $this->loadFile($path, $executable, $labels);
+        // merge labels into existing files
+        $this->files[$path]->addLabels($file->getLabels());
       }
-    }
-  }
 
-  /**
-   * Loads the content of a directory in the test suite.
-   *
-   * Only files with the configures suffix will be added.
-   *
-   * @param string $path
-   * @param array $labels
-   */
-  protected function loadDir($path, LimeExecutable $executable, $labels = 
array())
-  {
-    $iterator = new DirectoryIterator($path);
-
-    foreach ($iterator as $file)
-    {
-      if (!$file->isDot())
+      foreach ($file->getLabels() as $label)
       {
-        if ($file->isDir())
+        if (!isset($this->labels[$label]))
         {
-          $this->loadDir($file->getPathname(), $executable, $labels);
+          $this->labels[$label] = new LimeLabel();
         }
-        else if (preg_match($this->configuration->getFilePattern(), 
$file->getFilename()))
-        {
-          $this->loadFile($file->getPathname(), $executable, $labels);
-        }
+
+        $this->labels[$label]->addFile($this->files[$path]);
       }
     }
   }
 
   /**
-   * Loads all files matched by the given glob in the test suite.
+   * Returns whether the given label exists.
    *
-   * @param string $glob
-   * @param array $labels
+   * @param  string $label
+   * @return boolean
    */
-  protected function loadGlob($glob, LimeExecutable $executable, $labels = 
array())
-  {
-    if ($files = glob($glob))
-    {
-      $this->loadFiles($files, $executable, $labels);
-    }
-  }
-
-  /**
-   * Adds the results of a callback to be added to the test suite.
-   *
-   * The callback should return an array of file paths.
-   *
-   * @param callable $callback
-   * @param array $labels
-   */
-  protected function loadCallback($callback, LimeExecutable $executable, 
$labels = array())
-  {
-    if ($files = call_user_func($callback))
-    {
-      $this->loadFiles($files, $executable, $labels);
-    }
-  }
-
   public function isLabel($label)
   {
     preg_match('/^[+-]?(.+)$/', $label, $matches);

Modified: tools/lime/branches/2.0/lime.config.php
===================================================================
--- tools/lime/branches/2.0/lime.config.php     2010-02-18 17:40:42 UTC (rev 
28112)
+++ tools/lime/branches/2.0/lime.config.php     2010-02-18 17:45:11 UTC (rev 
28113)
@@ -10,6 +10,11 @@
  * with this source code in the file LICENSE.
  */
 
+/*
+ * Sets the directory where the registered files are searched for.
+ */
+$config->setBaseDir(dirname(__FILE__));
+
 /**
  * Register your test files here. The file paths can be absolute or relative to
  * the configured base directory.
@@ -57,11 +62,6 @@
 $config->registerFile('test/bin/prove.sh', $phpt);
 
 /*
- * Sets the directory where the registered files are searched for.
- */
-$config->setBaseDir(dirname(__FILE__));
-
-/*
  * If you set verbose to true, some test outputs will output additional
  * information. Only supported by some outputs.
  */

Modified: tools/lime/branches/2.0/test/unit/LimeLabelTest.php
===================================================================
--- tools/lime/branches/2.0/test/unit/LimeLabelTest.php 2010-02-18 17:40:42 UTC 
(rev 28112)
+++ tools/lime/branches/2.0/test/unit/LimeLabelTest.php 2010-02-18 17:45:11 UTC 
(rev 28113)
@@ -19,23 +19,31 @@
 
 // @BeforeAll
 
-  $executable = $t->stub('LimeExecutable');
+  $file1 = $t->stub('LimeFile');
+  $file1->getPath()->returns('test1.txt');
+  $file1->replay();
+  $file2 = $t->stub('LimeFile');
+  $file2->getPath()->returns('test2.txt');
+  $file2->replay();
+  $file3 = $t->stub('LimeFile');
+  $file3->getPath()->returns('test3.txt');
+  $file3->replay();
 
 
 // @Before
 
   $label1 = new LimeLabel();
-  $label1->addFile(new LimeFile('test1.txt', $executable));
-  $label1->addFile(new LimeFile('test2.txt', $executable));
+  $label1->addFile($file1);
+  $label1->addFile($file2);
   $label2 = new LimeLabel();
-  $label2->addFile(new LimeFile('test1.txt', $executable));
-  $label2->addFile(new LimeFile('test3.txt', $executable));
+  $label2->addFile($file1);
+  $label2->addFile($file3);
 
 
 // @Test: intersect() returns the intersection of two labels
 
   $expected = new LimeLabel();
-  $expected->addFile(new LimeFile('test1.txt', $executable));
+  $expected->addFile($file1);
   $actual = $label1->intersect($label2);
   $t->is($actual, $expected, 'The intersection is correct');
 
@@ -43,9 +51,9 @@
 // @Test: add() returns the sum of two labels
 
   $expected = new LimeLabel();
-  $expected->addFile(new LimeFile('test1.txt', $executable));
-  $expected->addFile(new LimeFile('test2.txt', $executable));
-  $expected->addFile(new LimeFile('test3.txt', $executable));
+  $expected->addFile($file1);
+  $expected->addFile($file2);
+  $expected->addFile($file3);
   $actual = $label1->add($label2);
   $t->is($actual, $expected, 'The sum is correct');
 
@@ -53,6 +61,6 @@
 // @Test: subtract() returns the first label without the second
 
   $expected = new LimeLabel();
-  $expected->addFile(new LimeFile('test2.txt', $executable));
+  $expected->addFile($file2);
   $actual = $label1->subtract($label2);
   $t->is($actual, $expected, 'The subtraction is correct');
\ No newline at end of file

Modified: tools/lime/branches/2.0/test/unit/output/LimeOutputSuiteTest.php
===================================================================
--- tools/lime/branches/2.0/test/unit/output/LimeOutputSuiteTest.php    
2010-02-18 17:40:42 UTC (rev 28112)
+++ tools/lime/branches/2.0/test/unit/output/LimeOutputSuiteTest.php    
2010-02-18 17:45:11 UTC (rev 28113)
@@ -54,8 +54,9 @@
   $printer->printText(str_pad(' script', 57, '.'));
   $printer->printLine("ok", LimePrinter::OK);
   $printer->replay();
-  $file = new LimeFile('path', $t->stub('LimeExecutable'));
-  $file->addLabels(array('label1', 'label2'));
+  $file = $t->stub('LimeFile');
+  $file->getLabels()->returns(array('label1', 'label2'));
+  $file->replay();
   $loader = $t->stub('LimeLoader');
   $loader->method('getFileByPath')->returns($file);
   $loader->replay();

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to