From 0a1a0da87e3256e79fadc9fa7ff40909bbfec4b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 27 May 2020 16:41:34 +0200 Subject: [PATCH] Compatibility with Mercure 0.10 --- .travis.yml | 9 +++++---- CHANGELOG.md | 5 +++++ README.md | 2 +- phpstan.neon | 10 +++++----- src/Publisher.php | 4 ++-- src/Update.php | 10 +++++----- tests/Debug/TraceablePublisherTest.php | 2 +- tests/PublisherTest.php | 6 +++--- tests/UpdateTest.php | 9 +++++---- 9 files changed, 32 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index 020d332..2a69a7e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,8 +5,9 @@ jobs: - php: '7.1' - php: '7.2' - php: '7.3' + - php: '7.4' env: lint=1 - - php: '7.3' + - php: '7.4' env: deps=low cache: @@ -14,8 +15,8 @@ cache: - $HOME/.composer/cache before_install: - - if [[ $lint = '1' ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.15.1/php-cs-fixer.phar; fi - - if [[ $lint = '1' ]]; then wget https://github.com/phpstan/phpstan/releases/download/0.11.8/phpstan.phar; fi + - if [[ $lint = '1' ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.16.3/php-cs-fixer.phar; fi + - if [[ $lint = '1' ]]; then wget https://github.com/phpstan/phpstan/releases/download/0.12.25/phpstan.phar; fi before_script: - phpenv config-rm xdebug.ini @@ -30,4 +31,4 @@ install: script: - vendor/bin/simple-phpunit - if [[ $lint = '1' ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi - - if [[ $lint = '1' ]]; then php phpstan.phar analyse src tests --level=7; fi + - if [[ $lint = '1' ]]; then php phpstan.phar analyse; fi diff --git a/CHANGELOG.md b/CHANGELOG.md index a4c3dbe..00316b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +0.4.0 +----- + +* Compatibility with Mercure 0.10 + 0.3.0 ----- diff --git a/README.md b/README.md index c50cf29..19543d1 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ use Symfony\Component\Mercure\Update; $publisher = new Publisher(HUB_URL, new StaticJwtProvider(JWT)); // Serialize the update, and dispatch it to the hub, that will broadcast it to the clients -$id = $publisher(new Update('https://example.com/books/1.jsonld', 'Hi from Symfony!', ['target1', 'target2'])); +$id = $publisher(new Update('https://example.com/books/1.jsonld', 'Hi from Symfony!')); ``` Resources diff --git a/phpstan.neon b/phpstan.neon index 114c8b9..0bd9150 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,9 +1,9 @@ parameters: + level: 5 + paths: + - src + - tests inferPrivatePropertyTypeFromConstructor: true + treatPhpDocTypesAsCertain: false autoload_files: - vendor/bin/.phpunit/phpunit-8.3-0/vendor/autoload.php - ignoreErrors: - # Intended - - '#Parameter \#1 \$topics of class Symfony\\Component\\Mercure\\Update constructor expects array\|string, int given\.#' - # False positive - - '#Result of && is always false\.#' diff --git a/src/Publisher.php b/src/Publisher.php index d6dbda0..52ebb8d 100644 --- a/src/Publisher.php +++ b/src/Publisher.php @@ -32,7 +32,7 @@ final class Publisher implements PublisherInterface private $httpClient; /** - * @param callable(): string $jwtProvider + * @param callable(Update $update): string $jwtProvider */ public function __construct(string $hubUrl, callable $jwtProvider, HttpClientInterface $httpClient = null) { @@ -46,7 +46,7 @@ public function __invoke(Update $update): string $postData = [ 'topic' => $update->getTopics(), 'data' => $update->getData(), - 'target' => $update->getTargets(), + 'private' => $update->isPrivate() ? 'on' : null, 'id' => $update->getId(), 'type' => $update->getType(), 'retry' => $update->getRetry(), diff --git a/src/Update.php b/src/Update.php index b411559..5d3ec38 100644 --- a/src/Update.php +++ b/src/Update.php @@ -27,7 +27,7 @@ final class Update { private $topics; private $data; - private $targets; + private $private; private $id; private $type; private $retry; @@ -35,7 +35,7 @@ final class Update /** * @param array|string $topics */ - public function __construct($topics, string $data, array $targets = [], string $id = null, string $type = null, int $retry = null) + public function __construct($topics, string $data = '', bool $private = false, string $id = null, string $type = null, int $retry = null) { if (!\is_array($topics) && !\is_string($topics)) { throw new \InvalidArgumentException('$topics must be an array of strings or a string'); @@ -43,7 +43,7 @@ public function __construct($topics, string $data, array $targets = [], string $ $this->topics = (array) $topics; $this->data = $data; - $this->targets = $targets; + $this->private = $private; $this->id = $id; $this->type = $type; $this->retry = $retry; @@ -59,9 +59,9 @@ public function getData(): string return $this->data; } - public function getTargets(): array + public function isPrivate(): bool { - return $this->targets; + return $this->private; } public function getId(): ?string diff --git a/tests/Debug/TraceablePublisherTest.php b/tests/Debug/TraceablePublisherTest.php index 33ff67e..b0972b8 100644 --- a/tests/Debug/TraceablePublisherTest.php +++ b/tests/Debug/TraceablePublisherTest.php @@ -36,7 +36,7 @@ public function testPublish(): void $update = new Update( 'https://demo.mercure.rocks/demo/books/1.jsonld', 'Hi from Symfony!', - [], + false, 'id', null, 3 diff --git a/tests/PublisherTest.php b/tests/PublisherTest.php index 3dc16a9..5f65712 100644 --- a/tests/PublisherTest.php +++ b/tests/PublisherTest.php @@ -26,7 +26,7 @@ class PublisherTest extends TestCase { const URL = 'https://demo.mercure.rocks/.well-known/mercure'; - const JWT = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InN1YnNjcmliZSI6WyJmb28iLCJiYXIiXSwicHVibGlzaCI6WyJmb28iXX19.LRLvirgONK13JgacQ_VbcjySbVhkSmHy3IznH3tA9PM'; + const JWT = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InN1YnNjcmliZSI6WyIqIl0sInB1Ymxpc2giOlsiKiJdfX0.M1yJUov4a6oLrigTqBZQO_ohWUsg3Uz1bnLD4MIyWLo'; const AUTH_HEADER = 'Authorization: Bearer '.self::JWT; public function testPublish() @@ -39,7 +39,7 @@ public function testPublish() $this->assertSame('POST', $method); $this->assertSame(self::URL, $url); $this->assertSame(self::AUTH_HEADER, $options['normalized_headers']['authorization'][0]); - $this->assertSame('topic=https%3A%2F%2Fdemo.mercure.rocks%2Fdemo%2Fbooks%2F1.jsonld&data=Hi+from+Symfony%21&id=id&retry=3', $options['body']); + $this->assertSame('topic=https%3A%2F%2Fdemo.mercure.rocks%2Fdemo%2Fbooks%2F1.jsonld&data=Hi+from+Symfony%21&private=on&id=id&retry=3', $options['body']); return new MockResponse('id'); }); @@ -50,7 +50,7 @@ public function testPublish() new Update( 'https://demo.mercure.rocks/demo/books/1.jsonld', 'Hi from Symfony!', - [], + true, 'id', null, 3 diff --git a/tests/UpdateTest.php b/tests/UpdateTest.php index 446d679..4b361b4 100644 --- a/tests/UpdateTest.php +++ b/tests/UpdateTest.php @@ -27,12 +27,12 @@ class UpdateTest extends TestCase * @param mixed $topics * @param mixed $data */ - public function testCreateUpdate($topics, $data, array $targets = [], string $id = null, string $type = null, int $retry = null) + public function testCreateUpdate($topics, $data, bool $private = false, string $id = null, string $type = null, int $retry = null) { - $update = new Update($topics, $data, $targets, $id, $type, $retry); + $update = new Update($topics, $data, $private, $id, $type, $retry); $this->assertSame((array) $topics, $update->getTopics()); $this->assertSame($data, $update->getData()); - $this->assertSame($targets, $update->getTargets()); + $this->assertSame($private, $update->isPrivate()); $this->assertSame($id, $update->getId()); $this->assertSame($type, $update->getType()); $this->assertSame($retry, $update->getRetry()); @@ -41,7 +41,7 @@ public function testCreateUpdate($topics, $data, array $targets = [], string $id public function updateProvider(): array { return [ - ['http://example.com/foo', 'payload', ['user-1', 'group-a'], 'id', 'type', 1936], + ['http://example.com/foo', 'payload', true, 'id', 'type', 1936], [['https://mercure.rocks', 'https://github.com/dunglas/mercure'], 'payload'], ]; } @@ -50,6 +50,7 @@ public function testInvalidTopic() { $this->expectException(\InvalidArgumentException::class); + // @phpstan-ignore-next-line new Update(1, 'data'); } }