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

Compatibility with Mercure 0.10 #25

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ 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:
directories:
- $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
Expand All @@ -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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

0.4.0
-----

* Compatibility with Mercure 0.10

0.3.0
-----

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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\.#'
4 changes: 2 additions & 2 deletions src/Publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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(),
Expand Down
10 changes: 5 additions & 5 deletions src/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ final class Update
{
private $topics;
private $data;
private $targets;
private $private;
private $id;
private $type;
private $retry;

/**
* @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');
}

$this->topics = (array) $topics;
$this->data = $data;
$this->targets = $targets;
$this->private = $private;
$this->id = $id;
$this->type = $type;
$this->retry = $retry;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Debug/TraceablePublisherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/PublisherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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');
});
Expand All @@ -50,7 +50,7 @@ public function testPublish()
new Update(
'https://demo.mercure.rocks/demo/books/1.jsonld',
'Hi from Symfony!',
[],
true,
'id',
null,
3
Expand Down
9 changes: 5 additions & 4 deletions tests/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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'],
];
}
Expand All @@ -50,6 +50,7 @@ public function testInvalidTopic()
{
$this->expectException(\InvalidArgumentException::class);

// @phpstan-ignore-next-line
new Update(1, 'data');
}
}