60 lines
2 KiB
PHP
60 lines
2 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Tests\Native;
|
|
|
|
use JumPHP\Bridge\JsBridge;
|
|
use JumPHP\Native\BrowserProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class BrowserNotificationTest extends TestCase
|
|
{
|
|
private function bridge(): JsBridge
|
|
{
|
|
return new class extends JsBridge {
|
|
/** @var list<array{string, array}> */
|
|
public array $sent = [];
|
|
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function send(string $type, array $data = []): bool
|
|
{
|
|
$this->sent[] = [$type, $data];
|
|
return true;
|
|
}
|
|
};
|
|
}
|
|
|
|
public function testNothingIsShownBeforeThePersonAllowsIt(): void
|
|
{
|
|
$bridge = $this->bridge();
|
|
$provider = new BrowserProvider($bridge);
|
|
self::assertTrue($provider->supports('notify'));
|
|
self::assertSame('default', $provider->notificationPermission());
|
|
self::assertFalse($provider->notify('Bob', 'Coucou'));
|
|
self::assertSame([], $bridge->sent);
|
|
}
|
|
|
|
public function testAskingGoesToThePageAndAGrantedPermissionShowsThem(): void
|
|
{
|
|
$bridge = $this->bridge();
|
|
$provider = new BrowserProvider($bridge);
|
|
$provider->requestNotifications();
|
|
self::assertSame(['notify-permission', []], $bridge->sent[0]);
|
|
$bridge->emit('notify-permission', [['state' => 'granted']]); // the person said yes
|
|
self::assertSame('granted', $provider->notificationPermission());
|
|
self::assertTrue($provider->notify('Bob', 'Coucou'));
|
|
self::assertSame(['notify', ['title' => 'Bob', 'body' => 'Coucou']], $bridge->sent[1]);
|
|
}
|
|
|
|
public function testADeniedPermissionStaysDenied(): void
|
|
{
|
|
$bridge = $this->bridge();
|
|
$provider = new BrowserProvider($bridge);
|
|
$bridge->emit('notify-permission', [['state' => 'denied']]);
|
|
self::assertFalse($provider->notify('Bob', 'Coucou'));
|
|
$bridge->emit('notify-permission', [['state' => 'nonsense']]);
|
|
self::assertSame('default', $provider->notificationPermission());
|
|
}
|
|
}
|