From 99af4bc841c1b6156fea2a325d685e3243dbbb6b Mon Sep 17 00:00:00 2001 From: howardwu Date: Wed, 17 Jul 2019 11:42:01 +0800 Subject: [PATCH 1/3] Add test into project, and fix client inject at constructor --- composer.json | 5 +++-- phpunit.xml | 7 +++++++ src/Client.php | 25 +++++++++++++++++++------ tests/ClientTest.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/ClientTest.php diff --git a/composer.json b/composer.json index 6a6702f..2f52670 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..f863c75 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,7 @@ + + + + tests + + + \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index 7d92a37..de90330 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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 * diff --git a/tests/ClientTest.php b/tests/ClientTest.php new file mode 100644 index 0000000..04f8c5c --- /dev/null +++ b/tests/ClientTest.php @@ -0,0 +1,43 @@ + '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)); + } +} \ No newline at end of file From 3e3d6007391ed00ce45297d67726eedc5d14e7e5 Mon Sep 17 00:00:00 2001 From: howardwu Date: Wed, 17 Jul 2019 11:45:26 +0800 Subject: [PATCH 2/3] Add travis ci yaml --- .travis.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..360d2b3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: php +php: + - '7.2' +install: + - composer install +script: vendor/bin/phpunit +sudo: false \ No newline at end of file From b0821788b794a3642ae5f6638c8db99b0ada2010 Mon Sep 17 00:00:00 2001 From: howardwu Date: Wed, 17 Jul 2019 11:48:39 +0800 Subject: [PATCH 3/3] Add travis build status image --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 06d022d..c07feef 100644 --- a/readme.md +++ b/readme.md @@ -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 方式