Permalink
Browse files

Use methods to define exception expectations

Since the usage of these annotations are going to be deprecated in
PHPUnit 8 (according to the roadmap).
  • Loading branch information...
lcobucci committed Oct 14, 2018
1 parent 322318f commit 9f100e499cd92c6bf962a2946d30176a51824a12
@@ -3,6 +3,7 @@
namespace Lcobucci\JWT\FunctionalTests;
use InvalidArgumentException;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Keys;
use Lcobucci\JWT\Signer\Ecdsa\Sha256;
@@ -11,6 +12,7 @@
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Token\Signature;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\InvalidToken;
use PHPUnit\Framework\TestCase;
use const PHP_EOL;
@@ -38,8 +40,6 @@ public function createConfiguration(): void
/**
* @test
*
* @expectedException \InvalidArgumentException
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Plain
@@ -55,6 +55,9 @@ public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void
{
$builder = $this->config->createBuilder();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('It was not possible to parse your key, reason:');
$builder->identifiedBy('1')
->permittedFor('http://client.abc.com')
->issuedBy('http://api.abc.com')
@@ -65,8 +68,6 @@ public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void
/**
* @test
*
* @expectedException \InvalidArgumentException
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Plain
@@ -82,6 +83,9 @@ public function builderShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(): void
{
$builder = $this->config->createBuilder();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('This key is not compatible with this signer');
$builder->identifiedBy('1')
->permittedFor('http://client.abc.com')
->issuedBy('http://api.abc.com')
@@ -156,8 +160,6 @@ public function parserCanReadAToken(Token $generated): void
* @test
* @depends builderCanGenerateAToken
*
* @expectedException \Lcobucci\JWT\Validation\InvalidToken
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
@@ -175,6 +177,9 @@ public function parserCanReadAToken(Token $generated): void
*/
public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $token): void
{
$this->expectException(InvalidToken::class);
$this->expectExceptionMessage('The token violates some mandatory constraints');
$this->config->getValidator()->assert(
$token,
new SignedWith(
@@ -188,8 +193,6 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t
* @test
* @depends builderCanGenerateAToken
*
* @expectedException \Lcobucci\JWT\Validation\InvalidToken
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
@@ -208,6 +211,9 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t
*/
public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(Token $token): void
{
$this->expectException(InvalidToken::class);
$this->expectExceptionMessage('The token violates some mandatory constraints');
$this->config->getValidator()->assert(
$token,
new SignedWith(
@@ -221,8 +227,6 @@ public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(T
* @test
* @depends builderCanGenerateAToken
*
* @expectedException \InvalidArgumentException
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
@@ -240,6 +244,9 @@ public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(T
*/
public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(Token $token): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('This key is not compatible with this signer');
$this->config->getValidator()->assert(
$token,
new SignedWith($this->config->getSigner(), self::$rsaKeys['public'])
@@ -10,6 +10,7 @@
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Token\Signature;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\InvalidToken;
use PHPUnit\Framework\TestCase;
class HmacTokenTest extends TestCase
@@ -85,8 +86,6 @@ public function parserCanReadAToken(Token $generated): void
* @test
* @depends builderCanGenerateAToken
*
* @expectedException \Lcobucci\JWT\Validation\InvalidToken
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
@@ -102,6 +101,9 @@ public function parserCanReadAToken(Token $generated): void
*/
public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $token): void
{
$this->expectException(InvalidToken::class);
$this->expectExceptionMessage('The token violates some mandatory constraints');
$this->config->getValidator()->assert(
$token,
new SignedWith($this->config->getSigner(), new Key('testing1'))
@@ -112,8 +114,6 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t
* @test
* @depends builderCanGenerateAToken
*
* @expectedException \Lcobucci\JWT\Validation\InvalidToken
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
@@ -130,6 +130,9 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t
*/
public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(Token $token): void
{
$this->expectException(InvalidToken::class);
$this->expectExceptionMessage('The token violates some mandatory constraints');
$this->config->getValidator()->assert(
$token,
new SignedWith(new Sha512(), $this->config->getVerificationKey())
@@ -3,6 +3,7 @@
namespace Lcobucci\JWT\FunctionalTests;
use InvalidArgumentException;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Keys;
use Lcobucci\JWT\Signer\Key;
@@ -11,6 +12,7 @@
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Token\Signature;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\InvalidToken;
use PHPUnit\Framework\TestCase;
class RsaTokenTest extends TestCase
@@ -37,8 +39,6 @@ public function createConfiguration(): void
/**
* @test
*
* @expectedException \InvalidArgumentException
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Plain
@@ -53,6 +53,9 @@ public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void
{
$builder = $this->config->createBuilder();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('It was not possible to parse your key');
$builder->identifiedBy('1')
->permittedFor('http://client.abc.com')
->issuedBy('http://api.abc.com')
@@ -63,8 +66,6 @@ public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void
/**
* @test
*
* @expectedException \InvalidArgumentException
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Plain
@@ -79,6 +80,9 @@ public function builderShouldRaiseExceptionWhenKeyIsNotRsaCompatible(): void
{
$builder = $this->config->createBuilder();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('This key is not compatible with this signer');
$builder->identifiedBy('1')
->permittedFor('http://client.abc.com')
->issuedBy('http://api.abc.com')
@@ -144,8 +148,6 @@ public function parserCanReadAToken(Token $generated): void
* @test
* @depends builderCanGenerateAToken
*
* @expectedException \Lcobucci\JWT\Validation\InvalidToken
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
@@ -162,6 +164,9 @@ public function parserCanReadAToken(Token $generated): void
*/
public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $token): void
{
$this->expectException(InvalidToken::class);
$this->expectExceptionMessage('The token violates some mandatory constraints');
$this->config->getValidator()->assert(
$token,
new SignedWith($this->config->getSigner(), self::$rsaKeys['encrypted-public'])
@@ -172,8 +177,6 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t
* @test
* @depends builderCanGenerateAToken
*
* @expectedException \Lcobucci\JWT\Validation\InvalidToken
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
@@ -191,6 +194,9 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t
*/
public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(Token $token): void
{
$this->expectException(InvalidToken::class);
$this->expectExceptionMessage('The token violates some mandatory constraints');
$this->config->getValidator()->assert(
$token,
new SignedWith(new Sha512(), $this->config->getVerificationKey())
@@ -201,8 +207,6 @@ public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(T
* @test
* @depends builderCanGenerateAToken
*
* @expectedException \InvalidArgumentException
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
@@ -218,6 +222,9 @@ public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(T
*/
public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRsaCompatible(Token $token): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('This key is not compatible with this signer');
$this->config->getValidator()->assert(
$token,
new SignedWith(
@@ -13,6 +13,7 @@
use Lcobucci\JWT\Validation\Constraint\PermittedFor;
use Lcobucci\JWT\Validation\Constraint\ValidAt;
use Lcobucci\JWT\Validation\ConstraintViolation;
use Lcobucci\JWT\Validation\InvalidToken;
use PHPUnit\Framework\TestCase;
class UnsignedTokenTest extends TestCase
@@ -143,8 +144,6 @@ public function tokenValidationShouldAllowCustomConstraint(Token $generated): vo
* @test
* @depends builderCanGenerateAToken
*
* @expectedException \Lcobucci\JWT\Validation\InvalidToken
*
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
@@ -165,6 +164,9 @@ public function tokenAssertionShouldRaiseExceptionWhenOneOfTheConstraintsFails(T
new IssuedBy('http://issuer.abc.com'),
];
$this->expectException(InvalidToken::class);
$this->expectExceptionMessage('The token violates some mandatory constraints');
$this->config->getValidator()->assert($generated, ...$constraints);
}
@@ -3,6 +3,7 @@
namespace Lcobucci\JWT\Signer;
use InvalidArgumentException;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
@@ -52,14 +53,15 @@ public function constructShouldBeAbleToConfigureContentFromFile(): void
/**
* @test
*
* @expectedException \InvalidArgumentException
*
* @covers \Lcobucci\JWT\Signer\Key::__construct
* @covers \Lcobucci\JWT\Signer\Key::setContent
* @covers \Lcobucci\JWT\Signer\Key::readFile
*/
public function constructShouldRaiseExceptionWhenFileDoesNotExists(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('You must inform a valid key file');
new Key('file://' . vfsStream::url('root/test2.pem'));
}
Oops, something went wrong.

0 comments on commit 9f100e4

Please sign in to comment.