1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179:
<?php
namespace Peridot\Runner;
use Evenement\EventEmitter;
use Peridot\Core\HasEventEmitterTrait;
use Peridot\Core\Test;
use Peridot\Core\Suite;
final class Context
{
use HasEventEmitterTrait;
protected $suites;
protected $file;
private static $instance = null;
private function __construct()
{
$this->clear();
}
public function clear()
{
$this->suites = [new Suite("", function () {
}, false)];
}
public function setFile($path)
{
$this->file = $path;
}
public function getFile()
{
return $this->file;
}
public function getCurrentSuite()
{
return $this->suites[0];
}
public function addSuite($description, callable $fn, $pending = null, $focused = false)
{
$suite = $this->createSuite($description, $fn, $pending, $focused);
$this->getCurrentSuite()->addTest($suite);
array_unshift($this->suites, $suite);
$suite->define();
array_shift($this->suites);
return $suite;
}
public function addTest($description, callable $fn = null, $pending = null, $focused = false)
{
$test = new Test($description, $fn, $focused);
if ($pending !== null) {
$test->setPending($pending);
}
$test->setFile($this->file);
$this->getCurrentSuite()->addTest($test);
return $test;
}
public function addSetupFunction(callable $fn)
{
$this->getCurrentSuite()->addSetupFunction($fn);
}
public function addTearDownFunction(callable $fn)
{
$this->getCurrentSuite()->addTearDownFunction($fn);
}
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new Context();
}
return self::$instance;
}
private function createSuite($description, callable $fn, $pending, $focused)
{
$suite = new Suite($description, $fn, $focused);
if ($pending !== null) {
$suite->setPending($pending);
}
$suite->setFile($this->file);
$suite->setEventEmitter($this->getEventEmitter());
return $suite;
}
}