Skip to content

Commit c1c614c

Browse files
committed
🚿 TestAbstract -> FactoryTrait
1 parent afb8800 commit c1c614c

File tree

7 files changed

+91
-76
lines changed

7 files changed

+91
-76
lines changed

tests/FactoryTrait.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Trait FactoryTrait
4+
*
5+
* @created 29.03.2021
6+
* @author smiley <smiley@chillerlan.net>
7+
* @copyright 2021 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\HTTPTest\Utils;
12+
13+
use chillerlan\HTTP\Utils\ServerUtil;
14+
use Exception;
15+
use Psr\Http\Message\{
16+
RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface,
17+
StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
18+
};
19+
use function class_exists;
20+
use function constant;
21+
use function defined;
22+
use function method_exists;
23+
use function sprintf;
24+
25+
trait FactoryTrait{
26+
27+
private array $FACTORIES = [
28+
'requestFactory' => 'REQUEST_FACTORY',
29+
'responseFactory' => 'RESPONSE_FACTORY',
30+
'serverRequestFactory' => 'SERVER_REQUEST_FACTORY',
31+
'streamFactory' => 'STREAM_FACTORY',
32+
'uploadedFileFactory' => 'UPLOADED_FILE_FACTORY',
33+
'uriFactory' => 'URI_FACTORY',
34+
];
35+
36+
protected RequestFactoryInterface $requestFactory;
37+
protected ResponseFactoryInterface $responseFactory;
38+
protected ServerRequestFactoryInterface $serverRequestFactory;
39+
protected StreamFactoryInterface $streamFactory;
40+
protected UploadedFileFactoryInterface $uploadedFileFactory;
41+
protected UriFactoryInterface $uriFactory;
42+
protected ServerUtil $server;
43+
44+
/**
45+
* @throws \Exception
46+
*/
47+
protected function setUp():void{
48+
49+
foreach($this->FACTORIES as $property => $const){
50+
51+
if(!defined($const)){
52+
throw new Exception(sprintf('constant "%s" not defined -> see phpunit.xml', $const));
53+
}
54+
55+
$class = constant($const);
56+
57+
if(!class_exists($class)){
58+
throw new Exception(sprintf('invalid class: "%s"', $class));
59+
}
60+
61+
$this->{$property} = new $class;
62+
}
63+
64+
$this->server = new ServerUtil(
65+
$this->serverRequestFactory,
66+
$this->uriFactory,
67+
$this->uploadedFileFactory,
68+
$this->streamFactory
69+
);
70+
71+
if(method_exists($this, '__setUp')){
72+
$this->__setUp();
73+
}
74+
75+
}
76+
77+
}

tests/HeaderUtilTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
use chillerlan\HTTP\Utils\HeaderUtil;
1414
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\TestCase;
1516

1617
/**
1718
*
1819
*/
19-
class HeaderUtilTest extends TestAbstract{
20+
class HeaderUtilTest extends TestCase{
21+
use FactoryTrait;
2022

2123
public static function headerDataProvider():array{
2224
return [

tests/MessageUtilTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use chillerlan\HTTP\Utils\MessageUtil;
1414
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\TestCase;
1516
use RuntimeException;
1617
use function extension_loaded;
1718
use function function_exists;
@@ -21,7 +22,8 @@
2122
/**
2223
*
2324
*/
24-
class MessageUtilTest extends TestAbstract{
25+
class MessageUtilTest extends TestCase{
26+
use FactoryTrait;
2527

2628
public function testGetJSON():void{
2729

tests/MimeTypeUtilTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
namespace chillerlan\HTTPTest\Utils;
1212

1313
use chillerlan\HTTP\Utils\MimeTypeUtil;
14+
use PHPUnit\Framework\TestCase;
1415
use function file_get_contents;
1516

1617
/**
1718
*
1819
*/
19-
class MimeTypeUtilTest extends TestAbstract{
20+
class MimeTypeUtilTest extends TestCase{
2021

2122
public function testGetMimetypeFromExtension():void{
2223
$this::assertSame('application/json', MimeTypeUtil::getFromExtension('json'));

tests/ServerUtilTest.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,16 @@
1212

1313
namespace chillerlan\HTTPTest\Utils;
1414

15-
use chillerlan\HTTP\Utils\ServerUtil;
1615
use InvalidArgumentException;
1716
use PHPUnit\Framework\Attributes\DataProvider;
17+
use PHPUnit\Framework\TestCase;
1818
use function microtime;
1919
use function time;
2020
use const UPLOAD_ERR_OK;
2121
use const UPLOAD_ERR_PARTIAL;
2222

23-
class ServerUtilTest extends TestAbstract{
24-
25-
protected ServerUtil $server;
26-
27-
protected function setUp():void{
28-
parent::setUp();
29-
30-
$this->server = new ServerUtil(
31-
$this->serverRequestFactory,
32-
$this->uriFactory,
33-
$this->uploadedFileFactory,
34-
$this->streamFactory
35-
);
36-
}
23+
class ServerUtilTest extends TestCase{
24+
use FactoryTrait;
3725

3826
public static function dataGetUriFromGlobals():array{
3927

tests/TestAbstract.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

tests/UriUtilTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
namespace chillerlan\HTTPTest\Utils;
1212

1313
use chillerlan\HTTP\Utils\UriUtil;
14+
use PHPUnit\Framework\TestCase;
1415

1516
/**
1617
*
1718
*/
18-
class UriUtilTest extends TestAbstract{
19+
class UriUtilTest extends TestCase{
20+
use FactoryTrait;
1921

2022
public function testUriIsAbsolute():void{
2123
$this::assertTrue(UriUtil::isAbsolute($this->uriFactory->createUri('http://example.org')));

0 commit comments

Comments
 (0)