Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 6324a5c

Browse files
committed
Added helper to expect one of the defined exception to be thrown
1 parent ee8ba14 commit 6324a5c

File tree

4 files changed

+103
-94
lines changed

4 files changed

+103
-94
lines changed

src/DelegatorTestTrait.php

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
namespace Zend\ContainerConfigTest;
1111

12+
use ArgumentCountError;
1213
use Error;
1314
use Generator;
1415
use Psr\Container\ContainerExceptionInterface;
15-
use Throwable;
16-
use TypeError;
16+
use Zend\ContainerConfigTest\Helper\Assert;
1717

1818
trait DelegatorTestTrait
1919
{
@@ -365,16 +365,12 @@ public function testDelegatorClassNameRequiringConstructorArgumentsResultsInExce
365365

366366
self::assertTrue($container->has(TestAsset\Service::class));
367367

368-
$caught = false;
369-
try {
370-
$container->get(TestAsset\Service::class);
371-
} catch (Throwable $e) {
372-
if ($e instanceof TypeError || $e instanceof ContainerExceptionInterface) {
373-
$caught = true;
374-
}
375-
}
376-
377-
$this->assertTrue($caught, 'No TypeError or ContainerExceptionInterface thrown when one was expected');
368+
Assert::expectedExceptions(
369+
function () use ($container) {
370+
$container->get(TestAsset\Service::class);
371+
},
372+
[ArgumentCountError::class, ContainerExceptionInterface::class]
373+
);
378374
}
379375

380376
public function factoriesForDelegators() : Generator
@@ -535,22 +531,12 @@ public function testWhenInvokableWithDelegatorsResolvesToNonExistentClassAnExcep
535531

536532
self::assertTrue($container->has(TestAsset\NonExistent::class));
537533

538-
$caught = false;
539-
try {
540-
$container->get(TestAsset\NonExistent::class);
541-
} catch (Throwable $e) {
542-
if (! $e instanceof Error && ! $e instanceof TypeError && ! $e instanceof ContainerExceptionInterface) {
543-
$this->fail(sprintf(
544-
'Throwable of type %s (%s) was raised; expected Error, TypeError, or %s',
545-
get_class($e),
546-
$e->getMessage(),
547-
ContainerExceptionInterface::class
548-
));
549-
}
550-
$caught = true;
551-
}
552-
553-
$this->assertTrue($caught, 'No TypeError or ContainerExceptionInterface thrown when one was expected');
534+
Assert::expectedExceptions(
535+
function () use ($container) {
536+
$container->get(TestAsset\NonExistent::class);
537+
},
538+
[Error::class, ContainerExceptionInterface::class]
539+
);
554540
}
555541

556542
// @codingStandardsIgnoreStart
@@ -570,22 +556,12 @@ public function testWhenInvokableWithDelegatorsResolvesToInvalidFactoryClassAnEx
570556

571557
self::assertTrue($container->has(TestAsset\FactoryWithRequiredParameters::class));
572558

573-
$caught = false;
574-
try {
575-
$container->get(TestAsset\FactoryWithRequiredParameters::class);
576-
} catch (Throwable $e) {
577-
if (! $e instanceof TypeError && ! $e instanceof ContainerExceptionInterface) {
578-
$this->fail(sprintf(
579-
'Throwable of type %s (%s) was raised; expected TypeError or %s',
580-
get_class($e),
581-
$e->getMessage(),
582-
ContainerExceptionInterface::class
583-
));
584-
}
585-
$caught = true;
586-
}
587-
588-
$this->assertTrue($caught, 'No TypeError or ContainerExceptionInterface thrown when one was expected');
559+
Assert::expectedExceptions(
560+
function () use ($container) {
561+
$container->get(TestAsset\FactoryWithRequiredParameters::class);
562+
},
563+
[ArgumentCountError::class, ContainerExceptionInterface::class]
564+
);
589565
}
590566

591567
// @codingStandardsIgnoreStart
@@ -663,21 +639,11 @@ public function testWhenServiceWithDelegatorsResolvesToInvalidFactoryClassAnExce
663639

664640
self::assertTrue($container->has('service'));
665641

666-
$caught = false;
667-
try {
668-
$container->get('service');
669-
} catch (Throwable $e) {
670-
if (! $e instanceof TypeError && ! $e instanceof ContainerExceptionInterface) {
671-
$this->fail(sprintf(
672-
'Throwable of type %s (%s) was raised; expected TypeError or %s',
673-
get_class($e),
674-
$e->getMessage(),
675-
ContainerExceptionInterface::class
676-
));
677-
}
678-
$caught = true;
679-
}
680-
681-
$this->assertTrue($caught, 'No TypeError or ContainerExceptionInterface thrown when one was expected');
642+
Assert::expectedExceptions(
643+
function () use ($container) {
644+
$container->get('service');
645+
},
646+
[ArgumentCountError::class, ContainerExceptionInterface::class]
647+
);
682648
}
683649
}

