Skip to content

Commit

Permalink
Remove bug in error management. Test adapter and mock controller are …
Browse files Browse the repository at this point in the history
…now case insensitive.
  • Loading branch information
mageekguy committed Aug 12, 2011
1 parent 0b0f170 commit 52b632b
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 48 deletions.
6 changes: 3 additions & 3 deletions classes/mock/controller.php
Expand Up @@ -49,7 +49,7 @@ public function __unset($method)

parent::__unset($method);

$this->callables[$method] = null;
$this->callables[strtolower($method)] = null;

return $this;
}
Expand Down Expand Up @@ -133,7 +133,7 @@ public function control(mock\aggregator $mock)
}
);

array_walk($methods, function(& $value) { $value = $value->getName(); });
array_walk($methods, function(& $value) { $value = strtolower($value->getName()); });

foreach ($this->callables as $method => $closure)
{
Expand Down Expand Up @@ -199,7 +199,7 @@ protected function checkMethod($method)
{
if ($this->mockClass !== null && $this->disableMethodChecking === false)
{
if (array_key_exists($method, $this->callables) === false)
if (array_key_exists(strtolower($method), $this->callables) === false)
{
throw new exceptions\logic('Method \'' . $this->mockClass . '::' . $method . '()\' does not exist');
}
Expand Down
6 changes: 3 additions & 3 deletions classes/test.php
Expand Up @@ -538,18 +538,18 @@ public function run(array $runTestMethods = array(), atoum\runner $runner = null

if ($terminatedChild[3] !== '')
{
$this->score->addOutput($this->class, $testMethod, $terminatedChild[3]);
$score->addOutput($this->class, $testMethod, $terminatedChild[3]);
}

if ($terminatedChild[4] !== '')
{
if (preg_match_all('/([^:]+): (.+) in (.+) on line ([0-9]+)/', trim($terminatedChild[4]), $errors, PREG_SET_ORDER) === 0)
{
$this->score->addError($this->path, null, $this->class, $testMethod, 'UNKNOWN', $terminatedChild[4]);
$score->addError($this->path, null, $this->class, $testMethod, 'UNKNOWN', $terminatedChild[4]);
}
else foreach ($errors as $error)
{
$this->score->addError($this->path, null, $this->class, $testMethod, $error[1], $error[2], $error[3], $error[4]);
$score->addError($this->path, null, $this->class, $testMethod, $error[1], $error[2], $error[3], $error[4]);
}
}

Expand Down
47 changes: 34 additions & 13 deletions classes/test/adapter.php
Expand Up @@ -22,12 +22,16 @@ public function __construct()

public function __set($functionName, $mixed)
{
$this->{$functionName}->return = $mixed;
$this->__get($functionName)->return = $mixed;

return $this;
}

public function __get($functionName)
{
if (isset($this->{$functionName}) === false)
$functionName = strtolower($functionName);

if (isset($this->callables[$functionName]) === false)
{
$this->callables[$functionName] = new adapter\callable();
}
Expand All @@ -37,20 +41,27 @@ public function __get($functionName)

public function __isset($functionName)
{
return (isset($this->callables[$functionName]) === true);
return (isset($this->callables[strtolower($functionName)]) === true);
}

public function __unset($functionName)
{
if (isset($this->{$functionName}) === true)
{
$functionName = strtolower($functionName);

unset($this->callables[$functionName]);

if (isset($this->calls[$functionName]) === true)
foreach ($this->calls as $callName => $closure)
{
unset($this->calls[$functionName]);
if ($functionName == strtolower($callName))
{
unset($this->calls[$callName]);
}
}
}

return $this;
}

public function getCallables()
Expand All @@ -66,15 +77,25 @@ public function getCalls($functionName = null, array $arguments = null)
{
$calls = $this->calls;
}
else if (isset($this->calls[$functionName]) === true)
else
{
if ($arguments === null)
{
$calls = $this->calls[$functionName];
}
else
$functionName = strtolower($functionName);

foreach ($this->calls as $callName => $callArguments)
{
$calls = array_filter($this->calls[$functionName], function($callArguments) use ($arguments) { return $arguments == $callArguments; });
if ($functionName == strtolower($callName))
{
if ($arguments === null)
{
$calls = $callArguments;
}
else
{
$calls = array_filter($callArguments, function($callArguments) use ($arguments) { return $arguments == $callArguments; });
}

break;
}
}
}

Expand Down Expand Up @@ -129,7 +150,7 @@ public static function resetCallsForAllInstances()

protected static function isLanguageConstruct($functionName)
{
switch ($functionName)
switch (strtolower($functionName))
{
case 'array':
case 'echo':
Expand Down
58 changes: 55 additions & 3 deletions tests/units/classes/mock/controller.php
Expand Up @@ -34,18 +34,18 @@ public function test__set()
{
$mockController = new mock\controller();

$return = uniqid();

$mockController->{$method = uniqid()} = function() use ($return) { return $return; };
$mockController->{$method = uniqid()} = function() use (& $return) { return $return = uniqid(); };

$this->assert
->string($mockController->invoke($method))->isEqualTo($return)
->string($mockController->invoke(strtoupper($method)))->isEqualTo($return)
;

$mockController->{$method = uniqid()} = $return = uniqid();

$this->assert
->string($mockController->invoke($method))->isEqualTo($return)
->string($mockController->invoke(strtoupper($method)))->isEqualTo($return)
;
}

Expand All @@ -55,13 +55,16 @@ public function test__isset()

$this->assert
->boolean(isset($mockController->{uniqid()}))->isFalse()
->boolean(isset($mockController->{strtoupper(uniqid())}))->isFalse()
;

$mockController->{$method = uniqid()} = function() {};

$this->assert
->boolean(isset($mockController->{uniqid()}))->isFalse()
->boolean(isset($mockController->{strtoupper(uniqid())}))->isFalse()
->boolean(isset($mockController->{$method}))->isTrue()
->boolean(isset($mockController->{strtoupper($method)}))->isTrue()
;
}

Expand All @@ -78,15 +81,19 @@ public function test__get()
$this->assert
->object($mockController->{uniqid()})->isInstanceOf('mageekguy\atoum\test\adapter\callable')
->object($mockController->{$method}->getClosure())->isIdenticalTo($function)
->object($mockController->{strtoupper($method)}->getClosure())->isIdenticalTo($function)
;

$mockController->{$otherMethod = uniqid()} = $return = uniqid();

$this->assert
->object($mockController->{uniqid()})->isInstanceOf('mageekguy\atoum\test\adapter\callable')
->object($mockController->{$method}->getClosure())->isIdenticalTo($function)
->object($mockController->{strtoupper($method)}->getClosure())->isIdenticalTo($function)
->object($mockController->{$otherMethod}->getClosure())->isInstanceOf('closure')
->object($mockController->{strtoupper($otherMethod)}->getClosure())->isInstanceOf('closure')
->string($mockController->{$otherMethod}->invoke())->isEqualTo($return)
->string($mockController->{strtoupper($otherMethod)}->invoke())->isEqualTo($return)
;
}

Expand All @@ -98,8 +105,10 @@ public function test__unset()
->boolean(isset($mockController->{$method = uniqid()}))->isFalse()
->when(function() use ($mockController, $method) { $mockController->{$method} = uniqid(); })
->boolean(isset($mockController->{$method}))->isTrue()
->boolean(isset($mockController->{strtoupper($method)}))->isTrue()
->when(function() use ($mockController, $method) { unset($mockController->{$method}); })
->boolean(isset($mockController->{$method}))->isFalse()
->boolean(isset($mockController->{strtoupper($method)}))->isFalse()
;

$this->mockGenerator
Expand All @@ -115,8 +124,12 @@ public function test__unset()
->boolean(isset($mockController->getMethods))->isFalse()
->when(function() use ($mockController) { $mockController->getMethods = null; })
->boolean(isset($mockController->getMethods))->isTrue()
->boolean(isset($mockController->GetMethods))->isTrue()
->boolean(isset($mockController->GETMETHODS))->isTrue()
->when(function() use ($mockController) { unset($mockController->getMethods); })
->boolean(isset($mockController->getMethods))->isFalse()
->boolean(isset($mockController->GetMethods))->isFalse()
->boolean(isset($mockController->GETMETHODS))->isFalse()
;
}

Expand Down Expand Up @@ -306,6 +319,7 @@ public function testInvoke()
->string($mockController->invoke('test'))->isEqualTo($return)
->sizeOf($mockController->getCalls())->isEqualTo(1)
->array($mockController->getCalls('test'))->isEqualTo(array(1 => array()))
->array($mockController->getCalls('TEST'))->isEqualTo(array(1 => array()))
->string($mockController->invoke('test', array($argument1)))->isEqualTo($return)
->array($mockController->getCalls())->isEqualTo(array(
'test' => array(
Expand All @@ -319,6 +333,11 @@ public function testInvoke()
2 => array($argument1)
)
)
->array($mockController->getCalls('TEST'))->isEqualTo(array(
1 => array(),
2 => array($argument1)
)
)
->string($mockController->invoke('test', array($argument1, $argument2)))->isEqualTo($return)
->array($mockController->getCalls())->isEqualTo(array(
'test' => array(
Expand All @@ -334,6 +353,12 @@ public function testInvoke()
3 => array($argument1, $argument2)
)
)
->array($mockController->getCalls('TEST'))->isEqualTo(array(
1 => array(),
2 => array($argument1),
3 => array($argument1, $argument2)
)
)
->string($mockController->invoke('test', array($argument1, $argument2, $argument3)))->isEqualTo($return)
->array($mockController->getCalls())->isEqualTo(array(
'test' => array(
Expand All @@ -351,6 +376,13 @@ public function testInvoke()
4 => array($argument1, $argument2, $argument3)
)
)
->array($mockController->getCalls('TEST'))->isEqualTo(array(
1 => array(),
2 => array($argument1),
3 => array($argument1, $argument2),
4 => array($argument1, $argument2, $argument3)
)
)
->when(function() use ($mockController, $return) { $mockController->test2 = function() use ($return) { return $return; }; })
->string($mockController->invoke('test2', array($argument1)))->isEqualTo($return)
->array($mockController->getCalls())->isEqualTo(array(
Expand All @@ -369,6 +401,10 @@ public function testInvoke()
5 => array($argument1)
)
)
->array($mockController->getCalls('TEST2'))->isEqualTo(array(
5 => array($argument1)
)
)
->string($mockController->invoke('test2', array($argument1, $argument2)))->isEqualTo($return)
->array($mockController->getCalls())->isEqualTo(array(
'test' => array(
Expand All @@ -388,6 +424,11 @@ public function testInvoke()
6 => array($argument1, $argument2)
)
)
->array($mockController->getCalls('TEST2'))->isEqualTo(array(
5 => array($argument1),
6 => array($argument1, $argument2)
)
)
->string($mockController->invoke('test2', array($argument1, $argument2, $argument3)))->isEqualTo($return)
->array($mockController->getCalls())->isEqualTo(array(
'test' => array(
Expand All @@ -409,6 +450,12 @@ public function testInvoke()
7 => array($argument1, $argument2, $argument3)
)
)
->array($mockController->getCalls('TEST2'))->isEqualTo(array(
5 => array($argument1),
6 => array($argument1, $argument2),
7 => array($argument1, $argument2, $argument3)
)
)
;
}

Expand Down Expand Up @@ -476,12 +523,17 @@ public function testGetCalls()
->when(function() use ($mockController, $method, & $arg) { $mockController->invoke($method, array($arg = uniqid())); })
->array($mockController->getCalls())->isEqualTo(array($method => array(1 => array($arg))))
->array($mockController->getCalls($method))->isEqualTo(array(1 => array($arg)))
->array($mockController->getCalls(strtoupper($method)))->isEqualTo(array(1 => array($arg)))
->array($mockController->getCalls($method, array($arg)))->isEqualTo(array(1 => array($arg)))
->array($mockController->getCalls(strtoupper($method), array($arg)))->isEqualTo(array(1 => array($arg)))
->when(function() use ($mockController, $method, & $otherArg) { $mockController->invoke($method, array($otherArg = uniqid())); })
->array($mockController->getCalls())->isEqualTo(array($method => array(1 => array($arg), 2 => array($otherArg))))
->array($mockController->getCalls($method))->isEqualTo(array(1 => array($arg), 2 => array($otherArg)))
->array($mockController->getCalls(strtoupper($method)))->isEqualTo(array(1 => array($arg), 2 => array($otherArg)))
->array($mockController->getCalls($method, array($arg)))->isEqualTo(array(1 => array($arg)))
->array($mockController->getCalls(strtoupper($method), array($arg)))->isEqualTo(array(1 => array($arg)))
->array($mockController->getCalls($method, array($otherArg)))->isEqualTo(array(2 => array($otherArg)))
->array($mockController->getCalls(strtoupper($method), array($otherArg)))->isEqualTo(array(2 => array($otherArg)))
;
}

Expand Down

0 comments on commit 52b632b

Please sign in to comment.