Inheritance: extends Node
 public function loadFromFile($filename, $line = null)
 {
     $oldClassnames = get_declared_classes();
     require_once $filename;
     $newClassnames = array_diff(get_declared_classes(), $oldClassnames);
     $specifications = array();
     foreach ($newClassnames as $classname) {
         $class = new ReflectionClass($classname);
         if ($class->isAbstract()) {
             continue;
         }
         if (!$class->implementsInterface('PHPSpec2\\SpecificationInterface')) {
             continue;
         }
         $preFunctions = array();
         if ($class->hasMethod('let')) {
             $preFunctions[] = $class->getMethod('let');
         }
         $postFunctions = array();
         if ($class->hasMethod('letgo')) {
             $postFunctions[] = $class->getMethod('letgo');
         }
         $specification = new Node\Specification($class->getName());
         $subject = $this->getClassSubject($class->getName());
         foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
             if (!preg_match('/^(it_|its_)/', $method->getName())) {
                 continue;
             }
             if (null !== $line && !$this->lineIsInsideMethod($line, $method)) {
                 continue;
             }
             $example = new Node\Example(str_replace('_', ' ', $method->getName()), $subject, $method);
             array_map(array($example, 'addPreFunction'), $preFunctions);
             array_map(array($example, 'addPostFunction'), $postFunctions);
             $specification->addChild($example);
         }
         if (count($specification->getChildren())) {
             $specifications[] = $specification;
         }
     }
     return $specifications;
 }
Beispiel #2
0
 public function runSpecification(Node\Specification $specification)
 {
     if (defined('PHPSPEC_ERROR_REPORTING')) {
         $errorLevel = PHPSPEC_ERROR_REPORTING;
     } else {
         $errorLevel = E_ALL ^ E_STRICT;
     }
     $oldHandler = set_error_handler(array($this, 'errorHandler'), $errorLevel);
     $this->eventDispatcher->dispatch('beforeSpecification', new Event\SpecificationEvent($specification));
     $startTime = microtime(true);
     $result = Event\ExampleEvent::PASSED;
     foreach ($specification->getChildren() as $child) {
         $result = max($result, $this->runExample($child));
     }
     $this->eventDispatcher->dispatch('afterSpecification', new Event\SpecificationEvent($specification, microtime(true) - $startTime, $result));
     if (null !== $oldHandler) {
         set_error_handler($oldHandler);
     }
     return $result;
 }