ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
HTTPServicesTest.php
Go to the documentation of this file.
1<?php
2
19namespace ILIAS\DI;
20
28use PHPUnit\Framework\MockObject\MockObject;
29use PHPUnit\Framework\TestCase as PHPUnitTestCase;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
35
43#[\PHPUnit\Framework\Attributes\BackupGlobals(false)]
44#[\PHPUnit\Framework\Attributes\BackupStaticProperties(false)]
45#[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
46#[\PHPUnit\Framework\Attributes\RunClassInSeparateProcess]
48{
70
71
72 protected function setUp(): void
73 {
75 $this->mockRequestFactory = $this->getMockBuilder(RequestFactory::class)->getMock();
76
77 $this->mockResponseFactory = $this->getMockBuilder(ResponseFactory::class)->getMock();
78
79 $this->mockSenderStrategy = $this->getMockBuilder(ResponseSenderStrategy::class)->getMock();
80
81 $this->mockCookieJarFactory = $this->getMockBuilder(CookieJarFactory::class)->getMock();
82
83 $this->mockDurationFactory = $this->createMock(DurationFactory::class);
84
85 $this->httpState = new RawHTTPServices($this->mockSenderStrategy, $this->mockCookieJarFactory, $this->mockRequestFactory, $this->mockResponseFactory, $this->mockDurationFactory, $this->createMock(CookieFactory::class), $this->createMock(MessageFactory::class));
86 }
87
88
89 #[\PHPUnit\Framework\Attributes\Test]
91 {
92 $expectedRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
93 $wrongRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
94
95 $this->mockRequestFactory->expects($this->once())
96 ->method('create')
97 ->willReturn($expectedRequest, $wrongRequest);
98
99 //test method
100
101 //this call should call the expectedRequest factory
102 $request1 = $this->httpState->request();
103
104 //this call should not call the factory
105 $request2 = $this->httpState->request();
106
107 //make sure that all requests are the same.
108 $this->assertEquals($expectedRequest, $request1);
109 $this->assertEquals($expectedRequest, $request2);
110 }
111
112
113 #[\PHPUnit\Framework\Attributes\Test]
115 {
116 $expectedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
117 $wrongResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
118
119 $this->mockResponseFactory->expects($this->once())
120 ->method('create')
121 ->willReturn($expectedResponse, $wrongResponse);
122
123 //test method
124
125 //this call should call the expectedResponse factory
126 $response1 = $this->httpState->response();
127
128 //this call should not call the factory
129 $response2 = $this->httpState->response();
130
131 //make sure that all requests are the same.
132 $this->assertEquals($expectedResponse, $response1);
133 $this->assertEquals($expectedResponse, $response2);
134 }
135}
RequestFactory $mockRequestFactory
ResponseSenderStrategy $mockSenderStrategy
ResponseFactory $mockResponseFactory
DurationFactory $mockDurationFactory
CookieJarFactory $mockCookieJarFactory
Provides an interface to the ILIAS HTTP services.
Class Services.
Definition: Services.php:43
Interface GlobalHttpState.
Creates psr-7 messages without binding components to the underlying library.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Container.php:19