src/FactoryTestTrait.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
namespace Zend\ContainerConfigTest;
1111

12+
use ArgumentCountError;
1213
use Generator;
1314
use Psr\Container\ContainerInterface;
1415
use Psr\Container\ContainerExceptionInterface;
15-
use Throwable;
16-
use TypeError;
16+
use Zend\ContainerConfigTest\Helper\Assert;
1717

1818
trait FactoryTestTrait
1919
{
@@ -130,15 +130,11 @@ public function testFactoryClassNameRequiringConstructorArgumentsResultsInExcept
130130

131131
self::assertTrue($container->has('service'));
132132

133-
$caught = false;
134-
try {
135-
$container->get('service');
136-
} catch (Throwable $e) {
137-
if ($e instanceof TypeError || $e instanceof ContainerExceptionInterface) {
138-
$caught = true;
139-
}
140-
}
141-
142-
$this->assertTrue($caught, 'No TypeError or ContainerExceptionInterface thrown when one was expected');
133+
Assert::expectedExceptions(
134+
function () use ($container) {
135+
$container->get('service');
136+
},
137+
[ArgumentCountError::class, ContainerExceptionInterface::class]
138+
);
143139
}
144140
}

src/Helper/Assert.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-container-config-test for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-container-config-test/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zend\ContainerConfigTest\Helper;
11+
12+
use PHPUnit\Framework\Assert as PHPUnitAssert;
13+
use Throwable;
14+
15+
use function get_class;
16+
use function implode;
17+
use function sprintf;
18+
19+
class Assert {
20+
21+
public static function expectedExceptions(callable $function, array $exceptions) : void
22+
{
23+
$caught = false;
24+
25+
try {
26+
$function();
27+
} catch (Throwable $e) {
28+
if (! self::isInstanceOf($e, $exceptions)) {
29+
PHPUnitAssert::fail(sprintf(
30+
'Throwable of type %s (%s) was raised; expected one of %s',
31+
get_class($e),
32+
$e->getMessage(),
33+
implode(', ', $exceptions)
34+
));
35+
}
36+
37+
$caught = true;
38+
}
39+
40+
PHPUnitAssert::assertTrue(
41+
$caught,
42+
sprintf('No any of [%s] thrown when one was expected', implode(', ', $exceptions))
43+
);
44+
}
45+
46+
private static function isInstanceOf(Throwable $e, array $types) : bool
47+
{
48+
foreach ($types as $type) {
49+
if ($e instanceof $type) {
50+
return true;
51+
}
52+
}
53+
54+
return false;
55+
}
56+
}

src/InvokableTestTrait.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
use ArgumentCountError;
1313
use Psr\Container\ContainerExceptionInterface;
14-
use Throwable;
15-
use TypeError;
14+
use Zend\ContainerConfigTest\Helper\Assert;
1615

1716
trait InvokableTestTrait
1817
{
@@ -99,16 +98,12 @@ public function testFetchingInvokableThatHasRequiredConstructorParametersResults
9998

10099
self::assertTrue($container->has(TestAsset\Delegator::class));
101100

102-
$caught = false;
103-
try {
104-
$container->get(TestAsset\Delegator::class);
105-
} catch (Throwable $e) {
106-
if ($e instanceof TypeError || $e instanceof ContainerExceptionInterface) {
107-
$caught = true;
108-
}
109-
}
110-
111-
$this->assertTrue($caught, 'No TypeError or ContainerExceptionInterface thrown when one was expected');
101+
Assert::expectedExceptions(
102+
function () use ($container) {
103+
$container->get(TestAsset\Delegator::class);
104+
},
105+
[ArgumentCountError::class, ContainerExceptionInterface::class]
106+
);
112107
}
113108

114109
public function testFetchingInvalidInvokableServiceByAliasResultsInException()
@@ -123,15 +118,11 @@ public function testFetchingInvalidInvokableServiceByAliasResultsInException()
123118

124119
self::assertTrue($container->has('alias'));
125120

126-
$caught = false;
127-
try {
128-
$container->get('alias');
129-
} catch (Throwable $e) {
130-
if ($e instanceof ArgumentCountError || $e instanceof ContainerExceptionInterface) {
131-
$caught = true;
132-
}
133-
}
134-
135-
$this->assertTrue($caught, 'No ArgumentError or ContainerExceptionInterface thrown when one was expected');
121+
Assert::expectedExceptions(
122+
function () use ($container) {
123+
$container->get('alias');
124+
},
125+
[ArgumentCountError::class, ContainerExceptionInterface::class]
126+
);
136127
}
137128
}

0 commit comments

Comments
 (0)