<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @since Class available since Release 2.0.0
*/
class Framework_AssertTest extends PHPUnit_Framework_TestCase
{
/**
* @var string
*/
private $filesDirectory;
protected function setUp()
{
$this->filesDirectory = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR;
}
/**
* @covers PHPUnit_Framework_Assert::fail
*/
public function testFail()
{
try {
$this->fail();
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
throw new PHPUnit_Framework_AssertionFailedError('Fail did not throw fail exception');
}
/**
* @covers PHPUnit_Framework_Assert::assertContains
*/
public function testAssertSplObjectStorageContainsObject()
{
$a = new stdClass;
$b = new stdClass;
$c = new SplObjectStorage;
$c->attach($a);
$this->assertContains($a, $c);
try {
$this->assertContains($b, $c);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
/**
* @covers PHPUnit_Framework_Assert::assertContains
*/
public function testAssertArrayContainsObject()
{
$a = new stdClass;
$b = new stdClass;
$this->assertContains($a, array($a));
try {
$this->assertContains($a, array($b));
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
/**
* @covers PHPUnit_Framework_Assert::assertContains
*/
public function testAssertArrayContainsString()
{
$this->assertContains('foo', array('foo'));
try {
$this->assertContains('foo', array('bar'));
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
/**
* @covers PHPUnit_Framework_Assert::assertContains
*/
public function testAssertArrayContainsNonObject()
{
$this->assertContains('foo', array(true));
try {
$this->assertContains('foo', array(true), '', false, true, true);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
/**
* @covers PHPUnit_Framework_Assert::assertContainsOnlyInstancesOf
*/
public function testAssertContainsOnlyInstancesOf()
{
$test = array(
new Book(),
new Book
);
$this->assertContainsOnlyInstancesOf('Book', $test);
$this->assertContainsOnlyInstancesOf('stdClass', array(new stdClass()));
$test2 = array(
new Author('Test')
);
try {
$this->assertContainsOnlyInstancesOf('Book', $test2);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
/**
* @covers PHPUnit_Framework_Assert::assertArrayHasKey
* @expectedException PHPUnit_Framework_Exception
*/
public function testAssertArrayHasKeyThrowsExceptionForInvalidFirstArgument()
{
$this->assertArrayHasKey(null, array());
}
/**
* @covers PHPUnit_Framework_Assert::assertArrayHasKey
* @expectedException PHPUnit_Framework_Exception
*/
public function testAssertArrayHasKeyThrowsExceptionForInvalidSecondArgument()
{
$this->assertArrayHasKey(0, null);
}
/**
* @covers PHPUnit_Framework_Assert::assertArrayHasKey
*/
public function testAssertArrayHasIntegerKey()
{
$this->assertArrayHasKey(0, array('foo'));
try {
$this->assertArrayHasKey(1, array('foo'));
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
/**
* @covers PHPUnit_Framework_Assert::assertArraySubset
* @covers PHPUnit_Framework_Constraint_ArraySubset
*/
public function testAssertArraySubset()
{
$array = array(
'a' => 'item a',
'b' => 'item b',
'c' => array('a2' => 'item a2', 'b2' => 'item b2'),
'd' => array('a2' => array('a3' => 'item a3', 'b3' => 'item b3'))
);
$this->assertArraySubset(array('a' => 'item a', 'c' => array('a2' => 'item a2')), $array);
$this->assertArraySubset(array('a' => 'item a', 'd' => array('a2' => array('b3' => 'item b3'))), $array);
$arrayAccessData = new ArrayObject($array);
$this->assertArraySubset(array('a' => 'item a', 'c' => array('a2' => 'item a2')), $arrayAccessData);
$this->assertArraySubset(array('a' => 'item a', 'd' => array('a2' => array('b3' => 'item b3'))), $arrayAccessData);
try {
$this->assertArraySubset(array('a' => 'bad value'), $array);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
}
try {
$this->assertArraySubset(array('d' => array('a2' => array('bad index' => 'item b3'))), $array);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
/**
* @covers PHPUnit_Framework_Assert::assertArraySubset
* @covers PHPUnit_Framework_Constraint_ArraySubset
*/
public function testAssertArraySubsetWithDeepNestedArrays()
{
$array = array(
'path' => array(
'to' => array(
'the' => array(
'cake' => 'is a lie'
)
)
)
);
$this->assertArraySubset(array('path' => array()), $array);
$this->assertArraySubset(array('path' => array('to' => array())), $array);
$this->assertArraySubset(array('path' => array('to' => array('the' => array()))), $array);
$this->assertArraySubset(array('path' => array('to' => array('the' => array('cake' => 'is a lie')))), $array);
try {
$this->assertArraySubset(array('path' => array('to' => array('the' => array('cake' => 'is not a lie')))), $array);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
/**
* @covers PHPUnit_Framework_Assert::assertArraySubset
* @covers PHPUnit_Framework_Constraint_ArraySubset
*/
public function testAssertArraySubsetWithNoStrictCheckAndObjects()
{
$obj = new \stdClass;
$reference = &$obj;
$array = array('a' => $obj);
$this->assertArraySubset(array('a' => $reference), $array);
$this->assertArraySubset(array('a' => new \stdClass), $array);
}
/**
* @covers PHPUnit_Framework_Assert::assertArraySubset
* @covers PHPUnit_Framework_Constraint_ArraySubset
*/
public function testAssertArraySubsetWithStrictCheckAndObjects()
{
$obj = new \stdClass;
$reference = &$obj;
$array = array('a' => $obj);
$this->assertArraySubset(array('a' => $reference), $array, true);
try {
$this->assertArraySubset(array('a' => new \stdClass), $array, true);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail('Strict recursive array check fail.');
}
/**
* @covers PHPUnit_Framework_Assert::assertArraySubset
* @covers PHPUnit_Framework_Constraint_ArraySubset
* @expectedException PHPUnit_Framework_Exception
* @expectedExceptionMessage array or ArrayAccess
* @dataProvider assertArraySubsetInvalidArgumentProvider
*/
public function testAssertArraySubsetRaisesExceptionForIn