Skip to content

Commit

Permalink
Fix tests in OoParamsTest and implement tests for array parameters de…
Browse files Browse the repository at this point in the history
…claration
  • Loading branch information
ovr committed Jul 23, 2014
1 parent 898b469 commit aad6e0c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
92 changes: 84 additions & 8 deletions unit-tests/Extension/MCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,49 @@

namespace Extension;

use Test\Mcall;

class MCallTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \ReflectionClass
*/
private $reflection;

public function assertNumberOfParameters($number)
{
$this->assertEquals($number, $this->getReflection()->getMethod($this->getName())->getNumberOfParameters());
}

public function assertNumberOfRequiredParameters($number)
{
$this->assertEquals($number, $this->getReflection()->getMethod($this->getName())->getNumberOfRequiredParameters());
}

private function getReflection()
{
if (is_null($this->reflection)) {
return $this->reflection = new \ReflectionClass('\Test\Mcall');
}

return $this->reflection;
}

/**
* @return \ReflectionParameter
*/
protected function getMethodFirstParameter()
{
$backtrace = debug_backtrace();
$methodInfo = $this->reflection->getMethod($this->getName());
$parameters = $methodInfo->getParameters();

return $parameters[0];
}

public function testCall()
{
$t = new \Test\Mcall();
$t = new Mcall();

$this->assertTrue($t->testCall1() === "hello public");
$this->assertTrue($t->testCall2() === "hello protected");
Expand All @@ -48,30 +86,30 @@ public function testCall()

public function testOptionalParameterString()
{
$t = new \Test\Mcall();
$t = new Mcall();
$this->assertTrue($t->optionalParameterString("test") == 'test');
$this->assertTrue($t->optionalParameterString() == "test string");
$this->assertTrue($t->optionalParameterStringNull() == "");
}

public function testOptionalParameterInt()
{
$t = new \Test\Mcall();
$t = new Mcall();
$this->assertTrue($t->optionalParameterInt(1) == 1);
$this->assertTrue($t->optionalParameterInt() == 2);
}

public function testOptionalParameterVar()
{
$t = new \Test\Mcall();
$t = new Mcall();
$this->assertTrue($t->optionalParameterVar(1) === 1);
$this->assertTrue($t->optionalParameterVar("testtesttesttest") === "testtesttesttest");
$this->assertTrue($t->optionalParameterVar() === null);
}

public function testOptionalParameterBoolean()
{
$t = new \Test\Mcall();
$t = new Mcall();
$this->assertTrue($t->optionalParameterBoolFalse() === false);
$this->assertTrue($t->optionalParameterBoolTrue() === true);

Expand All @@ -84,15 +122,53 @@ public function testOptionalParameterBoolean()

public function testOptionalParameterBooleanException()
{
$t = new \Test\Mcall();
$t = new Mcall();
$this->setExpectedException('\InvalidArgumentException');
$t->optionalParameterBoolean('test');
}

public function testArrayParamWithDefaultEmptyArray()
/**
* @test
*/
public function arrayParamWithDefaultEmptyArray()
{
$t = new \Test\Mcall();
$t = new Mcall();

$this->assertNumberOfParameters(1);
$this->assertNumberOfRequiredParameters(0);

$this->assertTrue($this->getMethodFirstParameter()->isArray());
$this->assertTrue($t->arrayParamWithDefaultEmptyArray() === array());
$this->assertTrue($t->arrayParamWithDefaultEmptyArray(array(1)) === array(1));
}

/**
* @test
*/
public function arrayParamWithDefaultNullValue()
{
$t = new Mcall();

$this->assertNumberOfParameters(1);
$this->assertNumberOfRequiredParameters(0);

$this->assertTrue($this->getMethodFirstParameter()->isArray());
$this->assertTrue($t->arrayParamWithDefaultNullValue() === null);
$this->assertTrue($t->arrayParamWithDefaultNullValue(array(1)) === array(1));
}

/**
* @test
*/
public function arrayParam()
{
$t = new Mcall();

$this->assertNumberOfParameters(1);
$this->assertNumberOfRequiredParameters(1);

$this->assertTrue($this->getMethodFirstParameter()->isArray());
$this->assertTrue($t->arrayParam(array()) === array());
$this->assertTrue($t->arrayParam(array(1, 2, 3)) === array(1, 2, 3));
}
}
2 changes: 1 addition & 1 deletion unit-tests/Extension/Oo/OoParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testOoParams()
$this->assertTrue($t->setEnabled("0") === false);

$this->assertTrue($t->setList(array(1, 2, 3, 4, 5)) === array(1, 2, 3, 4, 5));
$this->assertTrue($t->setList(null) == array());
$this->assertTrue($t->setList(array()) == array());
}

public function testSetStrictAgeException1()
Expand Down

0 comments on commit aad6e0c

Please sign in to comment.