Skip to content

Commit

Permalink
Merge pull request #242 from elliotchance/1.7/216-allow-verify
Browse files Browse the repository at this point in the history
1.7/216 allow verify
  • Loading branch information
elliotchance committed Dec 17, 2014
2 parents e101ead + d60acbe commit 7fdf088
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/Concise/TestCase.php
Expand Up @@ -8,6 +8,7 @@
use Concise\Mock\MockManager;
use Concise\Validation\ArgumentChecker;
use Exception;
use PHPUnit_Framework_AssertionFailedError;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
use Concise\Mock\MockInterface;
Expand All @@ -33,6 +34,11 @@ class TestCase extends PHPUnit_Framework_TestCase
*/
protected $properties = array();

/**
* @var array
*/
protected $verifyFailures = array();

/**
* @param string|null $name
* @param array $data
Expand Down Expand Up @@ -161,6 +167,12 @@ public function assert()
public function tearDown()
{
$this->mockManager->validateMocks();
if ($this->verifyFailures) {
$count = count($this->verifyFailures);
$message = "$count verify failure" . ($count === 1 ? '' : 's') . ":";
$message .= "\n\n" . implode("\n\n", $this->verifyFailures);
throw new PHPUnit_Framework_AssertionFailedError($message);
}
parent::tearDown();
}

Expand Down Expand Up @@ -218,12 +230,10 @@ protected function loadKeywords()
define('on_error', 'on error');
}

/**
* This looks useless but we need to change the visibility of setUp() to public.
*/
public function setUp()
{
parent::setUp();
$this->verifyFailures = array();
}

/**
Expand Down Expand Up @@ -274,4 +284,16 @@ public function assertMock(MockInterface $mock)
$this->mockManager->validateMockByInstance($mock);
return true;
}

/**
* @return bool
*/
public function verify()
{
try {
call_user_func_array(array($this, 'assert'), func_get_args());
} catch (PHPUnit_Framework_AssertionFailedError $e) {
$this->verifyFailures[] = $e->getMessage();
}
}
}
50 changes: 50 additions & 0 deletions tests/Concise/VerifyFailuresTest.php
@@ -0,0 +1,50 @@
<?php

namespace Concise\Mock;

use Colors\Color;
use Concise\TestCase;

class VerifyFailuresTest extends TestCase
{
protected static $failures = array();

protected static $expectedFailures = array(
'testMultipleVerifyFailures' => "2 verify failures:\n\n10 equals 15\n\n15 equals 20",
'testSingleVerifyFailures' => "1 verify failure:\n\n10 equals 15",
);

/**
* @group #216
*/
public function testMultipleVerifyFailures()
{
$this->verify(10, equals, 15);
$this->verify(15, equals, 20);
}

/**
* @group #216
*/
public function testSingleVerifyFailures()
{
$this->verify(10, equals, 15);
}

protected function onNotSuccessfulTest(\Exception $e)
{
$c = new Color();
self::$failures[] = $this->getName();
$this->assert(self::$expectedFailures[$this->getName()], equals, $c($e->getMessage())->clean());
}

public static function tearDownAfterClass()
{
$a = array_keys(self::$expectedFailures);
$b = self::$failures;
$testCase = new TestCase();
$testCase->setUp();
$testCase->assert(array_diff($a, $b), equals, array_diff($b, $a));
$testCase->tearDown();
}
}

0 comments on commit 7fdf088

Please sign in to comment.