From be0547bb96d21b0e715850b8ba635461b8e38acc Mon Sep 17 00:00:00 2001 From: Rafael Dohms Date: Mon, 10 Oct 2022 11:11:06 +0200 Subject: [PATCH] Apply Doctrine Coding Standard and fix Fixes all current files and applies the standard. --- .gitignore | 1 + composer.json | 11 +- composer.lock | 297 +++++++++++++++++- phpcs.xml.dist | 18 ++ src/Api/JoindInClient.php | 30 +- src/Api/MeetupClient.php | 13 +- src/Api/Middleware/DefaultStackFactory.php | 7 +- src/Api/Middleware/JsonAwareResponse.php | 18 +- src/Api/SlackWebhookClient.php | 10 +- src/Command/CreateJoindInCommand.php | 33 +- tests/Unit/Api/JoindInClientTest.php | 35 +-- tests/Unit/Api/MeetupClientTest.php | 16 +- .../Api/Middleware/JsonAwareResponseTest.php | 31 +- tests/Unit/Api/SlackWebhookClientTest.php | 5 +- .../Unit/Command/CreateJoindInCommandTest.php | 25 +- tests/Unit/Util/GuzzleTestCase.php | 24 +- 16 files changed, 471 insertions(+), 103 deletions(-) create mode 100644 phpcs.xml.dist diff --git a/.gitignore b/.gitignore index 9bb6513..11e4429 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.phar /vendor/ .env .phpunit.result.cache +.phpcs-cache diff --git a/composer.json b/composer.json index bad2ec8..f0f0b3e 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,15 @@ "mockery/mockery": "^1", "phpunit/phpunit": "^9", "vlucas/phpdotenv": "^5", - "deployer/deployer": "^7" + "deployer/deployer": "^7", + "doctrine/coding-standard": "^10.0" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "scripts": { + "phpcs": "phpcs" } } diff --git a/composer.lock b/composer.lock index a2c4268..ac8e302 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ff619fd6684ec349a7e7a3aee3b80a36", + "content-hash": "e2c6ece5075c3b61d8a1080f13214c9a", "packages": [ { "name": "codeliner/array-reader", @@ -1693,6 +1693,81 @@ } ], "packages-dev": [ + { + "name": "dealerdirect/phpcodesniffer-composer-installer", + "version": "v0.7.2", + "source": { + "type": "git", + "url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git", + "reference": "1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db", + "reference": "1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0 || ^2.0", + "php": ">=5.3", + "squizlabs/php_codesniffer": "^2.0 || ^3.1.0 || ^4.0" + }, + "require-dev": { + "composer/composer": "*", + "php-parallel-lint/php-parallel-lint": "^1.3.1", + "phpcompatibility/php-compatibility": "^9.0" + }, + "type": "composer-plugin", + "extra": { + "class": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" + }, + "autoload": { + "psr-4": { + "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Franck Nijhof", + "email": "franck.nijhof@dealerdirect.com", + "homepage": "http://www.frenck.nl", + "role": "Developer / IT Manager" + }, + { + "name": "Contributors", + "homepage": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer Standards Composer Installer Plugin", + "homepage": "http://www.dealerdirect.com", + "keywords": [ + "PHPCodeSniffer", + "PHP_CodeSniffer", + "code quality", + "codesniffer", + "composer", + "installer", + "phpcbf", + "phpcs", + "plugin", + "qa", + "quality", + "standard", + "standards", + "style guide", + "stylecheck", + "tests" + ], + "support": { + "issues": "https://github.com/dealerdirect/phpcodesniffer-composer-installer/issues", + "source": "https://github.com/dealerdirect/phpcodesniffer-composer-installer" + }, + "time": "2022-02-04T12:51:07+00:00" + }, { "name": "deployer/deployer", "version": "v7.0.2", @@ -1736,6 +1811,61 @@ ], "time": "2022-09-10T07:44:30+00:00" }, + { + "name": "doctrine/coding-standard", + "version": "10.0.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/coding-standard.git", + "reference": "7903671d7d33c231c8921058b7c14b8f57cbacb7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/coding-standard/zipball/7903671d7d33c231c8921058b7c14b8f57cbacb7", + "reference": "7903671d7d33c231c8921058b7c14b8f57cbacb7", + "shasum": "" + }, + "require": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7", + "php": "^7.2 || ^8.0", + "slevomat/coding-standard": "^8.2", + "squizlabs/php_codesniffer": "^3.7" + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Steve Müller", + "email": "st.mueller@dzh-online.de" + } + ], + "description": "The Doctrine Coding Standard is a set of PHPCS rules applied to all Doctrine projects.", + "homepage": "https://www.doctrine-project.org/projects/coding-standard.html", + "keywords": [ + "checks", + "code", + "coding", + "cs", + "doctrine", + "rules", + "sniffer", + "sniffs", + "standard", + "style" + ], + "support": { + "issues": "https://github.com/doctrine/coding-standard/issues", + "source": "https://github.com/doctrine/coding-standard/tree/10.0.0" + }, + "time": "2022-08-26T10:53:05+00:00" + }, { "name": "doctrine/instantiator", "version": "1.4.1", @@ -2292,6 +2422,51 @@ ], "time": "2022-07-30T15:51:26+00:00" }, + { + "name": "phpstan/phpdoc-parser", + "version": "1.8.0", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpdoc-parser.git", + "reference": "8dd908dd6156e974b9a0f8bb4cd5ad0707830f04" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/8dd908dd6156e974b9a0f8bb4cd5ad0707830f04", + "reference": "8dd908dd6156e974b9a0f8bb4cd5ad0707830f04", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan": "^1.5", + "phpstan/phpstan-phpunit": "^1.1", + "phpstan/phpstan-strict-rules": "^1.0", + "phpunit/phpunit": "^9.5", + "symfony/process": "^5.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "PHPStan\\PhpDocParser\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPDoc parser with support for nullable, intersection and generic types", + "support": { + "issues": "https://github.com/phpstan/phpdoc-parser/issues", + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.8.0" + }, + "time": "2022-09-04T18:59:06+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "9.2.17", @@ -3672,6 +3847,123 @@ ], "time": "2020-09-28T06:39:44+00:00" }, + { + "name": "slevomat/coding-standard", + "version": "8.5.2", + "source": { + "type": "git", + "url": "https://github.com/slevomat/coding-standard.git", + "reference": "f32937dc41b587f3500efed1dbca2f82aa519373" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/slevomat/coding-standard/zipball/f32937dc41b587f3500efed1dbca2f82aa519373", + "reference": "f32937dc41b587f3500efed1dbca2f82aa519373", + "shasum": "" + }, + "require": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7", + "php": "^7.2 || ^8.0", + "phpstan/phpdoc-parser": ">=1.7.0 <1.9.0", + "squizlabs/php_codesniffer": "^3.7.1" + }, + "require-dev": { + "phing/phing": "2.17.4", + "php-parallel-lint/php-parallel-lint": "1.3.2", + "phpstan/phpstan": "1.4.10|1.8.6", + "phpstan/phpstan-deprecation-rules": "1.0.0", + "phpstan/phpstan-phpunit": "1.0.0|1.1.1", + "phpstan/phpstan-strict-rules": "1.4.4", + "phpunit/phpunit": "7.5.20|8.5.21|9.5.25" + }, + "type": "phpcodesniffer-standard", + "extra": { + "branch-alias": { + "dev-master": "8.x-dev" + } + }, + "autoload": { + "psr-4": { + "SlevomatCodingStandard\\": "SlevomatCodingStandard" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Slevomat Coding Standard for PHP_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.", + "support": { + "issues": "https://github.com/slevomat/coding-standard/issues", + "source": "https://github.com/slevomat/coding-standard/tree/8.5.2" + }, + "funding": [ + { + "url": "https://github.com/kukulich", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/slevomat/coding-standard", + "type": "tidelift" + } + ], + "time": "2022-09-27T16:45:37+00:00" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.7.1", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/1359e176e9307e906dc3d890bcc9603ff6d90619", + "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards" + ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2022-06-18T07:21:10+00:00" + }, { "name": "symfony/polyfill-php80", "version": "v1.26.0", @@ -3892,7 +4184,8 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "ext-json": "*" + "ext-json": "*", + "ext-intl": "*" }, "platform-dev": [], "plugin-api-version": "2.3.0" diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..c14a9c3 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,18 @@ + + + + + + + + + + + + + src + tests + + + + diff --git a/src/Api/JoindInClient.php b/src/Api/JoindInClient.php index b987a30..0682f05 100644 --- a/src/Api/JoindInClient.php +++ b/src/Api/JoindInClient.php @@ -1,5 +1,7 @@ client = new Client([ 'base_uri' => $baseUrl, 'headers' => [ @@ -36,37 +42,39 @@ public function __construct(string $token, string $baseUrl) { * @throws GuzzleException * @throws JsonException */ - public function addEventHost($eventId, $eventHost): JsonAwareResponse + public function addEventHost(string $eventId, string $eventHost): JsonAwareResponse { - /** @var JsonAwareResponse $result */ - $result = $this->client->post('v2.1/events/'.$eventId.'/hosts', [ - 'body' => json_encode(['host_name' => $eventHost], JSON_THROW_ON_ERROR) + $result = $this->client->post('v2.1/events/' . $eventId . '/hosts', [ + 'body' => json_encode(['host_name' => $eventHost], JSON_THROW_ON_ERROR), ]); + assert($result instanceof JsonAwareResponse); return $result; } /** + * @param string[] $event + * * @throws GuzzleException * @throws JsonException */ - public function submitEvent($event): string + public function submitEvent(array $event): string { - /** @var JsonAwareResponse $result */ $result = $this->client->post('v2.1/events', [ - 'body' => json_encode($event, JSON_THROW_ON_ERROR) + 'body' => json_encode($event, JSON_THROW_ON_ERROR), ]); + assert($result instanceof JsonAwareResponse); return $this->extractIdFromLocation($result->getLocationHeader()); } - private function extractIdFromLocation($locationUrl): string + private function extractIdFromLocation(string $locationUrl): string { $matches = []; preg_match( - "/events\/(\d*)/", + '/events\/(\d*)/', $locationUrl, - $matches + $matches, ); return $matches[1] ?? ''; diff --git a/src/Api/MeetupClient.php b/src/Api/MeetupClient.php index cbf0123..c951463 100644 --- a/src/Api/MeetupClient.php +++ b/src/Api/MeetupClient.php @@ -1,5 +1,7 @@ meetupKey = $meetupKey; $this->client = new Client([ 'base_uri' => $baseUrl, 'handler' => DefaultStackFactory::createJsonHandlingStack(), - ]); + ]); } /** @@ -31,7 +33,6 @@ public function __construct(string $meetupKey, string $baseUrl) */ public function getUpcomingEventsForGroup(string $group): CollectionInterface { - /** @var JsonAwareResponse $result */ $result = $this->client->get('/2/events', [ 'query' => [ 'key' => [$this->meetupKey], @@ -41,8 +42,8 @@ public function getUpcomingEventsForGroup(string $group): CollectionInterface 'time' => '0m,1m', ], ]); + assert($result instanceof JsonAwareResponse); return new Collection('array', $result->getJson()['results']); } - } diff --git a/src/Api/Middleware/DefaultStackFactory.php b/src/Api/Middleware/DefaultStackFactory.php index 32ee5ee..6ff639b 100644 --- a/src/Api/Middleware/DefaultStackFactory.php +++ b/src/Api/Middleware/DefaultStackFactory.php @@ -1,5 +1,7 @@ getHeaders(), $response->getBody(), $response->getProtocolVersion(), - $response->getReasonPhrase() + $response->getReasonPhrase(), ); - }), 'json_decode_middleware'); + }, + ), 'json_decode_middleware'); return $stack; } diff --git a/src/Api/Middleware/JsonAwareResponse.php b/src/Api/Middleware/JsonAwareResponse.php index ed84cb0..3d5377b 100644 --- a/src/Api/Middleware/JsonAwareResponse.php +++ b/src/Api/Middleware/JsonAwareResponse.php @@ -1,22 +1,24 @@ cachedJson) { @@ -25,7 +27,7 @@ public function getJson(): array|StreamInterface $body = $this->getBody(); - if (!str_contains($this->getHeaderLine('Content-Type'), 'application/json')) { + if (! str_contains($this->getHeaderLine('Content-Type'), 'application/json')) { return $body; } @@ -34,12 +36,14 @@ public function getJson(): array|StreamInterface } $this->cachedJson = json_decode($body, true, 512, JSON_THROW_ON_ERROR); + return $this->cachedJson; } public function getLocationHeader(): string { $headerValue = $this->getHeader('Location'); - return array_shift($headerValue) ?? ""; + + return array_shift($headerValue) ?? ''; } } diff --git a/src/Api/SlackWebhookClient.php b/src/Api/SlackWebhookClient.php index 9b7beff..e663dfd 100644 --- a/src/Api/SlackWebhookClient.php +++ b/src/Api/SlackWebhookClient.php @@ -1,5 +1,7 @@ client->post($this->webhookUrl, [ 'body' => json_encode($message, JSON_THROW_ON_ERROR), ]); + assert($result instanceof JsonAwareResponse); return $result; } diff --git a/src/Command/CreateJoindInCommand.php b/src/Command/CreateJoindInCommand.php index 8fa2e6b..076656e 100644 --- a/src/Command/CreateJoindInCommand.php +++ b/src/Command/CreateJoindInCommand.php @@ -1,11 +1,14 @@ setDescription('Creates the monthly meeting event at joind.in'); } + /** + * @throws JsonException + * @throws GuzzleException + */ protected function execute(InputInterface $input, OutputInterface $output): int { $meetingCandidates = $this->getCurrentMonthlyMeetingCandidates(); if ($meetingCandidates->count() > 1) { $this->sendSlackMsg('Too many monthly meetings found, I was confused, sorry.', 'construction'); - $output->writeln("Too many monthly meetings"); + $output->writeln('Too many monthly meetings'); + return 0; } $meeting = new ArrayReader($meetingCandidates->first()); $time = $meeting->integerValue('time') / 1000; - $date = \DateTime::createFromFormat('U', $time); + $date = DateTime::createFromFormat('U', (string) $time); $output->writeln( sprintf( '=> Current meeting found: %s', - $meeting->stringValue('name') - ) + $meeting->stringValue('name'), + ), ); - $event = [ 'name' => sprintf('AmsterdamPHP Monthly Meeting - %s', IntlDateFormatter::formatObject($date, 'MMMM/y')), 'description' => 'Every month AmsterdamPHP holds a monthly meeting with a speaker a social event. You can find more info and signup at http://meetup.amsterdamphp.nl', @@ -64,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'tz_place' => 'Amsterdam', 'href' => $meeting->stringValue('event_url'), 'location' => $meeting->stringValue('venue.name'), - 'tags' => 'php, amsterdam' + 'tags' => 'php, amsterdam', ]; $eventId = $this->joindinApi->submitEvent($event); @@ -76,11 +86,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->sendSlackMsg( sprintf( 'Joind.in event created successfully, its awaiting approval. Find it here: https://joind.in/search?keyword=%s', - urlencode($event['name']) - ) + urlencode($event['name']), + ), ); $output->writeln('=> Payload sent to Slack.'); + return 0; } @@ -109,7 +120,7 @@ protected function sendSlackMsg(string $msg, string $icon = 'date'): void */ protected function getCurrentMonthlyMeetingCandidates(): CollectionInterface { - return $this->meetup->getUpcomingEventsForGroup('amsterdamphp')->filter(function($event) { + return $this->meetup->getUpcomingEventsForGroup('amsterdamphp')->filter(static function ($event) { return str_contains($event['name'], 'Monthly Meeting'); }); } diff --git a/tests/Unit/Api/JoindInClientTest.php b/tests/Unit/Api/JoindInClientTest.php index 5458b75..280bb3d 100644 --- a/tests/Unit/Api/JoindInClientTest.php +++ b/tests/Unit/Api/JoindInClientTest.php @@ -1,23 +1,27 @@ 'event_name', - ]; + $payload = ['name' => 'event_name']; $this->client->expects('post') - ->withArgs(fn($url, $opts) => $opts['body'] === json_encode($payload, JSON_THROW_ON_ERROR)) + ->withArgs(static fn ($url, $opts) => $opts['body'] === json_encode($payload, JSON_THROW_ON_ERROR)) ->andReturns($this->getFakeJsonAwareResponse( 200, [], - ['Location' => 'http://some.path/v2.1/events/34']) - ) + ['Location' => 'http://some.path/v2.1/events/34'], + )) ->once(); $eventId = $this->joindin->submitEvent($payload); @@ -47,16 +49,14 @@ public function testSubmitEvent(): void public function testSubmitEventReturnsEmptyEventIdOnBadLocation(): void { - $payload = [ - 'name' => 'event_name', - ]; + $payload = ['name' => 'event_name']; $this->client->expects('post') - ->withArgs(fn($url, $opts) => $opts['body'] === json_encode($payload, JSON_THROW_ON_ERROR)) + ->withArgs(static fn ($url, $opts) => $opts['body'] === json_encode($payload, JSON_THROW_ON_ERROR)) ->andReturns($this->getFakeJsonAwareResponse( 200, [], - ['Location' => 'http://some.path/v2.2/blobs/34']) - ) + ['Location' => 'http://some.path/v2.2/blobs/34'], + )) ->once(); $eventId = $this->joindin->submitEvent($payload); @@ -65,11 +65,10 @@ public function testSubmitEventReturnsEmptyEventIdOnBadLocation(): void public function testAddEventHost(): void { - $host = 'new_host'; + $host = 'new_host'; $eventId = 34; $this->client->expects('post') - ->withArgs(fn($url, $opts) => str_contains($url, $eventId) && $opts['body'] === '{"host_name":"'.$host.'"}' - ) + ->withArgs(static fn ($url, $opts) => str_contains($url, $eventId) && $opts['body'] === '{"host_name":"' . $host . '"}') ->andReturns($this->getFakeJsonAwareResponse(200)) ->once(); diff --git a/tests/Unit/Api/MeetupClientTest.php b/tests/Unit/Api/MeetupClientTest.php index 18c4bf2..1e66eb0 100644 --- a/tests/Unit/Api/MeetupClientTest.php +++ b/tests/Unit/Api/MeetupClientTest.php @@ -1,25 +1,22 @@ client->expects('get') - ->withArgs(fn($url, $opts) => $opts['query'][ - 'key'] === [self::KEY] - &&$opts['query']['group_urlname'] === self::GROUP) + ->withArgs(static fn ($url, $opts) => $opts['query']['key'] === [self::KEY] + && $opts['query']['group_urlname'] === self::GROUP) ->andReturns($this->getFakeJsonAwareResponse(200, ['results' => []])) ->once(); diff --git a/tests/Unit/Api/Middleware/JsonAwareResponseTest.php b/tests/Unit/Api/Middleware/JsonAwareResponseTest.php index 9c8af06..50ba954 100644 --- a/tests/Unit/Api/Middleware/JsonAwareResponseTest.php +++ b/tests/Unit/Api/Middleware/JsonAwareResponseTest.php @@ -1,25 +1,27 @@ 'data']; + $body = ['some' => 'data']; $response = new JsonAwareResponse( 200, ['Content-Type' => 'application/json'], - json_encode($body, JSON_THROW_ON_ERROR) + json_encode($body, JSON_THROW_ON_ERROR), ); self::assertEquals($body, $response->getJson()); @@ -30,7 +32,7 @@ public function testGetJsonCanHandleEmptyResponse(): void $response = new JsonAwareResponse( 200, ['Content-Type' => 'application/json'], - '' + '', ); self::assertEquals([], $response->getJson()); @@ -38,25 +40,23 @@ public function testGetJsonCanHandleEmptyResponse(): void public function testGetJsonCanHandleNonJson(): void { - $body = "Some nonjson content"; + $body = 'Some nonjson content'; $response = new JsonAwareResponse( 200, - body: $body + body: $body, ); self::assertEquals($body, $response->getJson()); } - /** - * @throws JsonException - */ + /** @throws JsonException */ public function testGetJsonIsCached(): void { - $body = ['some' => 'data']; + $body = ['some' => 'data']; $response = new JsonAwareResponse( 200, ['Content-Type' => 'application/json'], - json_encode($body, JSON_THROW_ON_ERROR) + json_encode($body, JSON_THROW_ON_ERROR), ); self::assertEquals($body, $response->getJson()); @@ -65,13 +65,14 @@ public function testGetJsonIsCached(): void public function testGetLocationHeaderWorks(): void { - $location = 'https://path/to/event'; + $location = 'https://path/to/event'; $response = new JsonAwareResponse(200, ['Location' => $location]); self::assertEquals($location, $response->getLocationHeader()); } + public function testGetLocationHeaderHandlesMissingHeader(): void { $response = new JsonAwareResponse(200); - self::assertEquals("", $response->getLocationHeader()); + self::assertEquals('', $response->getLocationHeader()); } } diff --git a/tests/Unit/Api/SlackWebhookClientTest.php b/tests/Unit/Api/SlackWebhookClientTest.php index a0d6cff..62b77ef 100644 --- a/tests/Unit/Api/SlackWebhookClientTest.php +++ b/tests/Unit/Api/SlackWebhookClientTest.php @@ -1,5 +1,7 @@ meetup->expects('getUpcomingEventsForGroup') ->withArgs(['amsterdamphp']) @@ -33,11 +36,11 @@ public function testCommand() 'time' => 1665252181 * 1000, //October 8 2022 'event_url' => 'https://meetup.link', 'venue' => ['name' => 'HQ'], - ] + ], ])); $this->joindin->expects('submitEvent') - ->withArgs(fn($event) => $event['name'] === 'AmsterdamPHP Monthly Meeting - October/2022') + ->withArgs(static fn ($event) => $event['name'] === 'AmsterdamPHP Monthly Meeting - October/2022') ->andReturns('34') ->once(); @@ -47,10 +50,10 @@ public function testCommand() ->once(); $this->slack->expects('sendMessage') - ->withArgs(fn($args) => is_array($args)) + ->withArgs(static fn ($args) => is_array($args)) ->once(); - $input = new ArrayInput([]); + $input = new ArrayInput([]); $output = new NullOutput(); $this->command->run($input, $output); @@ -59,9 +62,9 @@ public function testCommand() protected function setUp(): void { parent::setUp(); - $this->meetup = \Mockery::mock(MeetupClient::class); - $this->slack = \Mockery::mock(SlackWebhookClient::class); - $this->joindin = \Mockery::mock(JoindInClient::class); + $this->meetup = Mockery::mock(MeetupClient::class); + $this->slack = Mockery::mock(SlackWebhookClient::class); + $this->joindin = Mockery::mock(JoindInClient::class); $this->command = new CreateJoindInCommand($this->meetup, $this->slack, $this->joindin); } diff --git a/tests/Unit/Util/GuzzleTestCase.php b/tests/Unit/Util/GuzzleTestCase.php index 40b372b..f80f16d 100644 --- a/tests/Unit/Util/GuzzleTestCase.php +++ b/tests/Unit/Util/GuzzleTestCase.php @@ -1,5 +1,7 @@ getProperty('client'); + $clientProp = $relectedInstance->getProperty('client'); $clientProp->setAccessible(true); $clientProp->setValue($instance, $client); } catch (ReflectionException $e) { @@ -29,21 +34,26 @@ public function getInnerClient(object $instance): ClientInterface { try { $relectedInstance = new ReflectionClass($instance); - $clientProp = $relectedInstance->getProperty('client'); + $clientProp = $relectedInstance->getProperty('client'); $clientProp->setAccessible(true); + return $clientProp->getValue($instance); } catch (ReflectionException $e) { $this->fail('Could not Reflect the Client: ' . $e->getMessage()); } } + /** + * @param string[] $nonEncodedBody + * @param string[] $headers + */ public function getFakeJsonAwareResponse(int $statusCode, array $nonEncodedBody = [], array $headers = []): JsonAwareResponse { try { return new JsonAwareResponse( 200, $headers + ['Content-Type' => 'application/json'], - json_encode($nonEncodedBody, JSON_THROW_ON_ERROR) + json_encode($nonEncodedBody, JSON_THROW_ON_ERROR), ); } catch (JsonException $e) { $this->fail('Cound not create Fake Response: ' . $e->getMessage()); @@ -53,9 +63,9 @@ public function getFakeJsonAwareResponse(int $statusCode, array $nonEncodedBody public function validateStackPresence(object $instance): void { try { - $client = $this->getInnerClient($instance); - $reflectedClient = new ReflectionClass($client); - $configProp = $reflectedClient->getProperty('config'); + $client = $this->getInnerClient($instance); + $reflectedClient = new ReflectionClass($client); + $configProp = $reflectedClient->getProperty('config'); $configProp->setAccessible(true); $config = $configProp->getValue($client);