Skip to content

Commit

Permalink
fix: Upgrade unit tests for PHP 7.4 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpedrie committed Oct 22, 2019
1 parent cd3c379 commit 5c9486c
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 356 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ matrix:
include:
- php: 5.4
env: COMPOSER_CMD="composer update phpseclib/phpseclib guzzlehttp/guzzle guzzlehttp/psr7 --prefer-lowest" RUN_PHP_CS=true
allow_failures:
- php: 7.4snapshot

before_install:
- composer self-update
Expand All @@ -42,10 +40,6 @@ install:
- if [[ "$TRAVIS_PHP_VERSION" == "5.4" ]]; then composer remove --dev cache/filesystem-adapter; fi
- $(echo $COMPOSER_CMD)

before_script:
- phpenv version-name | grep ^5.[34] && echo "extension=apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true
- phpenv version-name | grep ^5.[34] && echo "apc.enable_cli=1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true

script:
- vendor/bin/phpunit
- if [[ "$RUN_PHP_CS" == "true" ]]; then vendor/bin/phpcs src -np; fi
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"guzzlehttp/psr7": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "~4.8.36",
"phpunit/phpunit": "^4.8|^5.0",
"squizlabs/php_codesniffer": "~2.3",
"symfony/dom-crawler": "~2.1",
"symfony/css-selector": "~2.1",
Expand Down
14 changes: 14 additions & 0 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,18 @@ public function onlyGuzzle5()
$this->markTestSkipped('Guzzle 5 only');
}
}

protected function getGuzzle5ResponseMock()
{
$response = $this->prophesize('GuzzleHttp\Message\ResponseInterface');
$response->getStatusCode()
->willReturn(200);

$response->getHeaders()->willReturn([]);
$response->getBody()->willReturn('');
$response->getProtocolVersion()->willReturn('');
$response->getReasonPhrase()->willReturn('');

return $response;
}
}
255 changes: 130 additions & 125 deletions tests/Google/AccessToken/RevokeTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use GuzzleHttp\Client;
use Prophecy\Argument;

