Skip to content

Commit

Permalink
Merge pull request #35 from gremo/use-realpath-icon
Browse files Browse the repository at this point in the history
Use realpath() when setting the notification icon
  • Loading branch information
pyrech authored Jun 2, 2017
2 parents 84c565c + f277c7b commit f4785d5
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes between versions

## Not yet released

* Fixed Notification icon to always use a canonical absolute path

## 1.1.0

* Added NotifierFactory::createOrThrowException() method
Expand Down
3 changes: 3 additions & 0 deletions src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public function setIcon($icon)
// This makes the icon accessible for native commands when it's embedded inside a phar
if (PharExtractor::isLocatedInsideAPhar($icon)) {
$icon = PharExtractor::extractFile($icon);
} else {
// Makes the icon path absolute (expanding all symbolic links and resolving references like "/../")
$icon = realpath($icon);
}

$this->icon = $icon;
Expand Down
10 changes: 10 additions & 0 deletions tests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Joli\JoliNotif\tests;

use Joli\JoliNotif\Notification;

class NotificationTest extends \PHPUnit_Framework_TestCase
{
public function testItExtractsIconFromPhar()
Expand Down Expand Up @@ -55,4 +57,12 @@ public function testItExtractsIconFromPhar()
$this->assertTrue(is_file($extractedIconPath));
$this->assertSame($iconContent, file_get_contents($extractedIconPath));
}

public function testItResolvesRealPathToIcon()
{
$notification = new Notification();
$notification->setIcon(__DIR__.'/../tests/fixtures/image.gif');

$this->assertSame(__DIR__.'/fixtures/image.gif', $notification->getIcon());
}
}
11 changes: 9 additions & 2 deletions tests/Notifier/CliBasedNotifierTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function testConfigureProcessAcceptAnyValidNotification(Notification $not
*/
public function provideValidNotifications()
{
$iconDir = $this->getIconDir();

return [
[
(new Notification())
Expand All @@ -75,14 +77,14 @@ public function provideValidNotifications()
[
(new Notification())
->setBody('I\'m the notification body')
->setIcon('/home/toto/Images/my-icon.png'),
->setIcon($iconDir.'/image.gif'),
$this->getExpectedCommandLineForNotificationWithAnIcon(),
],
[
(new Notification())
->setBody('I\'m the notification body')
->setTitle('I\'m the notification title')
->setIcon('/home/toto/Images/my-icon.png'),
->setIcon($iconDir.'/image.gif'),
$this->getExpectedCommandLineForNotificationWithAllOptions(),
],
];
Expand Down Expand Up @@ -117,6 +119,11 @@ public function testSendThrowsExceptionWhenNotificationHasAnEmptyBody()
}
}

public function getIconDir()
{
return realpath(dirname(__DIR__).'/fixtures');
}

/**
* @return string
*/
Expand Down
8 changes: 6 additions & 2 deletions tests/Notifier/GrowlNotifyNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ protected function getExpectedCommandLineForNotificationWithATitle()
*/
protected function getExpectedCommandLineForNotificationWithAnIcon()
{
$iconDir = $this->getIconDir();

return <<<CLI
'growlnotify' '--message' 'I'\''m the notification body' '--image' '/home/toto/Images/my-icon.png'
'growlnotify' '--message' 'I'\''m the notification body' '--image' '${iconDir}/image.gif'
CLI;
}

Expand All @@ -73,8 +75,10 @@ protected function getExpectedCommandLineForNotificationWithAnIcon()
*/
protected function getExpectedCommandLineForNotificationWithAllOptions()
{
$iconDir = $this->getIconDir();

return <<<CLI
'growlnotify' '--message' 'I'\''m the notification body' '--title' 'I'\''m the notification title' '--image' '/home/toto/Images/my-icon.png'
'growlnotify' '--message' 'I'\''m the notification body' '--title' 'I'\''m the notification title' '--image' '${iconDir}/image.gif'
CLI;
}
}
8 changes: 6 additions & 2 deletions tests/Notifier/NotifuNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ protected function getExpectedCommandLineForNotificationWithATitle()
*/
protected function getExpectedCommandLineForNotificationWithAnIcon()
{
$iconDir = $this->getIconDir();

return <<<CLI
'notifu' '/m' 'I'\''m the notification body' '/i' '/home/toto/Images/my-icon.png'
'notifu' '/m' 'I'\''m the notification body' '/i' '${iconDir}/image.gif'
CLI;
}

