Skip to content

Commit b0617f8

Browse files
committed
[FEATURE] Update tests
1 parent 06c150b commit b0617f8

File tree

4 files changed

+56
-30
lines changed

4 files changed

+56
-30
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ Thumbs.db
66
/composer.lock
77
/public
88
/vendor
9+
/Build/.phpunit.cache
10+
/Build/.phpunit.result.cache

Build/UnitTests.xml

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
<phpunit backupGlobals="true"
2-
backupStaticAttributes="false"
3-
bootstrap="../../../vendor/typo3/testing-framework/Resources/Core/Build/UnitTestsBootstrap.php"
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
4+
bootstrap="../vendor/typo3/testing-framework/Resources/Core/Build/UnitTestsBootstrap.php"
45
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertWarningsToExceptions="true"
7-
forceCoversAnnotation="false"
86
processIsolation="false"
9-
stopOnError="false"
107
stopOnFailure="false"
8+
stopOnError="false"
119
stopOnIncomplete="false"
1210
stopOnSkipped="false"
13-
verbose="false">
11+
backupGlobals="true"
12+
displayDetailsOnTestsThatTriggerWarnings="true"
13+
displayDetailsOnTestsThatTriggerErrors="true"
14+
displayDetailsOnTestsThatTriggerNotices="true"
15+
cacheDirectory=".phpunit.cache"
16+
requireCoverageMetadata="false"
17+
beStrictAboutTestsThatDoNotTestAnything="true"
18+
beStrictAboutOutputDuringTests="true">
1419
<testsuites>
1520
<testsuite name="EXT:sierrha">
1621
<directory>../Tests/Unit/</directory>

Tests/Unit/Utility/UrlTest.php

+36-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Plan2net\Sierrha\Tests\Error;
44

5+
use PHPUnit\Framework\Attributes\Test;
6+
use PHPUnit\Framework\MockObject\Exception;
57
use Plan2net\Sierrha\Utility\Url;
8+
use TYPO3\CMS\Core\Http\Client\GuzzleClientFactory;
69
use TYPO3\CMS\Core\Localization\LanguageService;
710
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
811
use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -12,28 +15,29 @@
1215

1316
class UrlTest extends UnitTestCase
1417
{
15-
1618
protected const ERROR_PAGE_CONTROLLER_CONTENT = 'FALLBACK ERROR TEXT';
1719

1820
/**
1921
* System Under Test
20-
*
21-
* @var Url
2222
*/
23-
protected $sut;
23+
protected Url $sut;
2424

25-
/** @var LanguageService */
26-
protected $languageServiceStub;
25+
protected LanguageService $languageServiceStub;
2726

27+
/**
28+
* @throws Exception
29+
*/
2830
protected function setUp(): void
2931
{
3032
$this->sut = new Url();
3133

3234
$this->languageServiceStub = $this->createMock(LanguageService::class);
3335
$this->languageServiceStub->method('sL')->willReturn('lorem ipsum');
36+
37+
parent::setUp();
3438
}
3539

36-
protected function setupErrorPageControllerStub()
40+
protected function setupErrorPageControllerStub(): void
3741
{
3842
$errorPageControllerStub = $this->getMockBuilder(ErrorPageController::class)
3943
->disableOriginalConstructor()
@@ -43,12 +47,28 @@ protected function setupErrorPageControllerStub()
4347
GeneralUtility::addInstance(ErrorPageController::class, $errorPageControllerStub);
4448
}
4549

46-
protected function setupRequestFactoryStub($response)
50+
protected function setupRequestFactoryStub($response): void
4751
{
52+
// Create a stub for GuzzleClientFactory
53+
$guzzleClientStub = $this->getMockBuilder(\GuzzleHttp\Client::class)
54+
->disableOriginalConstructor()
55+
->getMock();
56+
$guzzleClientStub->method('request')
57+
->willReturn($response);
58+
59+
$guzzleFactoryStub = $this->getMockBuilder(GuzzleClientFactory::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
$guzzleFactoryStub->method('getClient')
63+
->willReturn($guzzleClientStub);
64+
65+
// Now create the RequestFactory stub with the required dependency
4866
$requestFactoryStub = $this->getMockBuilder(RequestFactory::class)
67+
->setConstructorArgs([$guzzleFactoryStub])
4968
->getMock();
5069
$requestFactoryStub->method('request')
5170
->willReturn($response);
71+
5272
GeneralUtility::addInstance(RequestFactory::class, $requestFactoryStub);
5373
}
5474

@@ -61,37 +81,33 @@ protected function buildResponseBody(string $body)
6181
return $stream;
6282
}
6383

64-
/**
65-
* @test
66-
*/
84+
#[Test]
6785
public function httpErrorOnFetchingUrlIsDetected()
6886
{
6987
$this->setupErrorPageControllerStub();
7088

89+
// Anything but 200
7190
$this->setupRequestFactoryStub(
7291
new Response($this->buildResponseBody('SERVER ERROR TEXT'), 500)
73-
); // anything but 200
92+
);
7493

7594
$result = $this->sut->fetchWithFallback('http://foo.bar/', $this->languageServiceStub, '');
7695
$this->assertEquals(self::ERROR_PAGE_CONTROLLER_CONTENT, $result);
7796
}
7897

79-
/**
80-
* @test
81-
*/
98+
#[Test]
8299
public function emptyContentOfFetchedUrlIsDetected()
83100
{
84101
$this->setupErrorPageControllerStub();
85102

86-
$this->setupRequestFactoryStub(new Response()); // will return an empty string
103+
// Will return an empty string
104+
$this->setupRequestFactoryStub(new Response());
87105

88106
$result = $this->sut->fetchWithFallback('http://foo.bar/', $this->languageServiceStub, '');
89107
$this->assertEquals(self::ERROR_PAGE_CONTROLLER_CONTENT, $result);
90108
}
91109

92-
/**
93-
* @test
94-
*/
110+
#[Test]
95111
public function unusableContentOfFetchedUrlIsDetected()
96112
{
97113
$this->setupErrorPageControllerStub();
@@ -102,9 +118,7 @@ public function unusableContentOfFetchedUrlIsDetected()
102118
$this->assertEquals(self::ERROR_PAGE_CONTROLLER_CONTENT, $result);
103119
}
104120

105-
/**
106-
* @test
107-
*/
121+
#[Test]
108122
public function usableContentOfFetchedUrlIsReturned()
109123
{
110124
$errorPageContent = 'CUSTOM ERROR PAGE TEXT';

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@
6060
"typo3/class-alias-loader": true,
6161
"typo3/cms-composer-installers": true
6262
}
63+
},
64+
"scripts": {
65+
"test": [
66+
"./vendor/bin/phpunit --config Build/UnitTests.xml"
67+
]
6368
}
6469
}

0 commit comments

Comments
 (0)