ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
HTTPServicesTest.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\DI;
20 
33 
41 #[\PHPUnit\Framework\Attributes\BackupGlobals(false)]
42 #[\PHPUnit\Framework\Attributes\BackupStaticProperties(false)]
43 #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
44 #[\PHPUnit\Framework\Attributes\RunClassInSeparateProcess]
46 {
50  private RequestFactory $mockRequestFactory;
54  private ResponseFactory $mockResponseFactory;
58  private CookieJarFactory $mockCookieJarFactory;
62  private ResponseSenderStrategy $mockSenderStrategy;
66  private DurationFactory $mockDurationFactory;
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 
87  #[\PHPUnit\Framework\Attributes\Test]
89  {
90  $expectedRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
91  $wrongRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
92 
93  $this->mockRequestFactory->expects($this->once())
94  ->method('create')
95  ->willReturn($expectedRequest, $wrongRequest);
96 
97  //test method
98 
99  //this call should call the expectedRequest factory
100  $request1 = $this->httpState->request();
101 
102  //this call should not call the factory
103  $request2 = $this->httpState->request();
104 
105  //make sure that all requests are the same.
106  $this->assertEquals($expectedRequest, $request1);
107  $this->assertEquals($expectedRequest, $request2);
108  }
109 
110 
111  #[\PHPUnit\Framework\Attributes\Test]
113  {
114  $expectedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
115  $wrongResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
116 
117  $this->mockResponseFactory->expects($this->once())
118  ->method('create')
119  ->willReturn($expectedResponse, $wrongResponse);
120 
121  //test method
122 
123  //this call should call the expectedResponse factory
124  $response1 = $this->httpState->response();
125 
126  //this call should not call the factory
127  $response2 = $this->httpState->response();
128 
129  //make sure that all requests are the same.
130  $this->assertEquals($expectedResponse, $response1);
131  $this->assertEquals($expectedResponse, $response2);
132  }
133 }
ResponseSenderStrategy $mockSenderStrategy
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Container.php:19
DurationFactory $mockDurationFactory
RequestFactory $mockRequestFactory
CookieJarFactory $mockCookieJarFactory
ResponseFactory $mockResponseFactory