Skip to content

Commit

Permalink
Merge pull request #2 from Fred07/feature/implement-test
Browse files Browse the repository at this point in the history
Feature/implement test
  • Loading branch information
Fred07 authored Jul 17, 2019
2 parents 6de90f1 + b082178 commit 972526b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: php
php:
- '7.2'
install:
- composer install
script: vendor/bin/phpunit
sudo: false
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
],
"minimum-stability": "stable",
"require": {
"php": ">=7.1",
"php": ">=7.2",
"guzzlehttp/guzzle": "^6.3",
"ext-json": "*"
},
"require-dev": {
"orchestra/testbench": "^3.8"
"orchestra/testbench": "^3.8",
"phpunit/phpunit": "^8.2"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="phpunit test">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.com/Fred07/laravel-slack.svg?branch=master)](https://travis-ci.com/Fred07/laravel-slack)

# slack service provider

本專案以 service provider 方式
Expand Down
25 changes: 19 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,35 @@ class Client

/**
* Client constructor.
* @param Guzzle|null $client
* @param string $endpoint
* @param string|null $channel
* @param string $username
*/
public function __construct(string $endpoint, string $channel = null, $username = '')
public function __construct(string $endpoint, string $channel = null, $username = '', Guzzle $client = null)
{
$this->http = new Guzzle([
'headers' => [
'Content-type: application/json'
],
]);
if ($client === null) {
$this->http = $this->makeHttpClient();
} else {
$this->http = $client;
}

$this->channel = $channel;
$this->username = $username;
$this->endpoint = $endpoint;
}

/**
* @param array $headers
* @return Guzzle
*/
private function makeHttpClient(array $headers = []): Guzzle
{
return new Guzzle([
'headers' => !empty($headers) ? $headers : ['Content-type: application/json'],
]);
}

/**
* @param callable $callback
*
Expand Down
43 changes: 43 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Fred\SlackService\Message;
use PHPUnit\Framework\TestCase;

class ClientTest extends TestCase
{
/**
* @test
*/
public function it_should_send_correct_message_to_slack(): void
{
$expected = [
'text' => 'hello',
'channel' => 't-ch',
'mrkdwn' => true,
'username' => 't-name',
];

$container = [];
$history = GuzzleHttp\Middleware::history($container);
$mock = new GuzzleHttp\Handler\MockHandler([
new GuzzleHttp\Psr7\Response(200, ['test' => true]),
]);
$handler = GuzzleHttp\HandlerStack::create($mock);
$handler->push($history);
$client = new GuzzleHttp\Client(['handler' => $handler]);

$slackClient = new Fred\SlackService\Client('/mock-server', 't-ch', 't-name', $client);
$slackClient->compose(function (Message $message) {
$message->setText('hello');
return $message;
})->send();

$transaction = $container[0];
/** @var \GuzzleHttp\Psr7\Request $request */
$request = $transaction['request'];

$actual = $request->getBody()->getContents();

$this->assertEquals($expected, json_decode($actual, true));
}
}

0 comments on commit 972526b

Please sign in to comment.