Skip to content

Commit

Permalink
Use withConsecutive/willReturnOnConsecutiveCalls instead of the depre…
Browse files Browse the repository at this point in the history
…cated at matcher
  • Loading branch information
christeredvartsen committed Aug 9, 2020
1 parent d54e5a2 commit 74cd07f
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 293 deletions.
8 changes: 7 additions & 1 deletion src/CliCommand/AddPublicKey.php
Expand Up @@ -43,7 +43,7 @@ public function __construct() {
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
protected function execute(InputInterface $input, OutputInterface $output) : int {
$adapter = $this->getAclAdapter();
$publicKey = $input->getArgument('publicKey');

Expand Down Expand Up @@ -82,6 +82,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln('Public key: ' . $publicKey);
$output->writeln('Private key: ' . $privateKey);
$output->writeln('ACL rules added: ' . count($aclRules));

return self::SUCCESS;
}

/**
Expand All @@ -105,6 +107,7 @@ private function askForResources(InputInterface $input, OutputInterface $output)
);

$type = $this->getHelper('question')->ask($input, $output, $question);

switch ($type) {
case self::RESOURCES_READ_ONLY:
return Resource::getReadOnlyResources();
Expand Down Expand Up @@ -163,6 +166,7 @@ private function askForCustomResources(InputInterface $input, OutputInterface $o

return $resources;
});
$question->setMaxAttempts(1);

return $this->getHelper('question')->ask($input, $output, $question);
}
Expand Down Expand Up @@ -190,6 +194,7 @@ private function askForUsers(InputInterface $input, OutputInterface $output) {

return array_search('*', $users) === false ? $users : '*';
});
$question->setMaxAttempts(1);

return $this->getHelper('question')->ask($input, $output, $question);
}
Expand Down Expand Up @@ -222,6 +227,7 @@ private function askForPrivateKey(InputInterface $input, OutputInterface $output
'What do you want the private key to be (leave blank to generate)',
$privateKeyGenerator->generate()
);
$question->setMaxAttempts(1);

return $this->getHelper('question')->ask($input, $output, $question);
}
Expand Down
3 changes: 2 additions & 1 deletion src/CliCommand/GeneratePrivateKey.php
Expand Up @@ -27,8 +27,9 @@ public function __construct() {
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
protected function execute(InputInterface $input, OutputInterface $output) : int {
$output->writeln($this->generate());
return self::SUCCESS;
}

/**
Expand Down
17 changes: 8 additions & 9 deletions tests/Behat/FeatureContextTest.php
Expand Up @@ -76,16 +76,15 @@ public function testCanSetAnApiClient() : void {

$client = $this->createMock(ClientInterface::class);
$client
->expects($this->at(0))
->method('getConfig')
->with('handler')
->willReturn($handlerStack);

$client
->expects($this->at(1))
->method('getConfig')
->with('base_uri')
->willReturn('http://localhost:8080');
->withConsecutive(
['handler'],
['base_uri'],
)
->willReturnOnConsecutiveCalls(
$handlerStack,
'http://localhost:8080',
);

$context = new FeatureContext();
$this->assertSame($context, $context->setClient($client));
Expand Down
5 changes: 0 additions & 5 deletions tests/CliCommand/AddPublicKeyTest.php
Expand Up @@ -104,8 +104,6 @@ public function testWillNotAcceptEmptyUserSpecification() : void {
$commandTester->setInputs([
'0',
'',
'*',
'n',
]);

$this->expectException(RuntimeException::class);
Expand All @@ -122,9 +120,6 @@ public function testWillNotAcceptEmptyCustomResourceSpecification() : void {
$commandTester->setInputs([
'4',
'',
'foo.bar,bar.foo',
'*',
'n'
]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/must specify at least one resource/');
Expand Down
181 changes: 78 additions & 103 deletions tests/EventListener/AuthenticateTest.php
Expand Up @@ -62,13 +62,13 @@ protected function getEventMock($config = null) : Event {
*/
public function testThrowsExceptionWhenAuthInfoIsMissing() : void {
$this->headers
->expects($this->at(0))
->expects($this->once())
->method('has')
->with('x-imbo-authenticate-timestamp')
->willReturn(false);

$this->headers
->expects($this->at(1))
->expects($this->once())
->method('get')
->with('x-imbo-authenticate-timestamp')
->willReturn(null);
Expand All @@ -82,22 +82,23 @@ public function testThrowsExceptionWhenAuthInfoIsMissing() : void {
*/
public function testThrowsExceptionWhenSignatureIsMissing() : void {
$this->headers
->expects($this->at(0))
->method('has')
->with('x-imbo-authenticate-timestamp')
->willReturn(true);

$this->headers
->expects($this->at(1))
->method('has')
->with('x-imbo-authenticate-signature')
->withConsecutive(
['x-imbo-authenticate-timestamp'],
['x-imbo-authenticate-signature'],
)
->willReturn(true);

$this->headers
->expects($this->at(2))
->method('get')
->with('x-imbo-authenticate-timestamp')
->willReturn(gmdate('Y-m-d\TH:i:s\Z'));
->withConsecutive(
['x-imbo-authenticate-timestamp', null],
['x-imbo-authenticate-signature', null],
)
->willReturnOnConsecutiveCalls(
gmdate('Y-m-d\TH:i:s\Z'),
null,
);

$this->expectExceptionObject(new RuntimeException('Missing authentication signature', 400));
$this->listener->authenticate($this->event);
Expand All @@ -109,19 +110,14 @@ public function testThrowsExceptionWhenSignatureIsMissing() : void {
*/
public function testThrowsExceptionWhenTimestampIsInvalid() : void {
$this->headers
->expects($this->at(0))
->method('has')
->with('x-imbo-authenticate-timestamp')
->withConsecutive(
['x-imbo-authenticate-timestamp'],
['x-imbo-authenticate-signature'],
)
->willReturn(true);

$this->headers
->expects($this->at(1))
->method('has')
->with('x-imbo-authenticate-signature')
->willReturn(true);

$this->headers
->expects($this->at(2))
->method('get')
->with('x-imbo-authenticate-timestamp')
->willReturn('some string');
Expand All @@ -136,19 +132,14 @@ public function testThrowsExceptionWhenTimestampIsInvalid() : void {
*/
public function testThrowsExceptionWhenTimestampHasExpired() : void {
$this->headers
->expects($this->at(0))
->method('has')
->with('x-imbo-authenticate-timestamp')
->withConsecutive(
['x-imbo-authenticate-timestamp'],
['x-imbo-authenticate-signature'],
)
->willReturn(true);

$this->headers
->expects($this->at(1))
->method('has')
->with('x-imbo-authenticate-signature')
->willReturn(true);

$this->headers
->expects($this->at(2))
->method('get')
->with('x-imbo-authenticate-timestamp')
->willReturn('2010-07-10T20:02:10Z');
Expand All @@ -162,28 +153,23 @@ public function testThrowsExceptionWhenTimestampHasExpired() : void {
*/
public function testThrowsExceptionWhenSignatureDoesNotMatch() : void {
$this->headers
->expects($this->at(0))
->method('has')
->with('x-imbo-authenticate-timestamp')
->willReturn(true);

$this->headers
->expects($this->at(1))
->method('has')
->with('x-imbo-authenticate-signature')
->withConsecutive(
['x-imbo-authenticate-timestamp'],
['x-imbo-authenticate-signature'],
)
->willReturn(true);

$this->headers
->expects($this->at(2))
->method('get')
->with('x-imbo-authenticate-timestamp')
->willReturn(gmdate('Y-m-d\TH:i:s\Z'));

$this->headers
->expects($this->at(3))
->method('get')
->with('x-imbo-authenticate-signature')
->willReturn('foobar');
->withConsecutive(
['x-imbo-authenticate-timestamp'],
['x-imbo-authenticate-signature'],
)
->willReturnOnConsecutiveCalls(
gmdate('Y-m-d\TH:i:s\Z'),
'foobar',
);

$this->request
->expects($this->once())
Expand Down Expand Up @@ -221,28 +207,23 @@ public function testApprovesValidSignature() : void {
->willReturn($privateKey);

$this->headers
->expects($this->at(0))
->method('has')
->with('x-imbo-authenticate-timestamp')
->willReturn(true);

$this->headers
->expects($this->at(1))
->method('has')
->with('x-imbo-authenticate-signature')
->withConsecutive(
['x-imbo-authenticate-timestamp'],
['x-imbo-authenticate-signature'],
)
->willReturn(true);

$this->headers
->expects($this->at(2))
->method('get')
->with('x-imbo-authenticate-timestamp')
->willReturn($timestamp);

$this->headers
->expects($this->at(3))
->method('get')
->with('x-imbo-authenticate-signature')
->willReturn($signature);
->withConsecutive(
['x-imbo-authenticate-timestamp'],
['x-imbo-authenticate-signature'],
)
->willReturnOnConsecutiveCalls(
$timestamp,
$signature,
);

$this->request
->expects($this->once())
Expand Down Expand Up @@ -292,34 +273,31 @@ public function testApprovesValidSignatureWithAuthInfoFromQueryParameters() : vo
->willReturn($privateKey);

$this->headers
->expects($this->at(0))
->method('has')
->with('x-imbo-authenticate-timestamp')
->willReturn(false);

$this->headers
->expects($this->at(1))
->method('get')
->with('x-imbo-authenticate-timestamp', $timestamp)
->willReturn($timestamp);

$this->headers
->expects($this->at(2))
->method('get')
->with('x-imbo-authenticate-signature', $signature)
->willReturn($signature);

$this->query
->expects($this->at(0))
->method('get')
->with('timestamp')
->willReturn($timestamp);
->withConsecutive(
['x-imbo-authenticate-timestamp', $timestamp],
['x-imbo-authenticate-signature', $signature],
)
->willReturnOnConsecutiveCalls(
$timestamp,
$signature,
);

$this->query
->expects($this->at(1))
->method('get')
->with('signature')
->willReturn($signature);
->withConsecutive(
['timestamp'],
['signature'],
)
->willReturnOnConsecutiveCalls(
$timestamp,
$signature,
);

$this->request
->expects($this->once())
Expand Down Expand Up @@ -463,34 +441,31 @@ public function testApprovesSignaturesWhenConfigurationForcesProtocol(string $se
->willReturn('key');

$this->headers
->expects($this->at(0))
->method('has')
->with('x-imbo-authenticate-timestamp')
->willReturn(false);

$this->headers
->expects($this->at(1))
->method('get')
->with('x-imbo-authenticate-timestamp', $timestamp)
->willReturn($timestamp);

$this->headers
->expects($this->at(2))
->method('get')
->with('x-imbo-authenticate-signature', $signature)
->willReturn($signature);

$this->query
->expects($this->at(0))
->method('get')
->with('timestamp')
->willReturn($timestamp);
->withConsecutive(
['x-imbo-authenticate-timestamp', $timestamp],
['x-imbo-authenticate-signature', $signature],
)
->willReturnOnConsecutiveCalls(
$timestamp,
$signature,
);

$this->query
->expects($this->at(1))
->method('get')
->with('signature')
->willReturn($signature);
->withConsecutive(
['timestamp'],
['signature'],
)
->willReturnOnConsecutiveCalls(
$timestamp,
$signature,
);

$this->request
->expects($this->once())
Expand Down

0 comments on commit 74cd07f

Please sign in to comment.