Skip to content

Commit a87833c

Browse files
committed
Extract ExceptionTestTrait.
1 parent 3bbf254 commit a87833c

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

tests/src/ExceptionTestTrait.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Ock\ClassFilesIterator\Tests;
6+
7+
trait ExceptionTestTrait {
8+
9+
/**
10+
* @param class-string $exception_class
11+
* @param callable(): (void|mixed) $callback
12+
*/
13+
protected function callAndAssertException(string $exception_class, callable $callback): void {
14+
// Does not work with non-existing class.
15+
try {
16+
$callback();
17+
$this->fail("Expected exception was not thrown.");
18+
}
19+
catch (\Throwable $e) {
20+
if (get_class($e) !== $exception_class) {
21+
throw $e;
22+
}
23+
$this->addToAssertionCount(1);
24+
}
25+
}
26+
27+
}

tests/src/NamespaceDirectoryTest.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
class NamespaceDirectoryTest extends TestCase {
1212

13+
use ExceptionTestTrait;
1314
use ImmutableObjectsTrait;
1415

1516
public function testCreate(): void {
@@ -488,24 +489,6 @@ protected function assertNamespaceDir(
488489
$this->assertSame($namespace, $namespace_directory->getNamespace());
489490
}
490491

491-
/**
492-
* @param class-string $exception_class
493-
* @param callable(): (void|mixed) $callback
494-
*/
495-
protected function callAndAssertException(string $exception_class, callable $callback): void {
496-
// Does not work with non-existing class.
497-
try {
498-
$callback();
499-
$this->fail("Expected exception was not thrown.");
500-
}
501-
catch (\Throwable $e) {
502-
if (get_class($e) !== $exception_class) {
503-
throw $e;
504-
}
505-
$this->addToAssertionCount(1);
506-
}
507-
}
508-
509492
/**
510493
* @param string $directory
511494
* @param string $namespace

0 commit comments

Comments
 (0)