Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Upgrade unit tests for PHP 7.4 compat #1710

Merged
merged 3 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"phpunit/phpunit": "^4.8|^5.0",
"phpunit/phpunit": "^4.8.36|^5.6",

We need a PHPUnit version that already has the namespaced TestCase class alias.

"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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you fix the spaces here but not in the other tests? Is it because you realized that arduous task would crush your soul?

FWIW I think this is great, and if we want we can have a follow-up PR for just converting 2 spaces to 4 spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's a fairly good description of what happened.


$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