ILIAS  release_8 Revision v8.24
HTTPServicesTest.php
Go to the documentation of this file.
1<?php
2
18namespace ILIAS\DI;
19
25use PHPUnit\Framework\MockObject\MockObject;
26use PHPUnit\Framework\TestCase as PHPUnitTestCase;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
32
46{
68
69
70 protected function setUp(): void
71 {
72 parent::setUp();
73 $this->mockRequestFactory = $this->getMockBuilder(RequestFactory::class)->getMock();
74
75 $this->mockResponseFactory = $this->getMockBuilder(ResponseFactory::class)->getMock();
76
77 $this->mockSenderStrategy = $this->getMockBuilder(ResponseSenderStrategy::class)->getMock();
78
79 $this->mockCookieJarFactory = $this->getMockBuilder(CookieJarFactory::class)->getMock();
80
81 $this->mockDurationFactory = $this->createMock(DurationFactory::class);
82
83 $this->httpState = new RawHTTPServices($this->mockSenderStrategy, $this->mockCookieJarFactory, $this->mockRequestFactory, $this->mockResponseFactory, $this->mockDurationFactory);
84 }
85
86
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
117 {
118 $expectedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
119 $wrongResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
120
121 $this->mockResponseFactory->expects($this->once())
122 ->method('create')
123 ->willReturn($expectedResponse, $wrongResponse);
124
125 //test method
126
127 //this call should call the expectedResponse factory
128 $response1 = $this->httpState->response();
129
130 //this call should not call the factory
131 $response2 = $this->httpState->response();
132
133 //make sure that all requests are the same.
134 $this->assertEquals($expectedResponse, $response1);
135 $this->assertEquals($expectedResponse, $response2);
136 }
137}
RequestFactory $mockRequestFactory
ResponseSenderStrategy $mockSenderStrategy
testResponseWhichShouldGenerateANewResponseOnce()
@Test
ResponseFactory $mockResponseFactory
DurationFactory $mockDurationFactory
testRequestWhichShouldGenerateANewRequestOnce()
@Test
CookieJarFactory $mockCookieJarFactory
Provides an interface to the ILIAS HTTP services.
Class Services.
Definition: Services.php:38
Interface GlobalHttpState.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Container.php:19