<?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.
*/
class Framework_AssertTest extends PHPUnit_Framework_TestCase
{
/**
* @var string
*/
private $filesDirectory;
protected function setUp()
{
$this->filesDirectory = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR;
}
public function testFail()
{
try {
$this->fail();
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
throw new PHPUnit_Framework_AssertionFailedError('Fail did not throw fail exception');
}
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();
}
public function testAssertArrayContainsObject()
{
$a = new stdClass;
$b = new stdClass;
$this->assertContains($a, [$a]);
try {
$this->assertContains($a, [$b]);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
public function testAssertArrayContainsString()
{
$this->assertContains('foo', ['foo']);
try {
$this->assertContains('foo', ['bar']);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
public function testAssertArrayContainsNonObject()
{
$this->assertContains('foo', [true]);
try {
$this->assertContains('foo', [true], '', false, true, true);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
public function testAssertContainsOnlyInstancesOf()
{
$test = [
new Book(),
new Book
];
$this->assertContainsOnlyInstancesOf('Book', $test);
$this->assertContainsOnlyInstancesOf('stdClass', [new stdClass()]);
$test2 = [
new Author('Test')
];
try {
$this->assertContainsOnlyInstancesOf('Book', $test2);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
/**
* @expectedException PHPUnit_Framework_Exception
*/
public function testAssertArrayHasKeyThrowsExceptionForInvalidFirstArgument()
{
$this->assertArrayHasKey(null, []);
}
/**
* @expectedException PHPUnit_Framework_Exception
*/
public function testAssertArrayHasKeyThrowsExceptionForInvalidSecondArgument()
{
$this->assertArrayHasKey(0, null);
}
public function testAssertArrayHasIntegerKey()
{
$this->assertArrayHasKey(0, ['foo']);
try {
$this->assertArrayHasKey(1, ['foo']);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
public function testAssertArraySubset()
{
$array = [
'a' => 'item a',
'b' => 'item b',
'c' => ['a2' => 'item a2', 'b2' => 'item b2'],
'd' => ['a2' => ['a3' => 'item a3', 'b3' => 'item b3']]
];
$this->assertArraySubset(['a' => 'item a', 'c' => ['a2' => 'item a2']], $array);
$this->assertArraySubset(['a' => 'item a', 'd' => ['a2' => ['b3' => 'item b3']]], $array);
$arrayAccessData = new ArrayObject($array);
$this->assertArraySubset(['a' => 'item a', 'c' => ['a2' => 'item a2']], $arrayAccessData);
$this->assertArraySubset(['a' => 'item a', 'd' => ['a2' => ['b3' => 'item b3']]], $arrayAccessData);
try {
$this->assertArraySubset(['a' => 'bad value'], $array);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
}
try {
$this->assertArraySubset(['d' => ['a2' => ['bad index' => 'item b3']]], $array);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
public function testAssertArraySubsetWithDeepNestedArrays()
{
$array = [
'path' => [
'to' => [
'the' => [
'cake' => 'is a lie'
]
]
]
];
$this->assertArraySubset(['path' => []], $array);
$this->assertArraySubset(['path' => ['to' => []]], $array);
$this->assertArraySubset(['path' => ['to' => ['the' => []]]], $array);
$this->assertArraySubset(['path' => ['to' => ['the' => ['cake' => 'is a lie']]]], $array);
try {
$this->assertArraySubset(['path' => ['to' => ['the' => ['cake' => 'is not a lie']]]], $array);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
public function testAssertArraySubsetWithNoStrictCheckAndObjects()
{
$obj = new \stdClass;
$reference = &$obj;
$array = ['a' => $obj];
$this->assertArraySubset(['a' => $reference], $array);
$this->assertArraySubset(['a' => new \stdClass], $array);
}
public function testAssertArraySubsetWithStrictCheckAndObjects()
{
$obj = new \stdClass;
$reference = &$obj;
$array = ['a' => $obj];
$this->assertArraySubset(['a' => $reference], $array, true);
try {
$this->assertArraySubset(['a' => new \stdClass], $array, true);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail('Strict recursive array check fail.');
}
/**
* @expectedException PHPUnit_Framework_Exception
* @expectedExceptionMessage array or ArrayAccess
* @dataProvider assertArraySubsetInvalidArgumentProvider
*/
public function testAssertArraySubsetRaisesExceptionForInvalidArguments($partial, $subject)
{
$this->assertArraySubset($partial, $subject);
}
/**
* @return array
*/
public function assertArraySubsetInvalidArgumentProvider()
{
return [
[false, []],
[[], false],
];
}
/**
* @expectedException PHPUnit_Framework_Exception
*/
public function testAssertArrayNotHasKeyThrowsExceptionForInvalidFirstArgument()
{
$this->assertArrayNotHasKey(null, []);
}
/**
* @expectedException PHPUnit_Framework_Exception
*/
public function testAssertArrayNotHasKeyThrowsExceptionForInvalidSecondArgument()
{
$this->assertArrayNotHasKey(0, null);
}
public function testAssertArrayNotHasIntegerKey()
{
$this->assertArrayNotHasKey(1, ['foo']);
try {
$this->assertArrayNotHasKey(0, ['foo']);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
public function testAssertArrayHasStringKey()
{
$this->assertArrayHasKey('foo', ['foo' => 'bar']);
try {
$this->assertArrayHasKey('bar', ['foo' => 'bar']);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}
$this->fail();
}
public function testAssertArrayNotHasStringKey()
{
$this->assertArrayNotHasKey('bar', ['foo' => 'bar']);
try {
$this->assertArrayNotHasKey('foo', ['foo' => 'bar']);
} catch (PHPUnit_Framework_AssertionFailedError $e) {
return;
}