Expand All @@ -74,8 +76,10 @@ protected function getExpectedCommandLineForNotificationWithAnIcon()
*/
protected function getExpectedCommandLineForNotificationWithAllOptions()
{
$iconDir = $this->getIconDir();

return <<<CLI
'notifu' '/m' 'I'\''m the notification body' '/p' 'I'\''m the notification title' '/i' '/home/toto/Images/my-icon.png'
'notifu' '/m' 'I'\''m the notification body' '/p' 'I'\''m the notification title' '/i' '${iconDir}/image.gif'
CLI;
}
}
8 changes: 6 additions & 2 deletions tests/Notifier/NotifySendNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ protected function getExpectedCommandLineForNotificationWithATitle()
*/
protected function getExpectedCommandLineForNotificationWithAnIcon()
{
$iconDir = $this->getIconDir();

return <<<CLI
'notify-send' '--icon' '/home/toto/Images/my-icon.png' 'I'\''m the notification body'
'notify-send' '--icon' '${iconDir}/image.gif' 'I'\''m the notification body'
CLI;
}

Expand All @@ -73,8 +75,10 @@ protected function getExpectedCommandLineForNotificationWithAnIcon()
*/
protected function getExpectedCommandLineForNotificationWithAllOptions()
{
$iconDir = $this->getIconDir();

return <<<CLI
'notify-send' '--icon' '/home/toto/Images/my-icon.png' 'I'\''m the notification title' 'I'\''m the notification body'
'notify-send' '--icon' '${iconDir}/image.gif' 'I'\''m the notification title' 'I'\''m the notification body'
CLI;
}
}
8 changes: 6 additions & 2 deletions tests/Notifier/TerminalNotifierNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ protected function getExpectedCommandLineForNotificationWithATitle()
protected function getExpectedCommandLineForNotificationWithAnIcon()
{
if (OsHelper::isMacOS() && version_compare(OsHelper::getMacOSVersion(), '10.9.0', '>=')) {
$iconDir = $this->getIconDir();

return <<<CLI
'terminal-notifier' '-message' 'I'\''m the notification body' '-appIcon' '/home/toto/Images/my-icon.png'
'terminal-notifier' '-message' 'I'\''m the notification body' '-appIcon' '${iconDir}/image.gif'
CLI;
}

Expand All @@ -81,8 +83,10 @@ protected function getExpectedCommandLineForNotificationWithAnIcon()
protected function getExpectedCommandLineForNotificationWithAllOptions()
{
if (OsHelper::isMacOS() && version_compare(OsHelper::getMacOSVersion(), '10.9.0', '>=')) {
$iconDir = $this->getIconDir();

return <<<CLI
'terminal-notifier' '-message' 'I'\''m the notification body' '-title' 'I'\''m the notification title' '-appIcon' '/home/toto/Images/my-icon.png'
'terminal-notifier' '-message' 'I'\''m the notification body' '-title' 'I'\''m the notification title' '-appIcon' '${iconDir}/image.gif'
CLI;
}

Expand Down
8 changes: 6 additions & 2 deletions tests/Notifier/ToasterNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ protected function getExpectedCommandLineForNotificationWithATitle()
*/
protected function getExpectedCommandLineForNotificationWithAnIcon()
{
$iconDir = $this->getIconDir();

return <<<CLI
'toast' '-m' 'I'\''m the notification body' '-p' '/home/toto/Images/my-icon.png'
'toast' '-m' 'I'\''m the notification body' '-p' '${iconDir}/image.gif'
CLI;
}

Expand All @@ -74,8 +76,10 @@ protected function getExpectedCommandLineForNotificationWithAnIcon()
*/
protected function getExpectedCommandLineForNotificationWithAllOptions()
{
$iconDir = $this->getIconDir();

return <<<CLI
'toast' '-m' 'I'\''m the notification body' '-t' 'I'\''m the notification title' '-p' '/home/toto/Images/my-icon.png'
'toast' '-m' 'I'\''m the notification body' '-t' 'I'\''m the notification title' '-p' '${iconDir}/image.gif'
CLI;
}
}
1 change: 1 addition & 0 deletions tests/fixtures/image.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f4785d5

Please sign in to comment.