created mocks for testing

This commit is contained in:
Guillermo Dev
2018-10-13 18:00:51 +02:00
parent e53debb98d
commit 1211af09cc
26 changed files with 412 additions and 118 deletions

View File

@@ -1,78 +1,13 @@
<?php
namespace BSR\Webservice\Formatter;
abstract class Formatter {
private static $formats = array();
use PHPUnit\Framework\TestCase;
/**
* @param array $formats New available formats, array(mimetype => class)
*/
protected static function registerFormats(array $formats) {
foreach($formats as $f) {
self::$formats[$f] = get_called_class();
}
class FormatterTest extends TestCase
{
public function testFormatterAllwaysReturnsAJsonFormatter()
{
$formatter = Formatter::getFormatter();
$this->assertEquals($formatter instanceof Json, true);
}
/**
* @return Formatter The formatter to use for this request
*/
public static function getFormatter() {
self::loadFormatters();
$format = self::getFormatFromHeader();
return new $format();
}
/**
* Load all formatters in the current directory
*/
private static function loadFormatters() {
preg_match('/(.+)\\\([a-zA-Z0-9]+)/', get_called_class(), $parts);
$us = $parts[2];
$namespace = $parts[1];
$base = __DIR__.'/';
$ext = '.php';
$files = glob(sprintf('%s%s%s', $base, '*', $ext));
foreach($files as $f) {
$c = str_replace(array($base, $ext), '', $f);
if($c !== $us) {
$c = $namespace.'\\'.$c;
call_user_func(array($c, 'init'));
}
}
}
/**
* @return string The class name to instantiate in accord to the Accept header
*/
private static function getFormatFromHeader() {
//TODO this is ugly
return 'BSR\Webservice\Formatter\Json';
if(isset($_SERVER['HTTP_ACCEPT'])) {
$formats = array_map(function($f) {
$parts = explode(';', $f);
$parts[1] = (isset($parts[1]) ? (float) preg_replace('/[^0-9\.]/', '', $parts[1]) : 1.0) * 100;
return $parts;
}, explode(',', $_SERVER['HTTP_ACCEPT']));
usort($formats, function($a, $b) { return $b[1] - $a[1]; });
foreach($formats as $f) {
if(isset(self::$formats[$f[0]])) {
return self::$formats[$f[0]];
}
}
}
return 'BSR\Webservice\Formatter\Json';
}
/**
* Output the content for the given data
* @param array $data
*/
abstract public function render($data);
}

View File

@@ -1,17 +1,34 @@
<?php
namespace BSR\Webservice\Formatter;
class Json extends Formatter {
protected static function init() {
self::registerFormats(array(
'application/json',
'application/x-json',
));
use PHPUnit\Framework\TestCase;
class JsonTest extends TestCase
{
protected $accept = array(
'application/json',
'application/x-json',
);
public function testRenderEncodesDataIntoJsonFormat()
{
$format = new Json();
$someArray = array(
'asdf' => array(1, 'a', 'b', array()),
'1' => array(3 => 'c'),
0 => 'hello',
);
ob_start();
$format->render($someArray);
$jsonRepresentation = ob_get_clean();
$this->assertSame(json_decode($jsonRepresentation, true), $someArray);
}
public function render($data) {
echo json_encode($data);
public function testRegistersItsFormatAcceptHeader()
{
Formatter::getFormatter();
$formats = Formatter::getRegisterdFormats();
$this->assertSame(array_intersect($this->accept, array_keys($formats)), $this->accept);
}
}