Skip to content

Commit

Permalink
Unit tests for \AdamWojs\AwokadoRobot\Notification\SlackWebhookTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Mar 10, 2018
1 parent d0d147b commit e87fcce
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
"monolog/monolog": "^1.23"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~2.7.1"
"friendsofphp/php-cs-fixer": "~2.10",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
"AdamWojs\\AwokadoRobot\\": "src/"
}
},
"exclude-from-classmap": [
"/src/Tests/"
]
},
"scripts": {
"fix-cs": "@php ./vendor/bin/php-cs-fixer fix src -v --show-progress=estimating"
"fix-cs": "@php ./vendor/bin/php-cs-fixer fix src -v --show-progress=estimating",
"run-tests": "@php ./vendor/bin/phpunit -c phpunit.xml.dist"
}
}
26 changes: 26 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php">
<php>
<ini name="error_reporting" value="-1"/>
</php>

<testsuites>
<testsuite name="Load Balancer Test Suite">
<directory>./src/Tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./src/Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
4 changes: 2 additions & 2 deletions src/Notification/SlackWebhookTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function send(Menu $menu): void
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
'json' => [
'text' => $text,
]),
],
]);
} catch (Exception $e) {
throw new TransportException('Notification transport error: ' . $e->getMessage(), 0, $e);
Expand Down
74 changes: 74 additions & 0 deletions src/Tests/Notification/SlackWebhookTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace AdamWojs\AwokadoRobot\Tests\Notification;

use AdamWojs\AwokadoRobot\Menu;
use AdamWojs\AwokadoRobot\MenuItem;
use AdamWojs\AwokadoRobot\Notification\SlackWebhookTransport;
use DateTimeImmutable;
use Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\ClientInterface;

class SlackWebhookTransportTest extends TestCase
{
const WEBHOOK_URL = 'https://hooks.slack.com/services/XYZ';

/** @var SlackWebhookTransport */
private $transport;

/** @var ClientInterface|MockObject */
private $client;

protected function setUp()
{
$this->client = $this->createMock(ClientInterface::class);
$this->transport = new SlackWebhookTransport($this->client, self::WEBHOOK_URL);
}

/**
* @test
*/
public function isSendingProperRequest()
{
$payload = [
'headers' => [
'Content-Type' => 'application/json',
],
'json' => [
'text' => "Menu na dziś (tj. 01-01-2018) :fork_and_knife:\n```\nA - 14,50 zł\nB - 12,50 zł\nC - 11,50 zł\n```",
],
];

$this->client
->expects($this->once())
->method('request')
->with('POST', self::WEBHOOK_URL, $payload);

$menu = new Menu(new DateTimeImmutable('2018-01-01'), [
new MenuItem('A', '14,50 zł'),
new MenuItem('B', '12,50 zł'),
new MenuItem('C', '11,50 zł'),
]);

$this->transport->send($menu);
}

/**
* @test
* @expectedException \AdamWojs\AwokadoRobot\Notification\TransportException
* @expectedExceptionMessage Notification transport error: Krem Buraczano-Chrzanowy detected!
*/
public function isThrowingTransportExceptionOnFailure()
{
$exception = new Exception('Krem Buraczano-Chrzanowy detected!');

$this->client
->expects($this->once())
->method('request')
->willThrowException($exception);

$this->transport->send($this->createMock(Menu::class));
}
}

0 comments on commit e87fcce

Please sign in to comment.