Created
October 17, 2013 19:10
-
-
Save jubianchi/7030507 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| From 244d1b4f55116553ca5d529b98251ee643e17340 Mon Sep 17 00:00:00 2001 | |
| From: jubianchi <[email protected]> | |
| Date: Thu, 17 Oct 2013 21:09:27 +0200 | |
| Subject: [PATCH] Enable prefix annotation | |
| --- | |
| classes/test.php | 31 +++++++++++++++++++++++++------ | |
| 1 file changed, 25 insertions(+), 6 deletions(-) | |
| diff --git a/classes/test.php b/classes/test.php | |
| index 21b3f05..6d1fe3b 100644 | |
| --- a/classes/test.php | |
| +++ b/classes/test.php | |
| @@ -49,6 +49,7 @@ abstract class test implements observable, \countable | |
| private $testedClassPath = null; | |
| private $currentMethod = null; | |
| private $testNamespace = null; | |
| + private $testMethodPrefix = null; | |
| private $classEngine = null; | |
| private $bootstrapFile = null; | |
| private $maxAsynchronousEngines = null; | |
| @@ -75,7 +76,7 @@ abstract class test implements observable, \countable | |
| private static $namespace = null; | |
| private static $defaultEngine = self::defaultEngine; | |
| - protected static $testMethodPrefix = null; | |
| + protected static $methodPrefix = null; | |
| public function __construct(adapter $adapter = null, annotations\extractor $annotationExtractor = null, asserter\generator $asserterGenerator = null, test\assertion\manager $assertionManager = null, \closure $reflectionClassFactory = null) | |
| { | |
| @@ -726,21 +727,38 @@ abstract class test implements observable, \countable | |
| return $this->path; | |
| } | |
| - public static function getTestMethodPrefix() | |
| + public static function getMethodPrefix() | |
| { | |
| - return static::$testMethodPrefix ?: static::defaultTestMethodPrefix; | |
| + return static::$methodPrefix ?: static::defaultTestMethodPrefix; | |
| } | |
| - public static function setTestMethodPrefix($prefix) | |
| + public static function setMethodPrefix($prefix) | |
| { | |
| - static::$testMethodPrefix = (string) $prefix; | |
| + static::$methodPrefix = (string) $prefix; | |
| - if (static::$testMethodPrefix === '') | |
| + if (static::$methodPrefix === '') | |
| { | |
| throw new exceptions\logic\invalidArgument('Test method prefix must not be empty'); | |
| } | |
| } | |
| + public function setTestMethodPrefix($testMethodPrefix) | |
| + { | |
| + $this->testMethodPrefix = self::cleanNamespace($testMethodPrefix); | |
| + | |
| + if ($this->testMethodPrefix === '') | |
| + { | |
| + throw new exceptions\logic\invalidArgument('Test namespace must not be empty'); | |
| + } | |
| + | |
| + return $this; | |
| + } | |
| + | |
| + public function getTestMethodPrefix() | |
| + { | |
| + return $this->testMethodPrefix ?: self::getMethodPrefix(); | |
| + } | |
| + | |
| public function getTaggedTestMethods(array $methods, array $tags = array()) | |
| { | |
| return array_values(array_uintersect($methods, $this->getTestMethods($tags), 'strcasecmp')); | |
| @@ -1182,6 +1200,7 @@ abstract class test implements observable, \countable | |
| ->setHandler('ignore', function($value) use ($test) { $test->ignore(annotations\extractor::toBoolean($value)); }) | |
| ->setHandler('tags', function($value) use ($test) { $test->setTags(annotations\extractor::toArray($value)); }) | |
| ->setHandler('namespace', function($value) use ($test) { $test->setTestNamespace($value); }) | |
| + ->setHandler('prefix', function($value) use ($test) { $test->setTestMethodPrefix($value); }) | |
| ->setHandler('maxChildrenNumber', function($value) use ($test) { $test->setMaxChildrenNumber($value); }) | |
| ->setHandler('engine', function($value) use ($test) { $test->setClassEngine($value); }) | |
| ->setHandler('hasVoidMethods', function($value) use ($test) { $test->classHasVoidMethods(); }) | |
| -- | |
| 1.8.4 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace | |
| { | |
| use mageekguy\atoum; | |
| require_once __DIR__ . '/../vendor/autoload.php'; | |
| class locker | |
| { | |
| protected $adapter; | |
| public function __construct(atoum\adapter $adapter = null) | |
| { | |
| $this->adapter = $adapter ?: new atoum\adapter(); | |
| } | |
| public function lock($handle) | |
| { | |
| if(false === $this->adapter->flock($handle, LOCK_EX)) { | |
| throw new \RuntimeException('Could not lock file'); | |
| } | |
| return $this; | |
| } | |
| public function unlock($handle) | |
| { | |
| if(false === $this->adapter->flock($handle, LOCK_UN)) { | |
| throw new \RuntimeException('Could not unlock file'); | |
| } | |
| return $this; | |
| } | |
| } | |
| class writer | |
| { | |
| protected $path; | |
| protected $locker; | |
| protected $adapter; | |
| protected $handle; | |
| public function __construct($path, locker $locker = null, atoum\adapter $adapter = null) | |
| { | |
| $this->path = $path; | |
| $this->adapter = $adapter ?: new atoum\adapter(); | |
| $this->locker = $locker ?: new locker($this->adapter); | |
| } | |
| public function getPath() | |
| { | |
| return $this->path; | |
| } | |
| public function open() | |
| { | |
| if (false === file_exists($this->path)) | |
| { | |
| throw new \RuntimeException(sprintf('File %s does not exist', $this->path)); | |
| } | |
| $this->handle = $this->adapter->fopen($this->path, 'w'); | |
| if (false === $this->handle) | |
| { | |
| $this->handle = null; | |
| throw new \RuntimeException(sprintf('Could not open %s for writing', $this->path)); | |
| } | |
| try | |
| { | |
| $this->locker->lock($this->handle); | |
| } | |
| catch (\RuntimeException $exception) | |
| { | |
| $this->close(); | |
| throw new \RuntimeException(sprintf('Could not lock %s', $this->path), null, $exception); | |
| } | |
| return $this; | |
| } | |
| public function write($str) | |
| { | |
| if (false === $this->adapter->is_writable($this->isOpen()->path)) | |
| { | |
| throw new \RuntimeException(sprintf('File %s is not writable', $this->path)); | |
| } | |
| if(false === $this->adapter->fwrite($this->handle, $str)) | |
| { | |
| throw new \RuntimeException(sprintf('Could not write to %s', $this->path)); | |
| } | |
| return $this; | |
| } | |
| public function close() | |
| { | |
| try | |
| { | |
| $this->locker->unlock($this->isOpen()->handle); | |
| } | |
| catch (\RuntimeException $exception) {} | |
| $this->adapter->fclose($this->handle); | |
| $this->handle = null; | |
| return $this; | |
| } | |
| protected function isOpen() | |
| { | |
| if (null === $this->handle) | |
| { | |
| throw new \LogicException(sprintf('File %s was not opened', $this->path)); | |
| } | |
| return $this; | |
| } | |
| } | |
| } | |
| namespace tests\units | |
| { | |
| use | |
| mageekguy\atoum, | |
| mageekguy\atoum\mock\streams\fs, | |
| locker as testedLocker, | |
| writer as testedWriter | |
| ; | |
| class locker extends atoum | |
| { | |
| const defaultTestMethodPrefix = 'it'; | |
| public function it_should_lock_file() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->and($handle = fopen((string) $file, 'r')) | |
| ->if($locker = new testedLocker()) | |
| ->then | |
| ->object($locker->lock($handle))->isIdenticalTo($locker) | |
| ->adapter($file) | |
| ->call('stream_lock')->withArguments(LOCK_EX)->once() | |
| ; | |
| } | |
| public function it_should_throw_exception_if_lock_fails() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->and($handle = fopen((string) $file, 'r')) | |
| ->and($adapter = new atoum\test\adapter()) | |
| ->and($adapter->flock = false) | |
| ->if($locker = new testedLocker($adapter)) | |
| ->then | |
| ->exception(function() use ($locker, $handle) { | |
| $locker->lock($handle); | |
| }) | |
| ->isInstanceOf('RuntimeException') | |
| ->hasMessage('Could not lock file') | |
| ; | |
| } | |
| public function it_should_unlock_file() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->and($handle = fopen((string) $file, 'r')) | |
| ->if($locker = new testedLocker()) | |
| ->then | |
| ->object($locker->unlock($handle))->isIdenticalTo($locker) | |
| ->adapter($file) | |
| ->call('stream_lock')->withArguments(LOCK_UN)->once() | |
| ; | |
| } | |
| public function it_should_throw_exception_if_unlock_fails() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->and($handle = fopen((string) fs\file::get(), 'r')) | |
| ->and($adapter = new atoum\test\adapter()) | |
| ->and($adapter->flock = false) | |
| ->if($locker = new testedLocker($adapter)) | |
| ->then | |
| ->exception(function() use ($locker, $handle) { | |
| $locker->unlock($handle); | |
| }) | |
| ->isInstanceOf('RuntimeException') | |
| ->hasMessage('Could not unlock file') | |
| ; | |
| } | |
| } | |
| /** | |
| * @prefix it | |
| */ | |
| class writer extends atoum | |
| { | |
| public function it_should_construct() | |
| { | |
| $this | |
| ->if($writer = new testedWriter($path = uniqid())) | |
| ->then | |
| ->string($writer->getPath())->isEqualTo($path) | |
| ; | |
| } | |
| public function it_should_throw_exception_if_file_does_not_exist() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->and($locker = new \mock\locker()) | |
| ->and($writer = new testedWriter((string) $file, $locker)) | |
| ->if($file->notExists()) | |
| ->then | |
| ->exception(function() use ($writer) { | |
| $writer->open(); | |
| }) | |
| ->isInstanceOf('RuntimeException') | |
| ->hasMessage(sprintf('File %s does not exist', $file)) | |
| ; | |
| } | |
| public function it_should_throw_exception_if_open_fails() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->and($adapter = new atoum\test\adapter()) | |
| ->and($locker = new \mock\locker($adapter)) | |
| ->and($writer = new testedWriter((string) $file, $locker, $adapter)) | |
| ->and($adapter->fopen = false) | |
| ->then | |
| ->exception(function() use ($writer) { | |
| $writer->open(); | |
| }) | |
| ->isInstanceOf('RuntimeException') | |
| ->hasMessage(sprintf('Could not open %s for writing', $file)) | |
| ; | |
| } | |
| public function it_should_throw_exception_and_close_file_if_lock_fails() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->and($adapter = new atoum\test\adapter()) | |
| ->and($locker = new \mock\locker($adapter)) | |
| ->and($this->calling($locker)->lock->throw = $exception = new \RuntimeException()) | |
| ->if($writer = new testedWriter((string) $file, $locker)) | |
| ->then | |
| ->exception(function() use ($writer) { | |
| $writer->open(); | |
| }) | |
| ->isInstanceOf('RuntimeException') | |
| ->hasMessage(sprintf('Could not lock %s', $file)) | |
| ->object($this->exception->getPrevious())->isIdenticalTo($exception) | |
| ->adapter($file) | |
| ->call('stream_close')->withArguments() | |
| ->afterFunctionCall('stream_open')->withArguments((string) $file, 'w') | |
| ->afterFunctionCall('stream_lock')->withArguments(LOCK_UN) | |
| ->once() | |
| ; | |
| } | |
| public function it_should_open_and_lock_file() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->if($writer = new testedWriter((string) $file)) | |
| ->then | |
| ->object($writer->open())->isIdenticalTo($writer) | |
| ->adapter($file) | |
| ->call('stream_open')->withArguments((string) $file, 'w') | |
| ->beforeFunctionCall('stream_lock')->withArguments(LOCK_EX) | |
| ->once() | |
| ; | |
| } | |
| public function it_should_throw_exception_on_write_if_file_is_not_opened() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->if($writer = new testedWriter((string) $file)) | |
| ->then | |
| ->exception(function() use ($writer) { | |
| $writer->write(uniqid()); | |
| }) | |
| ->isInstanceOf('LogicException') | |
| ->hasMessage(sprintf('File %s was not opened', $file)) | |
| ; | |
| } | |
| public function it_should_throw_exception_if_file_is_not_writeable() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->and($adapter = new atoum\test\adapter()) | |
| ->if($writer = new testedWriter((string) $file, null, $adapter)) | |
| ->and($writer->open()) | |
| ->and($adapter->is_writable = false) | |
| ->then | |
| ->exception(function() use ($writer) { | |
| $writer->write(uniqid()); | |
| }) | |
| ->isInstanceOf('RuntimeException') | |
| ->hasMessage(sprintf('File %s is not writable', $file)) | |
| ; | |
| } | |
| public function it_should_throw_exception_if_write_fails() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->and($adapter = new atoum\test\adapter()) | |
| ->if($writer = new testedWriter((string) $file, null, $adapter)) | |
| ->and($writer->open()) | |
| ->and($adapter->fwrite = false) | |
| ->then | |
| ->exception(function() use ($writer) { | |
| $writer->write(uniqid()); | |
| }) | |
| ->isInstanceOf('RuntimeException') | |
| ->hasMessage(sprintf('Could not write to %s', $file)) | |
| ; | |
| } | |
| public function it_should_write_to_file() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->if($writer = new testedWriter((string) $file)) | |
| ->and($writer->open()) | |
| ->then | |
| ->object($writer->write($str = uniqid()))->isIdenticalTo($writer) | |
| ->adapter($file) | |
| ->call('stream_write')->withArguments($str)->once() | |
| ; | |
| } | |
| public function it_should_throw_exception_on_close_if_file_is_not_opened() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->if($writer = new testedWriter((string) $file)) | |
| ->then | |
| ->exception(function() use ($writer) { | |
| $writer->close(); | |
| }) | |
| ->isInstanceOf('LogicException') | |
| ->hasMessage(sprintf('File %s was not opened', $file)) | |
| ; | |
| } | |
| public function it_should_unlock_and_close_file() | |
| { | |
| $this | |
| ->given($file = fs\file::get()) | |
| ->if($writer = new testedWriter((string) $file)) | |
| ->and($writer->open()) | |
| ->then | |
| ->object($writer->close())->isIdenticalTo($writer) | |
| ->adapter($file) | |
| ->call('stream_close') | |
| ->afterFunctionCall('stream_lock')->withArguments(LOCK_UN) | |
| ->once() | |
| ; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment