Skip to content

Commit

Permalink
Enhanced tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fbett committed Aug 19, 2022
1 parent 5101436 commit 4409ff9
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/LE_ACME2Tests/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use PHPUnit\Framework\TestCase;
use LE_ACME2;

abstract class AbstractTest extends TestCase {
abstract class AbstractTest extends EnhancedTestCase {

protected $_accountEmail = 'le_acme2_php_client@test.com';
protected $_orderSubjects = [];
Expand Down
26 changes: 19 additions & 7 deletions src/LE_ACME2Tests/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ public function testNonExistingCommonKeyDirectoryPath() {

$notExistingPath = TestHelper::getInstance()->getTempPath() . 'should-not-exist/';

$this->expectException(\RuntimeException::class);

\LE_ACME2\Account::setCommonKeyDirectoryPath($notExistingPath);
$this->catchExpectedException(
\RuntimeException::class,
function() use($notExistingPath) {
\LE_ACME2\Account::setCommonKeyDirectoryPath($notExistingPath);
}
);
}

public function testCommonKeyDirectoryPath() {
Expand All @@ -48,8 +51,13 @@ public function testNonExisting() {

$this->assertTrue(!\LE_ACME2\Account::exists($this->_accountEmail));

$this->expectException(\RuntimeException::class);
\LE_ACME2\Account::get($this->_accountEmail);
$this->catchExpectedException(
\RuntimeException::class,
function() {
\LE_ACME2\Account::get($this->_accountEmail);
}
);

}

public function testCreate() {
Expand Down Expand Up @@ -151,8 +159,12 @@ public function testDeactivation() {
$this->assertTrue($result === false);

// The account is already deactivated
$this->expectException(\LE_ACME2\Exception\InvalidResponse::class);
$account->getData();
$this->catchExpectedException(
\LE_ACME2\Exception\InvalidResponse::class,
function() use($account) {
$account->getData();
}
);
}

public function testCreationAfterDeactivation() {
Expand Down
8 changes: 6 additions & 2 deletions src/LE_ACME2Tests/Authorizer/HTTPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ public function testNonExistingDirectoryPath() {

$this->assertTrue(\LE_ACME2\Authorizer\HTTP::getDirectoryPath() === null);

$this->expectException(\RuntimeException::class);
\LE_ACME2\Authorizer\HTTP::setDirectoryPath(TestHelper::getInstance()->getNonExistingPath());
$this->catchExpectedException(
\RuntimeException::class,
function() {
\LE_ACME2\Authorizer\HTTP::setDirectoryPath(TestHelper::getInstance()->getNonExistingPath());
}
);
}

public function testDirectoryPath() {
Expand Down
31 changes: 31 additions & 0 deletions src/LE_ACME2Tests/EnhancedTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace LE_ACME2Tests;

use PHPUnit;

class EnhancedTestCase extends PHPUnit\Framework\TestCase {

/**
* @deprecated Exception is not caught. Additional assertions in the same TestCase method will not be executed. Use catchExpectedException
*
* @param string $exception
* @return void
*/
public function expectException(string $exception): void
{
parent::expectException($exception);
}

protected function catchExpectedException(string $exception, \Closure $callback) {

try {
$callback();
} catch (\Exception $e) {
$this->assertEquals($exception, get_class($e));
return;
}

throw new \RuntimeException('Expected exception not thrown: ' . $exception);
}
}
8 changes: 6 additions & 2 deletions src/LE_ACME2Tests/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public function testNonExisting() {

$this->assertTrue(!\LE_ACME2\Order::exists($account, $this->_orderSubjects));

$this->expectException(\RuntimeException::class);
\LE_ACME2\Order::get($account, $this->_orderSubjects);
$this->catchExpectedException(
\RuntimeException::class,
function() use($account) {
\LE_ACME2\Order::get($account, $this->_orderSubjects);
}
);
}

public function testCreate() {
Expand Down
3 changes: 2 additions & 1 deletion src/LE_ACME2Tests/Response/Authorization/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
namespace LE_ACME2Tests\Response\Authorization;

use LE_ACME2Tests\Connector;
use LE_ACME2Tests\EnhancedTestCase;
use PHPUnit\Framework\TestCase;

class GetTest extends TestCase {
class GetTest extends EnhancedTestCase {

/**
* @covers \LE_ACME2\Response\Authorization\Get::getChallenges
Expand Down
11 changes: 8 additions & 3 deletions src/LE_ACME2Tests/Response/Order/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

use LE_ACME2\Exception\OrderStatusInvalid;
use LE_ACME2Tests\Connector;
use LE_ACME2Tests\EnhancedTestCase;
use PHPUnit\Framework\TestCase;

class GetTest extends TestCase {
class GetTest extends EnhancedTestCase {

public function testGetChallengeError() {

Expand All @@ -14,7 +15,11 @@ public function testGetChallengeError() {
file_get_contents(dirname(__FILE__, 2) . DIRECTORY_SEPARATOR . '_JSONSamples' . DIRECTORY_SEPARATOR . 'OrderStatusInvalid.json')
);

$this->expectException(OrderStatusInvalid::class);
$response = new \LE_ACME2\Response\Order\Get($rawResponse, 'http://dummy.org');
$this->catchExpectedException(
OrderStatusInvalid::class,
function() use($rawResponse) {
new \LE_ACME2\Response\Order\Get($rawResponse, 'http://dummy.org');
}
);
}
}

0 comments on commit 4409ff9

Please sign in to comment.