requestFactory = $this->getMockBuilder(RequestFactoryInterface::class) ->disableOriginalConstructor() ->getMock(); $this->streamFactory = $this->getMockBuilder(StreamFactoryInterface::class) ->disableOriginalConstructor() ->getMock(); $this->requestBuilder = new RequestBuilder(); // Everything but testing class is mock. Otherwise it wouldn't be unit testing $this->requestBuilder->setRequestFactory($this->requestFactory); $this->requestBuilder->setStreamFactory($this->streamFactory); } /** * Environment reset. */ protected function tearDown(): void { parent::tearDown(); $this->requestFactory = null; $this->requestBuilder = null; } public function testCreateSimpleStream() { $streamContent = 'content'; $stream = Stream::create($streamContent); $this->streamFactory ->expects($this->once()) ->method('createStream') ->with($streamContent) ->willReturn($stream); $request = $this->getMockBuilder(RequestInterface::class)->getMock(); $request->expects($this->once()) ->method('withBody') ->with($this->equalTo($stream)) ->willReturn($request); $request->expects($this->once()) ->method('withAddedHeader') ->with($this->equalTo('Content-Type'), $this->equalTo('application/json')) ->willReturn($request); $this->requestFactory ->expects($this->once()) ->method('createRequest') ->with( $this->equalTo('GET'), $this->equalTo('http://foo.bar') ) ->willReturn($request); $result = $this->requestBuilder ->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], 'content'); $this->assertSame($request, $result); } public function testCreateMultipartStream() { $multipartStreamBuilder = $this->createMultipartStreamBuilder(); $item0 = ['content' => 'foobar', 'name' => 'username', 'some_stuff' => 'some value']; $multipartStreamBuilder ->expects($this->once()) ->method('addResource') ->with( $this->equalTo($item0['name']), $this->equalTo($item0['content']), $this->callback(function (array $data) use ($item0) { unset($item0['name'], $item0['content']); $this->assertEquals($item0, $data); return true; }) ) ->willReturn($multipartStreamBuilder); $multipartStreamBuilder ->expects($this->once()) ->method('build') ->willReturn($stream = $this->getMockBuilder(StreamInterface::class)->getMock()); $multipartStreamBuilder ->expects($this->once()) ->method('getBoundary') ->willReturn('some boundary'); $multipartStreamBuilder ->expects($this->once()) ->method('reset') ->willReturn($multipartStreamBuilder); $request = $this->getMockBuilder(RequestInterface::class)->getMock(); $request->expects($this->once()) ->method('withBody') ->with($this->equalTo($stream)) ->willReturn($request); $request->expects($this->once()) ->method('withAddedHeader') ->with($this->equalTo('Content-Type'), $this->equalTo('multipart/form-data; boundary="some boundary"')) ->willReturn($request); $this->requestFactory ->expects($this->once()) ->method('createRequest') ->with( $this->equalTo('GET'), $this->equalTo('http://foo.bar') ) ->willReturn($request); $this->requestBuilder->setMultipartStreamBuilder($multipartStreamBuilder); $result = $this->requestBuilder ->create('GET', 'http://foo.bar', ['Content-Type' => 'application/json'], [$item0]); $this->assertSame($request, $result); } /** * Creates multipart stream builder. * * @return MockObject|MultipartStreamBuilder */ private function createMultipartStreamBuilder() { return $this->getMockBuilder(MultipartStreamBuilder::class)->disableOriginalConstructor()->getMock(); } }