Permalink
Browse files

Add includer.

  • Loading branch information...
1 parent 6766de7 commit 9352e19e4523ab988611e836c3f3d3a85d095244 @mageekguy committed Nov 30, 2011
View
@@ -1,6 +1,8 @@
+/G*
/*.un~
/vim.php
/config.php
+/.atoum.php
/.buildpath
/.project
/.settings/org.eclipse.php.core.prefs
@@ -10,13 +10,14 @@ class error extends \mageekguy\atoum\asserter
{
protected $message = null;
protected $type = null;
+ protected $messageIsPattern = false;
public function setWith($message = null, $type = null)
{
- $this->message = $message;
- $this->type = $type;
-
- return $this;
+ return $this
+ ->withType($type)
+ ->withMessage($message)
+ ;
}
public function getMessage()
@@ -31,11 +32,9 @@ public function getType()
public function exists()
{
- $key = null;
-
$score = $this->getScore();
- $key = $score->errorExists($this->message, $this->type);
+ $key = $score->errorExists($this->message, $this->type, $this->messageIsPattern);
if ($key !== null)
{
@@ -70,6 +69,87 @@ public function exists()
return $this;
}
+ public function notExists()
+ {
+ $score = $this->getScore();
+
+ $key = $score->errorExists($this->message, $this->type, $this->messageIsPattern);
+
+ if ($key === null)
+ {
+ $this->pass();
+ }
+ else
+ {
+ $failReason = '';
+
+ switch (true)
+ {
+ case $this->type === null && $this->message === null:
+ $failReason = $this->getLocale()->_('error exists');
+ break;
+
+ case $this->type === null && $this->message !== null:
+ $failReason = sprintf($this->getLocale()->_('error with message \'%s\' exists'), $this->message);
+ break;
+
+ case $this->type !== null && $this->message === null:
+ $failReason = sprintf($this->getLocale()->_('error of type %s exists'), self::getAsString($this->type));
+ break;
+
+ default:
+ $failReason = sprintf($this->getLocale()->_('error of type %s with message \'%s\' exists'), self::getAsString($this->type), $this->message);
+ }
+
+ $this->fail($failReason);
+ }
+
+ return $this;
+ }
+
+ public function withType($type)
+ {
+ $this->type = $type;
+
+ return $this;
+ }
+
+ public function withAnyType()
+ {
+ $this->type = null;
+
+ return $this;
+ }
+
+ public function messageIsPattern()
+ {
+ return $this->messageIsPattern;
+ }
+
+ public function withMessage($message)
+ {
+ $this->message = $message;
+ $this->messageIsPattern = false;
+
+ return $this;
+ }
+
+ public function withPattern($pattern)
+ {
+ $this->message = $pattern;
+ $this->messageIsPattern = true;
+
+ return $this;
+ }
+
+ public function withAnyMessage()
+ {
+ $this->message = null;
+ $this->messageIsPattern = false;
+
+ return $this;
+ }
+
public static function getAsString($errorType)
{
switch ($errorType)
@@ -3,19 +3,32 @@
namespace mageekguy\atoum\asserters;
use
+ mageekguy\atoum,
mageekguy\atoum\asserters
;
class output extends asserters\string
{
- public function setWith($value, $label = null, $charlist = null, $checkType = true)
+ public function __construct(atoum\asserter\generator $generator)
+ {
+ parent::__construct($generator);
+
+ $this->setWith(null);
+ }
+
+ public function setWith($value = null, $label = null, $charlist = null, $checkType = true)
{
if ($value instanceof \closure)
{
ob_start();
$value();
$value = ob_get_clean();
}
+ else if ($value === null && ob_get_level() > 0)
+ {
+ $value = ob_get_clean();
+ ob_start();
+ }
return parent::setWith($value, $label, $charlist, $checkType);
}
View
@@ -0,0 +1,49 @@
+<?php
+
+namespace mageekguy\atoum;
+
+class includer
+{
+ protected $errorHandler = null;
+
+ public function __construct(\closure $errorHandler = null)
+ {
+ if ($errorHandler !== null)
+ {
+ $this->setErrorHandler($errorHandler);
+ }
+ }
+
+ public function getErrorHandler()
+ {
+ return $this->errorHandler;
+ }
+
+ public function setErrorHandler(\closure $errorHandler)
+ {
+ $this->errorHandler = $errorHandler;
+
+ return $this;
+ }
+
+ public function includeOnce($path)
+ {
+ $oldErrorHandler = null;
+
+ if ($this->errorHandler !== null)
+ {
+ $oldErrorHandler = set_error_handler($this->errorHandler);
+ }
+
+ include_once $path;
+
+ if ($oldErrorHandler !== null)
+ {
+ restore_error_handler();
+ }
+
+ return $this;
+ }
+}
+
+?>
@@ -21,10 +21,17 @@ public function __set($method, $value)
switch ($method)
{
case 'file_get_contents':
- $this->fopen = true;
- $this->fread[1] = $value;
- $this->fread[2] = '';
- $this->fclose = true;
+ if ($value === false)
+ {
+ $this->fopen = false;
+ }
+ else
+ {
+ $this->fopen = true;
+ $this->fread[1] = $value;
+ $this->fread[2] = '';
+ $this->fclose = true;
+ }
return $this;
case 'file_put_contents':
View
@@ -386,14 +386,14 @@ public function unsetCase()
return $this;
}
- public function errorExists($message = null, $type = null)
+ public function errorExists($message = null, $type = null, $messageIsPattern = false)
{
$messageIsNull = $message === null;
$typeIsNull = $type === null;
foreach ($this->errors as $key => $error)
{
- $messageMatch = $messageIsNull === true ? true : $error['message'] == $message;
+ $messageMatch = $messageIsNull === true ? true : ($messageIsPattern == false ? $message == $error['message'] : preg_match($message, $error['message']) == 1);
$typeMatch = $typeIsNull === true ? true : $error['type'] == $type;
if ($messageMatch === true && $typeMatch === true)
View
@@ -608,7 +608,7 @@ public function run(array $runTestMethods = array(), array $tags = array())
'$test->setLocale(new ' . get_class($this->locale) . '(' . $this->locale->get() . '));' .
'$test->setPhpPath(\'' . $this->getPhpPath() . '\');' .
($this->codeCoverageIsEnabled() === true ? '' : '$test->disableCodeCoverage();') .
- '$test->runTestMethod($method = \'%s\');' .
+ '$test->runTestMethod(\'%s\');' .
'echo serialize($test->getScore());' .
'?>'
;
@@ -802,6 +802,11 @@ public function getTestsSubNamespace()
die(__METHOD__ . ' is deprecated, please use ' . __CLASS__ . '::getTestNamespace() instead');
}
+ public function getOutput()
+ {
+ return ob_get_clean() ?: '';
+ }
+
public static function setNamespace($namespace)
{
self::$namespace = self::cleanNamespace($namespace);
View
@@ -12,7 +12,10 @@
if (defined(__NAMESPACE__ . '\autorun') === false)
{
define(__NAMESPACE__ . '\autorun', true);
+}
+if (autorun === true)
+{
scripts\runner::autorun(__FILE__);
}
@@ -257,6 +257,100 @@ public function testExists()
->integer($score->getPassNumber())->isEqualTo(1)
->array($score->getErrors())->isEmpty()
;
+
+ $score->addError(uniqid(), rand(1, PHP_INT_MAX), uniqid(), uniqid(), rand(1, PHP_INT_MAX), $message = uniqid() . 'FOO' . uniqid(), uniqid(), rand(1, PHP_INT_MAX));
+
+ $asserter
+ ->withPattern('/FOO/')
+ ->withType(null);
+ ;
+
+ $this->assert
+ ->object($asserter->exists())->isIdenticalTo($asserter)
+ ->integer($score->getFailNumber())->isEqualTo(1)
+ ->array($score->getFailAssertions())->isEqualTo(array(
+ array(
+ 'case' => null,
+ 'class' => __CLASS__,
+ 'method' => $test->getCurrentMethod(),
+ 'file' => __FILE__,
+ 'line' => $line,
+ 'asserter' => get_class($asserter) . '::exists()',
+ 'fail' => sprintf($test->getLocale()->_('error of type %s does not exist'), asserters\error::getAsString($type))
+ )
+ )
+ )
+ ->integer($score->getPassNumber())->isEqualTo(2)
+ ->array($score->getErrors())->isEmpty()
+ ;
+ }
+
+ public function testWithType()
+ {
+ $asserter = new asserters\error(new asserter\generator(new self()));
+
+ $this->assert
+ ->object($asserter->withType($type = rand(1, PHP_INT_MAX)))->isIdenticalTo($asserter)
+ ->integer($asserter->getType())->isEqualTo($type)
+ ;
+ }
+
+ public function testWithAnyType()
+ {
+ $asserter = new asserters\error(new asserter\generator(new self()));
+
+ $this->assert
+ ->if($asserter->withType(rand(1, PHP_INT_MAX)))
+ ->then
+ ->variable($asserter->getType())->isNotNull()
+ ->object($asserter->withAnyType())->isIdenticalTo($asserter)
+ ->variable($asserter->getType())->isNull()
+ ;
+ }
+
+ public function testWithMessage()
+ {
+ $asserter = new asserters\error(new asserter\generator(new self()));
+
+ $this->assert
+ ->object($asserter->withMessage($message = uniqid()))->isIdenticalTo($asserter)
+ ->string($asserter->getMessage())->isEqualTo($message)
+ ->boolean($asserter->messageIsPattern())->isFalse()
+ ;
+ }
+
+ public function testWithPattern()
+ {
+ $asserter = new asserters\error(new asserter\generator(new self()));
+
+ $this->assert
+ ->boolean($asserter->messageIsPattern())->isFalse()
+ ->object($asserter->withPattern($pattern = uniqid()))->isIdenticalTo($asserter)
+ ->string($asserter->getMessage())->isEqualTo($pattern)
+ ->boolean($asserter->messageIsPattern())->isTrue()
+ ;
+ }
+
+ public function testWithAnyMessage()
+ {
+ $asserter = new asserters\error(new asserter\generator(new self()));
+
+ $this->assert
+ ->if($asserter->withMessage(uniqid()))
+ ->then
+ ->variable($asserter->getMessage())->isNotNull()
+ ->boolean($asserter->messageIsPattern())->isFalse()
+ ->object($asserter->withAnyMessage())->isIdenticalTo($asserter)
+ ->variable($asserter->getMessage())->isNull()
+ ->boolean($asserter->messageIsPattern())->isFalse()
+ ->if($asserter->withPattern(uniqid()))
+ ->then
+ ->variable($asserter->getMessage())->isNotNull()
+ ->boolean($asserter->messageIsPattern())->isTrue()
+ ->object($asserter->withAnyMessage())->isIdenticalTo($asserter)
+ ->variable($asserter->getMessage())->isNull()
+ ->boolean($asserter->messageIsPattern())->isFalse()
+ ;
}
}
@@ -27,8 +27,8 @@ public function test__construct()
->object($asserter->getScore())->isIdenticalTo($this->getScore())
->object($asserter->getLocale())->isIdenticalTo($this->getLocale())
->object($asserter->getGenerator())->isIdenticalTo($generator)
- ->variable($asserter->getValue())->isNull()
- ->boolean($asserter->wasSet())->isFalse()
+ ->string($asserter->getValue())->isEmpty()
+ ->boolean($asserter->wasSet())->isTrue()
;
}
Oops, something went wrong.

0 comments on commit 9352e19

Please sign in to comment.