Skip to content

Commit

Permalink
qa: remove prophecy as there are some issues with PHP 8
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
  • Loading branch information
boesing committed Oct 31, 2020
1 parent 22ea099 commit bfd0439
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 81 deletions.
1 change: 0 additions & 1 deletion composer.json
Expand Up @@ -35,7 +35,6 @@
"laminas/laminas-http": "^2.7",
"laminas/laminas-servicemanager": "^3.3",
"laminas/laminas-validator": "^2.10.1",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.3",
"psr/http-message": "^1.0.1"
},
Expand Down
149 changes: 110 additions & 39 deletions test/Reader/Http/LaminasHttpClientDecoratorTest.php
Expand Up @@ -15,64 +15,104 @@
use Laminas\Http\Headers;
use Laminas\Http\Request as HttpRequest;
use Laminas\Http\Response as HttpResponse;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;

/**
* @covers \Laminas\Feed\Reader\Http\LaminasHttpClientDecorator
*/
class LaminasHttpClientDecoratorTest extends TestCase
{
use ProphecyTrait;
/**
* @var Client|mixed|\PHPUnit\Framework\MockObject\MockObject
*/
private $client;

protected function setUp(): void
{
$this->client = $this->prophesize(Client::class);
$this->client = $this->createMock(Client::class);
}

public function prepareDefaultClientInteractions($uri, ObjectProphecy $response)
public function prepareDefaultClientInteractions($uri, MockObject $response)
{
$this->client->resetParameters()->shouldBeCalled();
$this->client->setMethod('GET')->shouldBeCalled();
$this->client->setHeaders(Argument::type(Headers::class))->shouldBeCalled();
$this->client->setUri($uri)->shouldBeCalled();
$this->client->send()->will(function () use ($response) {
return $response->reveal();
});
$this->client
->expects($this->atLeastOnce())
->method('resetParameters');

$this->client
->expects($this->atLeastOnce())
->method('setMethod')
->with('GET');

$this->client
->expects($this->atLeastOnce())
->method('setHeaders')
->with($this->callback(static function ($parameter): bool {
self::assertInstanceOf(Headers::class, $parameter);
return true;
}));

$this->client
->expects($this->atLeastOnce())
->method('setUri')
->with($uri);

$this->client
->expects($this->once())
->method('send')
->willReturn($response);
}

public function createMockHttpResponse($statusCode, $body, Headers $headers = null)
{
$response = $this->prophesize(HttpResponse::class);
$response->getStatusCode()->willReturn($statusCode);
$response->getBody()->willReturn($body);
$response->getHeaders()->willReturn($headers ?: new Headers());
$response = $this->createMock(HttpResponse::class);
$response
->expects($this->any())
->method('getStatusCode')
->willReturn($statusCode);

$response
->expects($this->any())
->method('getBody')
->willReturn($body);
$response
->expects($this->any())
->method('getHeaders')
->willReturn($headers ?? new Headers());

return $response;
}

public function createMockHttpHeaders(array $headers)
/**
* @param array $headers
*
* @return MockObject<Headers>
*/
public function createMockHttpHeaders(array $headers): Headers
{
$mock = $this->prophesize(Headers::class);
$mock->toArray()->willReturn($headers);
$mock = $this->createMock(Headers::class);
$mock
->expects($this->any())
->method('toArray')
->willReturn($headers);

return $mock;
}

public function testProvidesAccessToDecoratedClient()
{
$client = $this->prophesize(Client::class)->reveal();
$client = $this->createMock(Client::class);
$decorator = new LaminasHttpClientDecorator($client);
$this->assertSame($client, $decorator->getDecoratedClient());
}

public function testDecoratorReturnsFeedResponse()
{
$headers = $this->createMockHttpHeaders(['Content-Type' => 'application/rss+xml']);
$httpResponse = $this->createMockHttpResponse(200, '', $headers->reveal());
$httpResponse = $this->createMockHttpResponse(200, '', $headers);
$this->prepareDefaultClientInteractions('http://example.com', $httpResponse);

$client = new LaminasHttpClientDecorator($this->client->reveal());
$client = new LaminasHttpClientDecorator($this->client);
$response = $client->get('http://example.com');

$this->assertInstanceOf(FeedResponse::class, $response);
Expand All @@ -88,16 +128,26 @@ public function testDecoratorInjectsProvidedHeadersIntoClientWhenSending()
'Content-Length' => 1234,
'X-Content-Length' => 1234.56,
]);
$httpResponse = $this->createMockHttpResponse(200, '', $responseHeaders->reveal());
$httpResponse = $this->createMockHttpResponse(200, '', $responseHeaders);
$this->prepareDefaultClientInteractions('http://example.com', $httpResponse);

$requestHeaders = $this->prophesize(Headers::class);
$requestHeaders->addHeaderLine('Accept', 'application/rss+xml')->shouldBeCalled();
$request = $this->prophesize(HttpRequest::class);
$request->getHeaders()->willReturn($requestHeaders->reveal());
$this->client->getRequest()->willReturn($request->reveal());
$requestHeaders = $this->createMock(Headers::class);
$requestHeaders
->expects($this->atLeastOnce())
->method('addHeaderLine')
->with('Accept', 'application/rss+xml');

$request = $this->createMock(HttpRequest::class);
$request
->expects($this->any())
->method('getHeaders')
->willReturn($requestHeaders);
$this->client
->expects($this->any())
->method('getRequest')
->willReturn($request);

$client = new LaminasHttpClientDecorator($this->client->reveal());
$client = new LaminasHttpClientDecorator($this->client);
$response = $client->get('http://example.com', ['Accept' => ['application/rss+xml']]);

$this->assertInstanceOf(FeedResponse::class, $response);
Expand Down Expand Up @@ -188,17 +238,38 @@ public function invalidHeaders()
public function testDecoratorRaisesExceptionForInvalidHeaders($headers, $contains)
{
$httpResponse = $this->createMockHttpResponse(200, '');
$this->client->resetParameters()->shouldBeCalled();
$this->client->setMethod('GET')->shouldBeCalled();
$this->client->setHeaders(Argument::type(Headers::class))->shouldBeCalled();
$this->client->setUri('http://example.com')->shouldBeCalled();
$this->client
->expects($this->atLeastOnce())
->method('resetParameters');
$this->client
->expects($this->atLeastOnce())
->method('setMethod')
->with('GET');
$this->client
->expects($this->atLeastOnce())
->method('setHeaders')
->with($this->callback(static function ($argument): bool {
self::assertInstanceOf(Headers::class, $argument);
return true;
}));

$this->client
->expects($this->atLeastOnce())
->method('setUri')
->with('http://example.com');

$requestHeaders = $this->prophesize(Headers::class);
$request = $this->prophesize(HttpRequest::class);
$request->getHeaders()->willReturn($requestHeaders->reveal());
$this->client->getRequest()->willReturn($request->reveal());
$requestHeaders = $this->createMock(Headers::class);
$request = $this->createMock(HttpRequest::class);
$request
->expects($this->any())
->method('getHeaders')
->willReturn($requestHeaders);
$this->client
->expects($this->any())
->method('getRequest')
->willReturn($request);

$client = new LaminasHttpClientDecorator($this->client->reveal());
$client = new LaminasHttpClientDecorator($this->client);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($contains);
Expand Down
76 changes: 47 additions & 29 deletions test/Reader/Http/Psr7ResponseDecoratorTest.php
Expand Up @@ -13,84 +13,102 @@
use Laminas\Feed\Reader\Http\ResponseInterface;
use LaminasTest\Feed\Reader\TestAsset\Psr7Stream;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;

/**
* @covers \Laminas\Feed\Reader\Http\Psr7ResponseDecorator
*/
class Psr7ResponseDecoratorTest extends TestCase
{
use ProphecyTrait;

public function testDecoratorIsAFeedResponse()
{
$originalResponse = $this->prophesize(Psr7ResponseInterface::class);
$decorator = new Psr7ResponseDecorator($originalResponse->reveal());
$originalResponse = $this->createMock(Psr7ResponseInterface::class);
$decorator = new Psr7ResponseDecorator($originalResponse);
$this->assertInstanceOf(ResponseInterface::class, $decorator);
}

public function testDecoratorIsAHeaderAwareResponse()
{
$originalResponse = $this->prophesize(Psr7ResponseInterface::class);
$decorator = new Psr7ResponseDecorator($originalResponse->reveal());
$originalResponse = $this->createMock(Psr7ResponseInterface::class);
$decorator = new Psr7ResponseDecorator($originalResponse);
$this->assertInstanceOf(HeaderAwareResponseInterface::class, $decorator);
}

public function testDecoratorIsNotAPsr7Response()
{
$originalResponse = $this->prophesize(Psr7ResponseInterface::class);
$decorator = new Psr7ResponseDecorator($originalResponse->reveal());
$originalResponse = $this->createMock(Psr7ResponseInterface::class);
$decorator = new Psr7ResponseDecorator($originalResponse);
$this->assertNotInstanceOf(Psr7ResponseInterface::class, $decorator);
}

public function testCanRetrieveDecoratedResponse()
{
$originalResponse = $this->prophesize(Psr7ResponseInterface::class);
$decorator = new Psr7ResponseDecorator($originalResponse->reveal());
$this->assertSame($originalResponse->reveal(), $decorator->getDecoratedResponse());
$originalResponse = $this->createMock(Psr7ResponseInterface::class);
$decorator = new Psr7ResponseDecorator($originalResponse);
$this->assertSame($originalResponse, $decorator->getDecoratedResponse());
}

public function testProxiesToDecoratedResponseToRetrieveStatusCode()
{
$originalResponse = $this->prophesize(Psr7ResponseInterface::class);
$originalResponse->getStatusCode()->willReturn(301);
$decorator = new Psr7ResponseDecorator($originalResponse->reveal());
$originalResponse = $this->createMock(Psr7ResponseInterface::class);
$originalResponse
->method('getStatusCode')
->willReturn(301);
$decorator = new Psr7ResponseDecorator($originalResponse);
$this->assertSame(301, $decorator->getStatusCode());
}

public function testProxiesToDecoratedResponseToRetrieveBody()
{
$originalResponse = $this->prophesize(Psr7ResponseInterface::class);
$originalResponse->getBody()->willReturn('BODY');
$decorator = new Psr7ResponseDecorator($originalResponse->reveal());
$originalResponse = $this->createMock(Psr7ResponseInterface::class);
$originalResponse
->method('getBody')
->willReturn('BODY');
$decorator = new Psr7ResponseDecorator($originalResponse);
$this->assertSame('BODY', $decorator->getBody());
}

public function testCastsStreamToStringWhenReturningPsr7Body()
{
$stream = new Psr7Stream('BODY');
$originalResponse = $this->prophesize(Psr7ResponseInterface::class);
$originalResponse->getBody()->willReturn($stream);
$decorator = new Psr7ResponseDecorator($originalResponse->reveal());
$originalResponse = $this->createMock(Psr7ResponseInterface::class);
$originalResponse
->method('getBody')
->willReturn($stream);
$decorator = new Psr7ResponseDecorator($originalResponse);
$this->assertSame('BODY', $decorator->getBody());
}

public function testProxiesToDecoratedResponseToRetrieveHeaderLine()
{
$originalResponse = $this->prophesize(Psr7ResponseInterface::class);
$originalResponse->hasHeader('E-Tag')->willReturn(true);
$originalResponse->getHeaderLine('E-Tag')->willReturn('2015-11-17 12:32:00-06:00');
$decorator = new Psr7ResponseDecorator($originalResponse->reveal());
$originalResponse = $this->createMock(Psr7ResponseInterface::class);
$originalResponse
->method('hasHeader')
->with('E-Tag')
->willReturn(true);

$originalResponse
->method('getHeaderLine')
->with('E-Tag')
->willReturn('2015-11-17 12:32:00-06:00');
$decorator = new Psr7ResponseDecorator($originalResponse);
$this->assertSame('2015-11-17 12:32:00-06:00', $decorator->getHeaderLine('E-Tag'));
}

public function testDecoratorReturnsDefaultValueWhenOriginalResponseDoesNotHaveHeader()
{
$originalResponse = $this->prophesize(Psr7ResponseInterface::class);
$originalResponse->hasHeader('E-Tag')->willReturn(false);
$originalResponse->getHeaderLine('E-Tag')->shouldNotBeCalled();
$decorator = new Psr7ResponseDecorator($originalResponse->reveal());
$originalResponse = $this->createMock(Psr7ResponseInterface::class);
$originalResponse
->method('hasHeader')
->with('E-Tag')
->willReturn(false);

$originalResponse
->expects($this->never())
->method('getHeaderLine')
->with('E-Tag');

$decorator = new Psr7ResponseDecorator($originalResponse);
$this->assertSame('2015-11-17 12:32:00-06:00', $decorator->getHeaderLine('E-Tag', '2015-11-17 12:32:00-06:00'));
}
}
11 changes: 3 additions & 8 deletions test/Reader/StandaloneExtensionManagerTest.php
Expand Up @@ -13,11 +13,9 @@
use Laminas\Feed\Reader\ExtensionManagerInterface;
use Laminas\Feed\Reader\StandaloneExtensionManager;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class StandaloneExtensionManagerTest extends TestCase
{
use ProphecyTrait;

/**
* @var StandaloneExtensionManager
Expand Down Expand Up @@ -85,11 +83,9 @@ public function testEachPluginRetrievalReturnsNewInstance($pluginName, $pluginCl

public function testAddAcceptsValidExtensionClasses()
{
$ext = $this->prophesize(Extension\AbstractEntry::class)->reveal();
$this->extensions->add('Test/Entry', get_class($ext));
$this->extensions->add('Test/Entry', Extension\AbstractEntry::class);
$this->assertTrue($this->extensions->has('Test/Entry'));
$ext = $this->prophesize(Extension\AbstractFeed::class)->reveal();
$this->extensions->add('Test/Feed', get_class($ext));
$this->extensions->add('Test/Feed', Extension\AbstractFeed::class);
$this->assertTrue($this->extensions->has('Test/Feed'));
}

Expand All @@ -101,8 +97,7 @@ public function testAddRejectsInvalidExtensions()

public function testExtensionRemoval()
{
$ext = $this->prophesize(Extension\AbstractEntry::class)->reveal();
$this->extensions->add('Test/Entry', get_class($ext));
$this->extensions->add('Test/Entry', Extension\AbstractEntry::class);
$this->assertTrue($this->extensions->has('Test/Entry'));
$this->extensions->remove('Test/Entry');
$this->assertFalse($this->extensions->has('Test/Entry'));
Expand Down

0 comments on commit bfd0439

Please sign in to comment.