ILIAS  release_7 Revision v7.30-3-g800a261c036
HTTPServicesTest.php
Go to the documentation of this file.
1<?php
8namespace ILIAS\DI;
9
15use PHPUnit\Framework\MockObject\MockObject;
16use PHPUnit\Framework\TestCase as PHPUnitTestCase;
17use Psr\Http\Message\ResponseInterface;
18use Psr\Http\Message\ServerRequestInterface;
19
31{
32
52 private $httpState;
53
54
55 protected function setUp() : void
56 {
57 parent::setUp();
58 // $this->mockRequestFactory = \Mockery::mock('alias:' . RequestFactory::class);
59 $this->mockRequestFactory = $this->getMockBuilder(RequestFactory::class)->setMethods(['create'])->getMock();
60
61 // $this->mockResponseFactory = \Mockery::mock('alias:' . ResponseFactory::class);
62 $this->mockResponseFactory = $this->getMockBuilder(ResponseFactory::class)->setMethods(['create'])->getMock();
63
64 // $this->mockSenderStrategy = \Mockery::mock('alias:' . ResponseSenderStrategy::class);
65 $this->mockSenderStrategy = $this->getMockBuilder(ResponseSenderStrategy::class)->getMock();
66
67 // $this->mockCookieJarFactory = \Mockery::mock('alias:' . CookieJarFactory::class);
68 $this->mockCookieJarFactory = $this->getMockBuilder(CookieJarFactory::class)->getMock();
69
70 //setup http state
71 $this->httpState = new HTTPServices($this->mockSenderStrategy, $this->mockCookieJarFactory, $this->mockRequestFactory, $this->mockResponseFactory);
72 }
73
74
79 {
80 $expectedRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
81 $wrongRequest = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
82
83 $this->mockRequestFactory->expects($this->once())
84 ->method('create')
85 ->willReturn($expectedRequest, $wrongRequest);
86
87 //test method
88
89 //this call should call the expectedRequest factory
90 $request1 = $this->httpState->request();
91
92 //this call should not call the factory
93 $request2 = $this->httpState->request();
94
95 //make sure that all requests are the same.
96 $this->assertEquals($expectedRequest, $request1);
97 $this->assertEquals($expectedRequest, $request2);
98 }
99
100
105 {
106 $expectedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
107 $wrongResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
108
109 $this->mockResponseFactory->expects($this->once())
110 ->method('create')
111 ->willReturn($expectedResponse, $wrongResponse);
112
113 //test method
114
115 //this call should call the expectedResponse factory
116 $response1 = $this->httpState->response();
117
118 //this call should not call the factory
119 $response2 = $this->httpState->response();
120
121 //make sure that all requests are the same.
122 $this->assertEquals($expectedResponse, $response1);
123 $this->assertEquals($expectedResponse, $response2);
124 }
125}
An exception for terminatinating execution or to throw for unit testing.
testResponseWhichShouldGenerateANewResponseOnce()
@Test
testRequestWhichShouldGenerateANewRequestOnce()
@Test
Provides an interface to the ILIAS HTTP services.
Interface GlobalHttpState.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...