/**
* Licensed to the Apache Software Foundation (ASF) under one
Expand All @@ -23,128 +23,133 @@

class Google_AccessToken_RevokeTest extends BaseTest
{
public function testRevokeAccessGuzzle5()
{
$this->onlyGuzzle5();

$accessToken = 'ACCESS_TOKEN';
$refreshToken = 'REFRESH_TOKEN';
$token = '';

$response = $this->getMock('GuzzleHttp\Message\ResponseInterface');
$response->expects($this->exactly(3))
->method('getStatusCode')
->will($this->returnValue(200));
$http = $this->getMock('GuzzleHttp\ClientInterface');
$http->expects($this->exactly(3))
->method('send')
->will($this->returnCallback(
function ($request) use (&$token, $response) {
parse_str((string) $request->getBody(), $fields);
$token = isset($fields['token']) ? $fields['token'] : null;

return $response;
}
));

$requestToken = null;
$request = $this->getMock('GuzzleHttp\Message\RequestInterface');
$request->expects($this->exactly(3))
->method('getBody')
->will($this->returnCallback(
function () use (&$requestToken) {
return 'token='.$requestToken;
}));
$http->expects($this->exactly(3))
->method('createRequest')
->will($this->returnCallback(
function ($method, $url, $params) use (&$requestToken, $request) {
parse_str((string) $params['body'], $fields);
$requestToken = isset($fields['token']) ? $fields['token'] : null;

return $request;
}
));

$t = array(
'access_token' => $accessToken,
'created' => time(),
'expires_in' => '3600'
);

// Test with access token.
$revoke = new Google_AccessToken_Revoke($http);
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);

// Test with refresh token.
$revoke = new Google_AccessToken_Revoke($http);
$t = array(
'access_token' => $accessToken,
'refresh_token' => $refreshToken,
'created' => time(),
'expires_in' => '3600'
);
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($refreshToken, $token);

// Test with token string.
$revoke = new Google_AccessToken_Revoke($http);
$t = $accessToken;
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);
}

public function testRevokeAccessGuzzle6()
{
$this->onlyGuzzle6();

$accessToken = 'ACCESS_TOKEN';
$refreshToken = 'REFRESH_TOKEN';
$token = '';

$response = $this->getMock('Psr\Http\Message\ResponseInterface');
$response->expects($this->exactly(3))
->method('getStatusCode')
->will($this->returnValue(200));
$http = $this->getMock('GuzzleHttp\ClientInterface');
$http->expects($this->exactly(3))
->method('send')
->will($this->returnCallback(
function ($request) use (&$token, $response) {
parse_str((string) $request->getBody(), $fields);
$token = isset($fields['token']) ? $fields['token'] : null;

return $response;
}
));

$t = array(
'access_token' => $accessToken,
'created' => time(),
'expires_in' => '3600'
);

// Test with access token.
$revoke = new Google_AccessToken_Revoke($http);
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);

// Test with refresh token.
$revoke = new Google_AccessToken_Revoke($http);
$t = array(
'access_token' => $accessToken,
'refresh_token' => $refreshToken,
'created' => time(),
'expires_in' => '3600'
);
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($refreshToken, $token);

// Test with token string.
$revoke = new Google_AccessToken_Revoke($http);
$t = $accessToken;
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);
}
public function testRevokeAccessGuzzle5()
{
$this->onlyGuzzle5();

$accessToken = 'ACCESS_TOKEN';
$refreshToken = 'REFRESH_TOKEN';
$token = '';

$response = $this->prophesize('GuzzleHttp\Message\ResponseInterface');
$response->getStatusCode()
->shouldBeCalledTimes(3)
->willReturn(200);

$response->getHeaders()->willReturn([]);
$response->getBody()->willReturn('');
$response->getProtocolVersion()->willReturn('');
$response->getReasonPhrase()->willReturn('');

$http = $this->prophesize('GuzzleHttp\ClientInterface');
$http->send(Argument::type('GuzzleHttp\Message\RequestInterface'))
->shouldBeCalledTimes(3)
->will(function ($args) use (&$token, $response) {
$request = $args[0];
parse_str((string) $request->getBody(), $fields);
$token = isset($fields['token']) ? $fields['token'] : null;

return $response->reveal();
});

$requestToken = null;
$request = $this->prophesize('GuzzleHttp\Message\RequestInterface');
$request->getBody()
->shouldBeCalledTimes(3)
->will(function () use (&$requestToken) {
return 'token='.$requestToken;
});

$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->shouldBeCalledTimes(3)
->will(function ($args) use (&$requestToken, $request) {
$params = $args[2];
parse_str((string) $params['body'], $fields);
$requestToken = isset($fields['token']) ? $fields['token'] : null;

return $request;
});

$t = [
'access_token' => $accessToken,
'created' => time(),
'expires_in' => '3600'
];

// Test with access token.
$revoke = new Google_AccessToken_Revoke($http->reveal());
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);

// Test with refresh token.
$revoke = new Google_AccessToken_Revoke($http->reveal());
$t = [
'access_token' => $accessToken,
'refresh_token' => $refreshToken,
'created' => time(),
'expires_in' => '3600'
];

$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($refreshToken, $token);

// Test with token string.
$revoke = new Google_AccessToken_Revoke($http->reveal());
$t = $accessToken;
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);
}

public function testRevokeAccessGuzzle6()
{
$this->onlyGuzzle6();

$accessToken = 'ACCESS_TOKEN';
$refreshToken = 'REFRESH_TOKEN';
$token = '';

$response = $this->prophesize('Psr\Http\Message\ResponseInterface');
$response->getStatusCode()
->shouldBeCalledTimes(3)
->willReturn(200);

$http = $this->prophesize('GuzzleHttp\ClientInterface');
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(3)
->will(function ($args) use (&$token, $response) {
parse_str((string) $args[0]->getBody(), $fields);
$token = isset($fields['token']) ? $fields['token'] : null;

return $response->reveal();
});

$t = [
'access_token' => $accessToken,
'created' => time(),
'expires_in' => '3600'
];

// Test with access token.
$revoke = new Google_AccessToken_Revoke($http->reveal());
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);

// Test with refresh token.
$revoke = new Google_AccessToken_Revoke($http->reveal());
$t = [
'access_token' => $accessToken,
'refresh_token' => $refreshToken,
'created' => time(),
'expires_in' => '3600'
];

$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($refreshToken, $token);

// Test with token string.
$revoke = new Google_AccessToken_Revoke($http->reveal());
$t = $accessToken;
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);
}
}
2 changes: 0 additions & 2 deletions tests/Google/AccessToken/VerifyTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use GuzzleHttp\Client;

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand Down
Loading

0 comments on commit 5c9486c

Please sign in to comment.