Created
May 29, 2012 17:24
-
-
Save olivergondza/2829626 to your computer and use it in GitHub Desktop.
Testcase for undocumented PHP feature allowing to use default argument values in the middle of the argument list
This file contains 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 | |
/** | |
* Declarations of necessary classes and functions | |
*/ | |
class ClassA {} | |
class ClassB {} | |
class ClassC {} | |
function argsFirst ( | |
ClassA $arg1 = null, | |
ClassB $arg2, | |
ClassC $arg3 | |
) { | |
assert ( $arg1 instanceof ClassA || $arg1 === null ); | |
assert ( $arg2 instanceof ClassB ); | |
assert ( $arg3 instanceof ClassC ); | |
} | |
function argsMiddle ( | |
ClassA $arg1, | |
ClassB $arg2 = null, | |
ClassC $arg3 | |
) { | |
assert ( $arg1 instanceof ClassA ); | |
assert ( $arg2 instanceof ClassB || $arg2 === null ); | |
assert ( $arg3 instanceof ClassC ); | |
} | |
function argsLast ( | |
ClassA $arg1, | |
ClassB $arg2, | |
ClassC $arg3 = null | |
) { | |
assert ( $arg1 instanceof ClassA ); | |
assert ( $arg2 instanceof ClassB ); | |
assert ( $arg3 instanceof ClassC || $arg3 === null ); | |
} | |
function argsFirstAndLast ( | |
ClassA $arg1 = null, | |
ClassB $arg2, | |
ClassC $arg3 = null | |
) { | |
assert ( $arg1 instanceof ClassA || $arg1 === null ); | |
assert ( $arg2 instanceof ClassB ); | |
assert ( $arg3 instanceof ClassC || $arg3 === null ); | |
} | |
function argsAllButLast ( | |
ClassA $arg1 = null, | |
ClassB $arg2 = null, | |
ClassC $arg3 | |
) { | |
assert ( $arg1 instanceof ClassA || $arg1 === null ); | |
assert ( $arg2 instanceof ClassB || $arg2 === null ); | |
assert ( $arg3 instanceof ClassC ); | |
} | |
function argsAll ( | |
ClassA $arg1 = null, | |
ClassB $arg2 = null, | |
ClassC $arg3 = null | |
) { | |
assert ( $arg1 instanceof ClassA || $arg1 === null ); | |
assert ( $arg2 instanceof ClassB || $arg2 === null ); | |
assert ( $arg3 instanceof ClassC || $arg3 === null ); | |
} |
This file contains 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 | |
/** | |
* Supported PHP versions: | |
* 5.3.0 | |
* 5.3.1 | |
* 5.3.2 | |
* 5.3.3 | |
* 5.3.4 | |
* 5.3.5 | |
* 5.3.7 | |
* 5.3.8 | |
* 5.3.9 | |
* 5.3.10 | |
* 5.3.11 | |
* 5.3.12 | |
* 5.4.0 | |
* 5.4.1 | |
* 5.4.2 | |
* 5.4.3 | |
*/ | |
require_once dirname ( __FILE__ ) . '/DefaultArgs.php'; | |
/** | |
* | |
*/ | |
class DefaultArgsTest extends PHPUnit_Framework_TestCase { | |
private $_valA; | |
private $_valB; | |
private $_valC; | |
public function setUp () { | |
$this->_valA = new ClassA; | |
$this->_valB = new ClassB; | |
$this->_valC = new ClassC; | |
} | |
public function testCorrectCallsFirst () { | |
argsFirst ( $this->_valA, $this->_valB, $this->_valC ); | |
argsFirst ( null, $this->_valB, $this->_valC ); | |
} | |
public function testCorrectCallsMiddle () { | |
argsMiddle ( $this->_valA, $this->_valB, $this->_valC ); | |
argsMiddle ( $this->_valA, null, $this->_valC ); | |
} | |
public function testCorrectCallsLast () { | |
argsLast ( $this->_valA, $this->_valB, $this->_valC ); | |
argsLast ( $this->_valA, $this->_valB, null ); | |
argsLast ( $this->_valA, $this->_valB ); | |
} | |
public function testCorrectCallsFirstAndLast () { | |
argsFirstAndLast ( $this->_valA, $this->_valB, $this->_valC ); | |
argsFirstAndLast ( $this->_valA, $this->_valB, null ); | |
argsFirstAndLast ( $this->_valA, $this->_valB ); | |
argsFirstAndLast ( null, $this->_valB, null ); | |
argsFirstAndLast ( null, $this->_valB ); | |
} | |
public function testCorrectCallsAllButLast () { | |
argsAllButLast ( $this->_valA, $this->_valB, $this->_valC ); | |
argsAllButLast ( null, $this->_valB, $this->_valC ); | |
argsAllButLast ( $this->_valA, null, $this->_valC ); | |
argsAllButLast ( null, null, $this->_valC ); | |
} | |
public function testCorrectCallsAll () { | |
argsAll ( $this->_valA, $this->_valB, $this->_valC ); | |
argsAll ( null, $this->_valB, $this->_valC ); | |
argsAll ( $this->_valA, null, $this->_valC ); | |
argsAll ( $this->_valA, $this->_valB, null ); | |
argsAll ( $this->_valA, $this->_valB ); | |
argsAll ( null, null, $this->_valC ); | |
argsAll ( null, $this->_valB, null ); | |
argsAll ( null, $this->_valB ); | |
argsAll ( $this->_valA, null, null ); | |
argsAll ( $this->_valA, null ); | |
argsAll ( $this->_valA ); | |
argsAll ( null, null, null ); | |
argsAll ( null, null ); | |
argsAll ( null ); | |
argsAll (); | |
} | |
public function provideAllCalls () { | |
return Array ( | |
Array ( "argsFirst" ), | |
Array ( "argsMiddle" ), | |
Array ( "argsLast" ), | |
Array ( "argsFirstAndLast" ), | |
Array ( "argsAllButLast" ), | |
Array ( "argsAll" ), | |
); | |
} | |
/** | |
* @dataProvider provideAllCalls | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassA, integer given | |
*/ | |
public function testBadTypeFirst ( $call ) { | |
$call ( 42, $this->_valB, $this->_valC ); | |
} | |
/** | |
* @dataProvider provideAllCalls | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassA, instance of ClassB given | |
*/ | |
public function testBadClassFirst ( $call ) { | |
$call ( new ClassB, $this->_valB, $this->_valC ); | |
} | |
/** | |
* @dataProvider provideAllCalls | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassB, integer given | |
*/ | |
public function testBadTypeMiddle ( $call ) { | |
$call ( $this->_valA, 42, $this->_valC ); | |
} | |
/** | |
* @dataProvider provideAllCalls | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassB, instance of ClassC given | |
*/ | |
public function testBadClassMiddle ( $call ) { | |
$call ( $this->_valA, new ClassC, $this->_valC ); | |
} | |
/** | |
* @dataProvider provideAllCalls | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassC, integer given | |
*/ | |
public function testBadTypeLast ( $call ) { | |
$call ( $this->_valA, $this->_valB, 42 ); | |
} | |
/** | |
* @dataProvider provideAllCalls | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassC, instance of ClassA given | |
*/ | |
public function testBadClassLast ( $call ) { | |
$call ( $this->_valA, $this->_valB, new ClassA ); | |
} | |
public function provideCallsThatRequireClassA () { | |
return Array ( | |
Array ( "argsMiddle" ), | |
Array ( "argsLast" ), | |
); | |
} | |
/** | |
* @dataProvider provideCallsThatRequireClassA | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassA, null given | |
*/ | |
public function testNullFirstType ( $call ) { | |
$call ( null, $this->_valB, $this->_valC ); | |
} | |
/** | |
* @dataProvider provideCallsThatRequireClassA | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassA, none given | |
*/ | |
public function testNoneFirstType ( $call ) { | |
$call (); | |
} | |
public function provideCallsThatRequireClassB () { | |
return Array ( | |
Array ( "argsFirst" ), | |
Array ( "argsLast" ), | |
Array ( "argsFirstAndLast" ), | |
); | |
} | |
/** | |
* @dataProvider provideCallsThatRequireClassB | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassB, null given | |
*/ | |
public function testNullSecondType ( $call ) { | |
$call ( $this->_valA, null, $this->_valC ); | |
} | |
/** | |
* @dataProvider provideCallsThatRequireClassB | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassB, none given | |
*/ | |
public function testNoneSecondType ( $call ) { | |
$call ( $this->_valA ); | |
} | |
public function provideCallsThatRequireClassC () { | |
return Array ( | |
Array ( "argsFirst" ), | |
Array ( "argsMiddle" ), | |
Array ( "argsAllButLast" ), | |
); | |
} | |
/** | |
* @dataProvider provideCallsThatRequireClassC | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassC, null given | |
*/ | |
public function testNullLastType ( $call ) { | |
$call ( $this->_valA, $this->_valB, null ); | |
} | |
/** | |
* @dataProvider provideCallsThatRequireClassC | |
* | |
* @expectedException PHPUnit_Framework_Error | |
* @expectedExceptionMessage must be an instance of ClassC, none given | |
*/ | |
public function testNoneLastType ( $call ) { | |
$call ( $this->_valA, $this->_valB ); | |
} | |
} |
This file contains 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 | |
/** | |
* Simple test script to execute without PHPUnit | |
*/ | |
require_once dirname ( __FILE__ ) . '/DefaultArgs.php'; | |
$valA = new ClassA; | |
$valB = new ClassB; | |
$valC = new ClassC; | |
argsFirst ( $valA, $valB, $valC ); | |
argsMiddle ( $valA, $valB, $valC ); | |
argsLast ( $valA, $valB, $valC ); | |
argsFirstAndLast ( $valA, $valB, $valC ); | |
argsAllButLast ( $valA, $valB, $valC ); | |
argsAll ( $valA, $valB, $valC ); | |
argsFirst ( null, $valB, $valC ); | |
argsMiddle ( $valA, null, $valC ); | |
argsLast ( $valA, $valB, null ); | |
argsFirstAndLast ( null, $valB, null ); | |
argsAllButLast ( null, null, $valC ); | |
argsAll ( null, null, null ); | |
argsLast ( $valA, $valB ); | |
argsFirstAndLast ( null, $valB ); | |
argsAll ( null, null ); | |
argsAll ( null ); | |
argsAll (); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment