diff --git a/module/VuFind/src/VuFind/Log/Writer/Post.php b/module/VuFind/src/VuFind/Log/Writer/Post.php index e9733208189d22f34549cfe3edd81fd12ae89757..ba4d563ca1080e5a503cb8242b5288929cfe3b42 100644 --- a/module/VuFind/src/VuFind/Log/Writer/Post.php +++ b/module/VuFind/src/VuFind/Log/Writer/Post.php @@ -96,7 +96,9 @@ class Post extends \Laminas\Log\Writer\AbstractWriter */ protected function getBody($event) { - return ['message' => $this->formatter->format($event) . PHP_EOL]; + return json_encode( + ['message' => $this->formatter->format($event) . PHP_EOL] + ); } /** diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Log/LoggerTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Log/LoggerTest.php index 05cea73db21b10a23ab61b020dca19542e191da9..6b8b4bc369d5c2e66afbcf101451ae617d685efb 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/Log/LoggerTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Log/LoggerTest.php @@ -30,7 +30,7 @@ namespace VuFindTest\Log; use VuFind\Log\Logger; /** - * Sitemap Test Class + * Logger Test Class * * @category VuFind * @package Tests diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Log/Writer/PostTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Log/Writer/PostTest.php new file mode 100644 index 0000000000000000000000000000000000000000..3aa83a28cb867624453400a90c0370311abb29bc --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Log/Writer/PostTest.php @@ -0,0 +1,83 @@ +<?php +/** + * POST Log Writer Test Class + * + * PHP version 7 + * + * Copyright (C) Villanova University 2020. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:testing:unit_tests Wiki + */ +namespace VuFindTest\Log\Writer; + +use Laminas\Http\Client; +use Laminas\Log\Formatter\Simple; +use VuFind\Log\Writer\Post; + +/** + * POST Log Writer Test Class + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:testing:unit_tests Wiki + */ +class PostTest extends \VuFindTest\Unit\TestCase +{ + /** + * Test writer functionality + */ + public function testWriter() + { + // Set up data and expectations: + $fakeUri = 'http://fake'; + $expectedBody = json_encode( + ['message' => 'Formatted message.' . PHP_EOL] + ); + $message = ['message' => 'test', 'priority' => 1]; + + // Set up mock client: + $client = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor()->getMock(); + $client->expects($this->once())->method('setUri') + ->with($this->equalTo($fakeUri)); + $client->expects($this->once())->method('setMethod') + ->with($this->equalTo('POST')); + $client->expects($this->once())->method('setEncType') + ->with($this->equalTo('application/json')); + $client->expects($this->once())->method('setRawBody') + ->with($this->equalTo($expectedBody)); + $client->expects($this->once())->method('send'); + + // Set up mock formatter: + $formatter = $this->getMockBuilder(Simple::class) + ->disableOriginalConstructor()->getMock(); + $formatter->expects($this->once())->method('format') + ->with($this->equalTo($message)) + ->will($this->returnValue('Formatted message.')); + + // Run the test! + $writer = new Post($fakeUri, $client); + $writer->setContentType('application/json'); + $writer->setFormatter($formatter); + $writer->write($message); + } +} diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/Log/Writer/SlackTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/Log/Writer/SlackTest.php new file mode 100644 index 0000000000000000000000000000000000000000..75abe8ad0e19cfc51bc29f2022e9cd60763b7673 --- /dev/null +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/Log/Writer/SlackTest.php @@ -0,0 +1,88 @@ +<?php +/** + * Slack Log Writer Test Class + * + * PHP version 7 + * + * Copyright (C) Villanova University 2020. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:testing:unit_tests Wiki + */ +namespace VuFindTest\Log\Writer; + +use Laminas\Http\Client; +use Laminas\Log\Formatter\Simple; +use VuFind\Log\Writer\Slack; + +/** + * Slack Log Writer Test Class + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org/wiki/development:testing:unit_tests Wiki + */ +class SlackTest extends \VuFindTest\Unit\TestCase +{ + /** + * Test writer functionality + */ + public function testWriter() + { + // Set up data and expectations: + $fakeUri = 'http://fake'; + $expectedBody = json_encode( + [ + "channel" => "#test", + "username" => "TestName", + "text" => ":rotating_light: Formatted message." . PHP_EOL, + ] + ); + $message = ['message' => 'test', 'priority' => 1]; + $options = ['name' => 'TestName', 'channel' => '#test']; + + // Set up mock client: + $client = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor()->getMock(); + $client->expects($this->once())->method('setUri') + ->with($this->equalTo($fakeUri)); + $client->expects($this->once())->method('setMethod') + ->with($this->equalTo('POST')); + $client->expects($this->once())->method('setEncType') + ->with($this->equalTo('application/json')); + $client->expects($this->once())->method('setRawBody') + ->with($this->equalTo($expectedBody)); + $client->expects($this->once())->method('send'); + + // Set up mock formatter: + $formatter = $this->getMockBuilder(Simple::class) + ->disableOriginalConstructor()->getMock(); + $formatter->expects($this->once())->method('format') + ->with($this->equalTo($message)) + ->will($this->returnValue('Formatted message.')); + + // Run the test! + $writer = new Slack($fakeUri, $client, $options); + $writer->setContentType('application/json'); + $writer->setFormatter($formatter); + $writer->write($message); + } +}