diff --git a/src/CliCommand/AddPublicKey.php b/src/CliCommand/AddPublicKey.php index a2a0a9791..95240d665 100644 --- a/src/CliCommand/AddPublicKey.php +++ b/src/CliCommand/AddPublicKey.php @@ -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'); @@ -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; } /** @@ -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(); @@ -163,6 +166,7 @@ private function askForCustomResources(InputInterface $input, OutputInterface $o return $resources; }); + $question->setMaxAttempts(1); return $this->getHelper('question')->ask($input, $output, $question); } @@ -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); } @@ -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); } diff --git a/src/CliCommand/GeneratePrivateKey.php b/src/CliCommand/GeneratePrivateKey.php index 69154e8e1..63b2584ce 100644 --- a/src/CliCommand/GeneratePrivateKey.php +++ b/src/CliCommand/GeneratePrivateKey.php @@ -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; } /** diff --git a/tests/Behat/FeatureContextTest.php b/tests/Behat/FeatureContextTest.php index 0200ac759..2e4a8b9f5 100644 --- a/tests/Behat/FeatureContextTest.php +++ b/tests/Behat/FeatureContextTest.php @@ -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)); diff --git a/tests/CliCommand/AddPublicKeyTest.php b/tests/CliCommand/AddPublicKeyTest.php index 84d429520..0aa6d1172 100644 --- a/tests/CliCommand/AddPublicKeyTest.php +++ b/tests/CliCommand/AddPublicKeyTest.php @@ -104,8 +104,6 @@ public function testWillNotAcceptEmptyUserSpecification() : void { $commandTester->setInputs([ '0', '', - '*', - 'n', ]); $this->expectException(RuntimeException::class); @@ -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/'); diff --git a/tests/EventListener/AuthenticateTest.php b/tests/EventListener/AuthenticateTest.php index 795bf7139..9050f7d94 100644 --- a/tests/EventListener/AuthenticateTest.php +++ b/tests/EventListener/AuthenticateTest.php @@ -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); @@ -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); @@ -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'); @@ -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'); @@ -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()) @@ -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()) @@ -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()) @@ -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()) diff --git a/tests/EventListener/DatabaseOperationsTest.php b/tests/EventListener/DatabaseOperationsTest.php index b0d515408..cae866e65 100644 --- a/tests/EventListener/DatabaseOperationsTest.php +++ b/tests/EventListener/DatabaseOperationsTest.php @@ -228,106 +228,41 @@ public function testCanLoadImages() : void { $query = $this->createMock(ParameterBag::class); $query - ->expects($this->at(0)) ->method('has') - ->with('page') + ->withConsecutive( + ['page'], + ['limit'], + ['metadata'], + ['from'], + ['to'], + ['sort'], + ['ids'], + ['checksums'], + ['originalChecksums'], + ) ->willReturn(true); - - $query - ->expects($this->at(1)) - ->method('get') - ->with('page') - ->willReturn(1); - - $query - ->expects($this->at(2)) - ->method('has') - ->with('limit') - ->willReturn(true); - - $query - ->expects($this->at(3)) - ->method('get') - ->with('limit') - ->willReturn(5); - - $query - ->expects($this->at(4)) - ->method('has') - ->with('metadata') - ->willReturn(true); - - $query - ->expects($this->at(5)) - ->method('has') - ->with('from') - ->willReturn(true); - - $query - ->expects($this->at(6)) - ->method('get') - ->with('from') - ->willReturn(1355156488); - - $query - ->expects($this->at(7)) - ->method('has') - ->with('to') - ->willReturn(true); - - $query - ->expects($this->at(8)) - ->method('get') - ->with('to') - ->willReturn(1355176488); - - $query - ->expects($this->at(9)) - ->method('has') - ->with('sort') - ->willReturn(true); - - $query - ->expects($this->at(10)) - ->method('get') - ->with('sort') - ->willReturn(['size:desc']); - - $query - ->expects($this->at(11)) - ->method('has') - ->with('ids') - ->willReturn(true); - - $query - ->expects($this->at(12)) - ->method('get') - ->with('ids') - ->willReturn(['identifier1', 'identifier2', 'identifier3']); - $query - ->expects($this->at(13)) - ->method('has') - ->with('checksums') - ->willReturn(true); - - $query - ->expects($this->at(14)) ->method('get') - ->with('checksums') - ->willReturn(['checksum1', 'checksum2', 'checksum3']); - - $query - ->expects($this->at(15)) - ->method('has') - ->with('originalChecksums') - ->willReturn(true); - - $query - ->expects($this->at(16)) - ->method('get') - ->with('originalChecksums') - ->willReturn(['checksum1', 'checksum2', 'checksum3']); + ->withConsecutive( + ['page'], + ['limit'], + ['from'], + ['to'], + ['sort'], + ['ids'], + ['checksums'], + ['originalChecksums'], + ) + ->willReturnOnConsecutiveCalls( + 1, + 5, + 1355156488, + 1355176488, + ['size:desc'], + ['identifier1', 'identifier2', 'identifier3'], + ['checksum1', 'checksum2', 'checksum3'], + ['checksum1', 'checksum2', 'checksum3'], + ); $this->request->query = $query; @@ -397,20 +332,13 @@ public function testCanLoadUser() : void { */ public function testCanLoadStats() : void { $this->database - ->expects($this->at(0)) ->method('getNumImages') - ->willReturn(1); + ->willReturnOnConsecutiveCalls(1, 2); $this->database - ->expects($this->at(1)) ->method('getNumBytes') ->willReturn(1); - $this->database - ->expects($this->at(2)) - ->method('getNumImages') - ->willReturn(2); - $this->response ->expects($this->once()) ->method('setModel') diff --git a/tests/Http/Response/ResponseFormatterTest.php b/tests/Http/Response/ResponseFormatterTest.php index 0f649e6ec..0565d4185 100644 --- a/tests/Http/Response/ResponseFormatterTest.php +++ b/tests/Http/Response/ResponseFormatterTest.php @@ -547,7 +547,6 @@ public function testTriggersAConversionTransformationIfNeededWhenTheModelIsAnIma $eventManager = $this->createMock(EventManager::class); $eventManager - ->expects($this->at(0)) ->method('trigger') ->with('image.transformed', ['image' => $image]); diff --git a/tests/Resource/GlobalShortUrlTest.php b/tests/Resource/GlobalShortUrlTest.php index ec3745968..36230e7eb 100644 --- a/tests/Resource/GlobalShortUrlTest.php +++ b/tests/Resource/GlobalShortUrlTest.php @@ -58,25 +58,17 @@ public function testCanTriggerAnImageGetEventWhenRequestedWithAValidShortUrl() : $route = $this->createMock(Route::class); $route - ->expects($this->at(0)) ->method('get') ->with('shortUrlId') ->willReturn($id); $route - ->expects($this->at(1)) ->method('set') - ->with('user', $user); - - $route - ->expects($this->at(2)) - ->method('set') - ->with('imageIdentifier', $imageIdentifier); - - $route - ->expects($this->at(3)) - ->method('set') - ->with('extension', $extension); + ->withConsecutive( + ['user', $user], + ['imageIdentifier', $imageIdentifier], + ['extension', $extension], + ); $this->request ->expects($this->once()) diff --git a/tests/Resource/ImageTest.php b/tests/Resource/ImageTest.php index 6a12ac3af..2709caa96 100644 --- a/tests/Resource/ImageTest.php +++ b/tests/Resource/ImageTest.php @@ -49,13 +49,11 @@ public function setUp() : void { */ public function testSupportsHttpDelete() : void { $this->manager - ->expects($this->at(0)) ->method('trigger') - ->with('db.image.delete'); - $this->manager - ->expects($this->at(1)) - ->method('trigger') - ->with('storage.image.delete'); + ->withConsecutive( + ['db.image.delete'], + ['storage.image.delete'], + ); $this->request ->expects($this->once()) @@ -96,13 +94,11 @@ public function testSupportsHttpGet() : void { ->with($this->isInstanceOf(ImageModel::class)); $this->manager - ->expects($this->at(0)) - ->method('trigger') - ->with('db.image.load'); - $this->manager - ->expects($this->at(1)) ->method('trigger') - ->with('storage.image.load'); + ->withConsecutive( + ['db.image.load'], + ['storage.image.load'], + ); $this->response ->expects($this->once()) diff --git a/tests/Resource/ImagesTest.php b/tests/Resource/ImagesTest.php index 86f9aa6c5..09301a981 100644 --- a/tests/Resource/ImagesTest.php +++ b/tests/Resource/ImagesTest.php @@ -13,6 +13,7 @@ use Imbo\Model\Image; use Imbo\Storage\StorageInterface; use Symfony\Component\HttpFoundation\ResponseHeaderBag; +use PHPUnit\Framework\MockObject\Stub\Exception as ExceptionStub; /** * @coversDefaultClass Imbo\Resource\Images @@ -67,13 +68,11 @@ public function testSupportsHttpPost() : void { ->willReturn('id'); $this->manager - ->expects($this->at(0)) ->method('trigger') - ->with('db.image.insert', ['updateIfDuplicate' => false]); - $this->manager - ->expects($this->at(1)) - ->method('trigger') - ->with('storage.image.insert'); + ->withConsecutive( + ['db.image.insert', ['updateIfDuplicate' => false]], + ['storage.image.insert'], + ); $image = $this->createMock(Image::class); $image @@ -147,22 +146,19 @@ public function testSupportsHttpGet() : void { */ public function testAddImageWithCallableImageIdentifierGenerator() : void { $this->manager - ->expects($this->at(0)) - ->method('trigger') - ->with('db.image.insert', ['updateIfDuplicate' => true]) - ->willThrowException(new DuplicateImageIdentifierException()); - $this->manager - ->expects($this->at(1)) - ->method('trigger') - ->with('db.image.insert', ['updateIfDuplicate' => true]); - $this->manager - ->expects($this->at(2)) - ->method('trigger') - ->with('db.image.insert', ['updateIfDuplicate' => true]); - $this->manager - ->expects($this->at(3)) ->method('trigger') - ->with('storage.image.insert'); + ->withConsecutive( + ['db.image.insert', ['updateIfDuplicate' => true]], + ['db.image.insert', ['updateIfDuplicate' => true]], + ['db.image.insert', ['updateIfDuplicate' => true]], + ['storage.image.insert'], + ) + ->willReturnOnConsecutiveCalls( + new ExceptionStub(new DuplicateImageIdentifierException()), + null, + null, + null, + ); $image = $this->createConfiguredMock(Image::class, [ 'getImageIdentifier' => 'some id', diff --git a/tests/Resource/MetadataTest.php b/tests/Resource/MetadataTest.php index d570d523f..6d8eb3c23 100644 --- a/tests/Resource/MetadataTest.php +++ b/tests/Resource/MetadataTest.php @@ -70,14 +70,11 @@ public function testSupportsHttpPut() : void { ->method('getContent') ->willReturn('{"foo":"bar"}'); $this->manager - ->expects($this->at(0)) ->method('trigger') - ->with('db.metadata.delete') - ->willReturnSelf(); - $this->manager - ->expects($this->at(1)) - ->method('trigger') - ->with('db.metadata.update', ['metadata' => $metadata]) + ->withConsecutive( + ['db.metadata.delete'], + ['db.metadata.update', ['metadata' => $metadata]], + ) ->willReturnSelf(); $this->response ->expects($this->once()) diff --git a/tests/Resource/ShortUrlsTest.php b/tests/Resource/ShortUrlsTest.php index d538e2334..7e0f05dec 100755 --- a/tests/Resource/ShortUrlsTest.php +++ b/tests/Resource/ShortUrlsTest.php @@ -258,32 +258,26 @@ public function testWillGenerateANewIdIfTheGeneratedOneExists() : void { ->willReturn('{"user": "user", "imageIdentifier": "id"}'); $this->database - ->expects($this->at(0)) ->method('imageExists') ->with('user', 'id') ->willReturn(true); $this->database - ->expects($this->at(1)) ->method('getShortUrlId') ->with('user', 'id', null, []) ->willReturn(null); $this->database - ->expects($this->at(2)) ->method('getShortUrlParams') - ->with($this->matchesRegularExpression('/[a-zA-Z0-9]{7}/')) - ->willReturn(['user' => 'value']); - $this->database - ->expects($this->at(3)) - ->method('getShortUrlParams') - ->with($this->matchesRegularExpression('/[a-zA-Z0-9]{7}/')) - ->willReturn(['user' => 'value']); - $this->database - ->expects($this->at(4)) - ->method('getShortUrlParams') - ->with($this->matchesRegularExpression('/[a-zA-Z0-9]{7}/')) - ->willReturn(null); + ->withConsecutive( + [$this->matchesRegularExpression('/[a-zA-Z0-9]{7}/')], + [$this->matchesRegularExpression('/[a-zA-Z0-9]{7}/')], + [$this->matchesRegularExpression('/[a-zA-Z0-9]{7}/')], + ) + ->willReturnOnConsecutiveCalls( + ['user' => 'value'], + ['user' => 'value'], + null, + ); $this->database - ->expects($this->at(5)) ->method('insertShortUrl') ->with($this->matchesRegularExpression('/[a-zA-Z0-9]{7}/'), 'user', 'id', null, []);