Skip to content

Commit

Permalink
added withErrorReporting helper
Browse files Browse the repository at this point in the history
  • Loading branch information
saeideng committed Oct 9, 2017
1 parent 8805d9c commit 12f84af
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/TestSuite/TestCase.php
Expand Up @@ -87,6 +87,24 @@ public function skipIf($shouldSkip, $message = '')
return $shouldSkip;
}

/**
* Helper method for tests that needs to use error_reporting()
*
* @param int $errorLevel value of error_reporting() that needs to use
* @param callable $callable callable function that will receive asserts
* @return void
*/
public function withErrorReporting($errorLevel, $callable)
{
try {
$default = error_reporting();
error_reporting($errorLevel);
$callable();
} finally {
error_reporting($default);
}
}

/**
* Helper method for check deprecation methods
*
Expand Down
32 changes: 32 additions & 0 deletions tests/TestCase/TestSuite/TestCaseTest.php
Expand Up @@ -175,6 +175,38 @@ public function testSkipIf()
$this->assertEquals(0, $result->skippedCount());
}

/**
* test withErrorReporting
*
* @return void
*/
public function testWithErrorReporting()
{
$errorLevel = error_reporting();
$this->withErrorReporting(E_USER_WARNING, function () {
$this->assertSame(E_USER_WARNING, error_reporting());
});
$this->assertSame($errorLevel, error_reporting());
}

/**
* test withErrorReporting with exceptions
*
* @expectedException \PHPUnit\Framework\AssertionFailedError
* @return void
*/
public function testWithErrorReportingWithException()
{
$errorLevel = error_reporting();
try {
$this->withErrorReporting(E_USER_WARNING, function () {
$this->assertSame(1, 2);
});
} finally {
$this->assertSame($errorLevel, error_reporting());
}
}

/**
* testDeprecated
*
Expand Down

0 comments on commit 12f84af

Please sign in to comment.