1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
<?php
/*
* This file is part of the JoliNotif project.
*
* (c) Loïck Piera <pyrech@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Joli\JoliNotif\tests\Driver;
use Joli\JoliNotif\Driver\DriverInterface;
use Joli\JoliNotif\Notification;
use JoliCode\PhpOsHelper\OsHelper;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use Symfony\Component\Process\Process;
/**
* Classes using this trait should define a BINARY constant and extend
* AbstractDriverTestCase.
*/
trait AbstractCliBasedDriverTestTrait
{
public function testIsSupported()
{
if (OsHelper::isUnix()) {
$commandLine = 'command -v ' . static::BINARY . ' >/dev/null 2>&1';
} else {
$commandLine = 'where ' . static::BINARY;
}
passthru($commandLine, $return);
$supported = 0 === $return;
$this->assertSame($supported, $this->getDriver()->isSupported());
}
#[DataProvider('provideValidNotifications')]
#[RequiresPhpunit('< 11')]
public function testConfigureProcessAcceptAnyValidNotification(Notification $notification, string $expectedCommandLine)
{
try {
$arguments = $this->invokeMethod($this->getDriver(), 'getCommandLineArguments', [$notification]);
$this->assertSame($expectedCommandLine, (new Process(array_merge([self::BINARY], $arguments)))->getCommandLine());
} catch (\Exception $e) {
$this->fail($e->getMessage());
}
}
public function provideValidNotifications(): array
{
$iconDir = $this->getIconDir();
return [
[
(new Notification())
->setBody('I\'m the notification body'),
$this->getExpectedCommandLineForNotification(),
],
[
(new Notification())
->setBody('I\'m the notification body')
->setTitle('I\'m the notification title'),
$this->getExpectedCommandLineForNotificationWithATitle(),
],
[
(new Notification())
->setBody('I\'m the notification body')
->addOption('subtitle', 'I\'m the notification subtitle'),
$this->getExpectedCommandLineForNotificationWithASubtitle(),
],
[
(new Notification())
->setBody('I\'m the notification body')
->addOption('sound', 'Frog'),
$this->getExpectedCommandLineForNotificationWithASound(),
],
[
(new Notification())
->setBody('I\'m the notification body')
->addOption('url', 'https://google.com'),
$this->getExpectedCommandLineForNotificationWithAnUrl(),
],
[
(new Notification())
->setBody('I\'m the notification body')
->setIcon($iconDir . '/image.gif'),
$this->getExpectedCommandLineForNotificationWithAnIcon(),
],
[
(new Notification())
->setBody('I\'m the notification body')
->setTitle('I\'m the notification title')
->addOption('subtitle', 'I\'m the notification subtitle')
->addOption('sound', 'Frog')
->addOption('url', 'https://google.com')
->setIcon($iconDir . '/image.gif'),
$this->getExpectedCommandLineForNotificationWithAllOptions(),
],
];
}
public function testSendThrowsExceptionWhenNotificationDoesntHaveBody()
{
$driver = $this->getDriver();
$notification = new Notification();
try {
$driver->send($notification);
$this->fail('Expected a InvalidNotificationException');
} catch (\Exception $e) {
$this->assertInstanceOf('Joli\JoliNotif\Exception\InvalidNotificationException', $e);
}
}
public function testSendThrowsExceptionWhenNotificationHasAnEmptyBody()
{
$driver = $this->getDriver();
$notification = new Notification();
$notification->setBody('');
try {
$driver->send($notification);
$this->fail('Expected a InvalidNotificationException');
} catch (\Exception $e) {
$this->assertInstanceOf('Joli\JoliNotif\Exception\InvalidNotificationException', $e);
}
}
abstract protected function getDriver(): DriverInterface;
abstract protected function getExpectedCommandLineForNotification(): string;
abstract protected function getExpectedCommandLineForNotificationWithATitle(): string;
/**
* Subtitle is supported only on few driver.
*/
protected function getExpectedCommandLineForNotificationWithASubtitle(): string
{
return $this->getExpectedCommandLineForNotification();
}
/**
* Sound is supported only on few driver.
*/
protected function getExpectedCommandLineForNotificationWithASound(): string
{
return $this->getExpectedCommandLineForNotification();
}
/**
* Sound is supported only on few driver.
*/
protected function getExpectedCommandLineForNotificationWithAnUrl(): string
{
return $this->getExpectedCommandLineForNotification();
}
abstract protected function getExpectedCommandLineForNotificationWithAnIcon(): string;
abstract protected function getExpectedCommandLineForNotificationWithAllOptions(): string;
}
|