Skip to content
Snippets Groups Projects
Commit ccbd86d8 authored by Demian Katz's avatar Demian Katz Committed by Robert Lange
Browse files

Expand log writer test coverage; fix bug and typo. (#1774)

parent 727530e4
Branches
Tags
No related merge requests found
......@@ -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]
);
}
/**
......
......@@ -30,7 +30,7 @@ namespace VuFindTest\Log;
use VuFind\Log\Logger;
/**
* Sitemap Test Class
* Logger Test Class
*
* @category VuFind
* @package Tests
......
<?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);
}
}
<?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);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment