2
2
3
3
namespace Plan2net \Sierrha \Tests \Error ;
4
4
5
+ use PHPUnit \Framework \Attributes \Test ;
6
+ use PHPUnit \Framework \MockObject \Exception ;
5
7
use Plan2net \Sierrha \Utility \Url ;
8
+ use TYPO3 \CMS \Core \Http \Client \GuzzleClientFactory ;
6
9
use TYPO3 \CMS \Core \Localization \LanguageService ;
7
10
use TYPO3 \TestingFramework \Core \Unit \UnitTestCase ;
8
11
use TYPO3 \CMS \Core \Utility \GeneralUtility ;
12
15
13
16
class UrlTest extends UnitTestCase
14
17
{
15
-
16
18
protected const ERROR_PAGE_CONTROLLER_CONTENT = 'FALLBACK ERROR TEXT ' ;
17
19
18
20
/**
19
21
* System Under Test
20
- *
21
- * @var Url
22
22
*/
23
- protected $ sut ;
23
+ protected Url $ sut ;
24
24
25
- /** @var LanguageService */
26
- protected $ languageServiceStub ;
25
+ protected LanguageService $ languageServiceStub ;
27
26
27
+ /**
28
+ * @throws Exception
29
+ */
28
30
protected function setUp (): void
29
31
{
30
32
$ this ->sut = new Url ();
31
33
32
34
$ this ->languageServiceStub = $ this ->createMock (LanguageService::class);
33
35
$ this ->languageServiceStub ->method ('sL ' )->willReturn ('lorem ipsum ' );
36
+
37
+ parent ::setUp ();
34
38
}
35
39
36
- protected function setupErrorPageControllerStub ()
40
+ protected function setupErrorPageControllerStub (): void
37
41
{
38
42
$ errorPageControllerStub = $ this ->getMockBuilder (ErrorPageController::class)
39
43
->disableOriginalConstructor ()
@@ -43,12 +47,28 @@ protected function setupErrorPageControllerStub()
43
47
GeneralUtility::addInstance (ErrorPageController::class, $ errorPageControllerStub );
44
48
}
45
49
46
- protected function setupRequestFactoryStub ($ response )
50
+ protected function setupRequestFactoryStub ($ response ): void
47
51
{
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
48
66
$ requestFactoryStub = $ this ->getMockBuilder (RequestFactory::class)
67
+ ->setConstructorArgs ([$ guzzleFactoryStub ])
49
68
->getMock ();
50
69
$ requestFactoryStub ->method ('request ' )
51
70
->willReturn ($ response );
71
+
52
72
GeneralUtility::addInstance (RequestFactory::class, $ requestFactoryStub );
53
73
}
54
74
@@ -61,37 +81,33 @@ protected function buildResponseBody(string $body)
61
81
return $ stream ;
62
82
}
63
83
64
- /**
65
- * @test
66
- */
84
+ #[Test]
67
85
public function httpErrorOnFetchingUrlIsDetected ()
68
86
{
69
87
$ this ->setupErrorPageControllerStub ();
70
88
89
+ // Anything but 200
71
90
$ this ->setupRequestFactoryStub (
72
91
new Response ($ this ->buildResponseBody ('SERVER ERROR TEXT ' ), 500 )
73
- ); // anything but 200
92
+ );
74
93
75
94
$ result = $ this ->sut ->fetchWithFallback ('http://foo.bar/ ' , $ this ->languageServiceStub , '' );
76
95
$ this ->assertEquals (self ::ERROR_PAGE_CONTROLLER_CONTENT , $ result );
77
96
}
78
97
79
- /**
80
- * @test
81
- */
98
+ #[Test]
82
99
public function emptyContentOfFetchedUrlIsDetected ()
83
100
{
84
101
$ this ->setupErrorPageControllerStub ();
85
102
86
- $ this ->setupRequestFactoryStub (new Response ()); // will return an empty string
103
+ // Will return an empty string
104
+ $ this ->setupRequestFactoryStub (new Response ());
87
105
88
106
$ result = $ this ->sut ->fetchWithFallback ('http://foo.bar/ ' , $ this ->languageServiceStub , '' );
89
107
$ this ->assertEquals (self ::ERROR_PAGE_CONTROLLER_CONTENT , $ result );
90
108
}
91
109
92
- /**
93
- * @test
94
- */
110
+ #[Test]
95
111
public function unusableContentOfFetchedUrlIsDetected ()
96
112
{
97
113
$ this ->setupErrorPageControllerStub ();
@@ -102,9 +118,7 @@ public function unusableContentOfFetchedUrlIsDetected()
102
118
$ this ->assertEquals (self ::ERROR_PAGE_CONTROLLER_CONTENT , $ result );
103
119
}
104
120
105
- /**
106
- * @test
107
- */
121
+ #[Test]
108
122
public function usableContentOfFetchedUrlIsReturned ()
109
123
{
110
124
$ errorPageContent = 'CUSTOM ERROR PAGE TEXT ' ;
0 commit comments