Skip to content

Commit

Permalink
Added tests for ExceptionComparator.
Browse files Browse the repository at this point in the history
  • Loading branch information
whatthejeff committed May 1, 2014
1 parent fd9e42c commit f81cc1a
Showing 1 changed file with 174 additions and 0 deletions.
174 changes: 174 additions & 0 deletions tests/ExceptionComparatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php
/**
* Comparator
*
* Copyright (c) 2001-2014, Sebastian Bergmann <[email protected]>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Sebastian Bergmann nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Comparator
* @author Jeff Welch <[email protected]>
* @copyright 2001-2014 Sebastian Bergmann <[email protected]>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @link http://www.github.com/sebastianbergmann/comparator
*/

namespace SebastianBergmann\Comparator;

use \Exception;
use \RuntimeException;

/**
* @coversDefaultClass SebastianBergmann\Comparator\ExceptionComparator
*
* @package Comparator
* @author Jeff Welch <[email protected]>
* @copyright 2001-2014 Sebastian Bergmann <[email protected]>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @link http://www.github.com/sebastianbergmann/comparator
*/
class ExceptionComparatorTest extends \PHPUnit_Framework_TestCase
{
private $comparator;

protected function setUp()
{
$this->comparator = new ExceptionComparator;
$this->comparator->setFactory(new Factory);
}

public function acceptsSucceedsProvider()
{
return array(
array(new Exception, new Exception),
array(new RuntimeException, new RuntimeException),
array(new Exception, new RuntimeException)
);
}

public function acceptsFailsProvider()
{
return array(
array(new Exception, null),
array(null, new Exception),
array(null, null)
);
}

public function assertEqualsSucceedsProvider()
{
$exception1 = new Exception;
$exception2 = new Exception;

$exception3 = new RunTimeException('Error', 100);
$exception4 = new RunTimeException('Error', 100);

return array(
array($exception1, $exception1),
array($exception1, $exception2),
array($exception3, $exception3),
array($exception3, $exception4)
);
}

public function assertEqualsFailsProvider()
{
$typeMessage = 'not instance of expected class';
$equalMessage = 'Failed asserting that two objects are equal.';

$exception1 = new Exception('Error', 100);
$exception2 = new Exception('Error', 101);
$exception3 = new Exception('Errors', 101);

$exception4 = new RunTimeException('Error', 100);
$exception5 = new RunTimeException('Error', 101);

return array(
array($exception1, $exception2, $equalMessage),
array($exception1, $exception3, $equalMessage),
array($exception1, $exception4, $typeMessage),
array($exception2, $exception3, $equalMessage),
array($exception4, $exception5, $equalMessage)
);
}

/**
* @covers ::accepts
* @dataProvider acceptsSucceedsProvider
*/
public function testAcceptsSucceeds($expected, $actual)
{
$this->assertTrue(
$this->comparator->accepts($expected, $actual)
);
}

/**
* @covers ::accepts
* @dataProvider acceptsFailsProvider
*/
public function testAcceptsFails($expected, $actual)
{
$this->assertFalse(
$this->comparator->accepts($expected, $actual)
);
}

/**
* @covers ::assertEquals
* @dataProvider assertEqualsSucceedsProvider
*/
public function testAssertEqualsSucceeds($expected, $actual)
{
$exception = null;

try {
$this->comparator->assertEquals($expected, $actual);
}

catch (ComparisonFailure $exception) {
}

$this->assertNull($exception, 'Unexpected ComparisonFailure');
}

/**
* @covers ::assertEquals
* @dataProvider assertEqualsFailsProvider
*/
public function testAssertEqualsFails($expected, $actual, $message)
{
$this->setExpectedException(
'SebastianBergmann\\Comparator\\ComparisonFailure', $message
);
$this->comparator->assertEquals($expected, $actual);
}
}

0 comments on commit f81cc1a

Please sign in to comment.