diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Actions/ActionImplementationTest.php | 106 | ||||
-rw-r--r-- | tests/Actions/ListActionTest.php | 165 | ||||
-rw-r--r-- | tests/Bridges/BridgeImplementationTest.php | 451 | ||||
-rw-r--r-- | tests/Caches/CacheImplementationTest.php | 75 | ||||
-rw-r--r-- | tests/Formats/AtomFormatTest.php | 27 | ||||
-rw-r--r-- | tests/Formats/BaseFormatTest.php | 116 | ||||
-rw-r--r-- | tests/Formats/FormatImplementationTest.php | 67 | ||||
-rw-r--r-- | tests/Formats/JsonFormatTest.php | 27 | ||||
-rw-r--r-- | tests/Formats/MrssFormatTest.php | 27 |
9 files changed, 554 insertions, 507 deletions
diff --git a/tests/Actions/ActionImplementationTest.php b/tests/Actions/ActionImplementationTest.php index 0caf6d80..3f063682 100644 --- a/tests/Actions/ActionImplementationTest.php +++ b/tests/Actions/ActionImplementationTest.php @@ -5,54 +5,60 @@ namespace RssBridge\Tests\Actions; use ActionInterface; use PHPUnit\Framework\TestCase; -class ActionImplementationTest extends TestCase { - private $class; - private $obj; - - /** - * @dataProvider dataActionsProvider - */ - public function testClassName($path) { - $this->setAction($path); - $this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character'); - $this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces'); - $this->assertStringEndsWith('Action', $this->class, 'class name must end with "Action"'); - } - - /** - * @dataProvider dataActionsProvider - */ - public function testClassType($path) { - $this->setAction($path); - $this->assertInstanceOf(ActionInterface::class, $this->obj); - } - - /** - * @dataProvider dataActionsProvider - */ - public function testVisibleMethods($path) { - $allowedMethods = get_class_methods(ActionInterface::class); - sort($allowedMethods); - - $this->setAction($path); - - $methods = get_class_methods($this->obj); - sort($methods); - - $this->assertEquals($allowedMethods, $methods); - } - - public function dataActionsProvider() { - $actions = array(); - foreach (glob(PATH_LIB_ACTIONS . '*.php') as $path) { - $actions[basename($path, '.php')] = array($path); - } - return $actions; - } - - private function setAction($path) { - $this->class = '\\' . basename($path, '.php'); - $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); - $this->obj = new $this->class(); - } +class ActionImplementationTest extends TestCase +{ + private $class; + private $obj; + + /** + * @dataProvider dataActionsProvider + */ + public function testClassName($path) + { + $this->setAction($path); + $this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character'); + $this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces'); + $this->assertStringEndsWith('Action', $this->class, 'class name must end with "Action"'); + } + + /** + * @dataProvider dataActionsProvider + */ + public function testClassType($path) + { + $this->setAction($path); + $this->assertInstanceOf(ActionInterface::class, $this->obj); + } + + /** + * @dataProvider dataActionsProvider + */ + public function testVisibleMethods($path) + { + $allowedMethods = get_class_methods(ActionInterface::class); + sort($allowedMethods); + + $this->setAction($path); + + $methods = get_class_methods($this->obj); + sort($methods); + + $this->assertEquals($allowedMethods, $methods); + } + + public function dataActionsProvider() + { + $actions = []; + foreach (glob(PATH_LIB_ACTIONS . '*.php') as $path) { + $actions[basename($path, '.php')] = [$path]; + } + return $actions; + } + + private function setAction($path) + { + $this->class = '\\' . basename($path, '.php'); + $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); + $this->obj = new $this->class(); + } } diff --git a/tests/Actions/ListActionTest.php b/tests/Actions/ListActionTest.php index 1ecf50ed..2eb2049d 100644 --- a/tests/Actions/ListActionTest.php +++ b/tests/Actions/ListActionTest.php @@ -6,85 +6,88 @@ use ActionFactory; use BridgeFactory; use PHPUnit\Framework\TestCase; -class ListActionTest extends TestCase { - - private $data; - - /** - * @runInSeparateProcess - * @requires function xdebug_get_headers - */ - public function testHeaders() { - $this->initAction(); - - $this->assertContains( - 'Content-Type: application/json', - xdebug_get_headers() - ); - } - - /** - * @runInSeparateProcess - */ - public function testOutput() { - $this->initAction(); - - $items = json_decode($this->data, true); - - $this->assertNotNull($items, 'invalid JSON output: ' . json_last_error_msg()); - - $this->assertArrayHasKey('total', $items, 'Missing "total" parameter'); - $this->assertIsInt($items['total'], 'Invalid type'); - - $this->assertArrayHasKey('bridges', $items, 'Missing "bridges" array'); - - $this->assertEquals( - $items['total'], - count($items['bridges']), - 'Item count doesn\'t match' - ); - - $bridgeFac = new BridgeFactory(); - - $this->assertEquals( - count($bridgeFac->getBridgeNames()), - count($items['bridges']), - 'Number of bridges doesn\'t match' - ); - - $expectedKeys = array( - 'status', - 'uri', - 'name', - 'icon', - 'parameters', - 'maintainer', - 'description' - ); - - $allowedStatus = array( - 'active', - 'inactive' - ); - - foreach($items['bridges'] as $bridge) { - foreach($expectedKeys as $key) { - $this->assertArrayHasKey($key, $bridge, 'Missing key "' . $key . '"'); - } - - $this->assertContains($bridge['status'], $allowedStatus, 'Invalid status value'); - } - } - - private function initAction() { - $actionFac = new ActionFactory(); - - $action = $actionFac->create('list'); - - ob_start(); - $action->execute(); - $this->data = ob_get_contents(); - ob_clean(); - ob_end_flush(); - } +class ListActionTest extends TestCase +{ + private $data; + + /** + * @runInSeparateProcess + * @requires function xdebug_get_headers + */ + public function testHeaders() + { + $this->initAction(); + + $this->assertContains( + 'Content-Type: application/json', + xdebug_get_headers() + ); + } + + /** + * @runInSeparateProcess + */ + public function testOutput() + { + $this->initAction(); + + $items = json_decode($this->data, true); + + $this->assertNotNull($items, 'invalid JSON output: ' . json_last_error_msg()); + + $this->assertArrayHasKey('total', $items, 'Missing "total" parameter'); + $this->assertIsInt($items['total'], 'Invalid type'); + + $this->assertArrayHasKey('bridges', $items, 'Missing "bridges" array'); + + $this->assertEquals( + $items['total'], + count($items['bridges']), + 'Item count doesn\'t match' + ); + + $bridgeFac = new BridgeFactory(); + + $this->assertEquals( + count($bridgeFac->getBridgeNames()), + count($items['bridges']), + 'Number of bridges doesn\'t match' + ); + + $expectedKeys = [ + 'status', + 'uri', + 'name', + 'icon', + 'parameters', + 'maintainer', + 'description' + ]; + + $allowedStatus = [ + 'active', + 'inactive' + ]; + + foreach ($items['bridges'] as $bridge) { + foreach ($expectedKeys as $key) { + $this->assertArrayHasKey($key, $bridge, 'Missing key "' . $key . '"'); + } + + $this->assertContains($bridge['status'], $allowedStatus, 'Invalid status value'); + } + } + + private function initAction() + { + $actionFac = new ActionFactory(); + + $action = $actionFac->create('list'); + + ob_start(); + $action->execute(); + $this->data = ob_get_contents(); + ob_clean(); + ob_end_flush(); + } } diff --git a/tests/Bridges/BridgeImplementationTest.php b/tests/Bridges/BridgeImplementationTest.php index e0e095a6..60f94d4a 100644 --- a/tests/Bridges/BridgeImplementationTest.php +++ b/tests/Bridges/BridgeImplementationTest.php @@ -7,223 +7,236 @@ use BridgeInterface; use FeedExpander; use PHPUnit\Framework\TestCase; -class BridgeImplementationTest extends TestCase { - private $class; - private $obj; - - /** - * @dataProvider dataBridgesProvider - */ - public function testClassName($path) { - $this->setBridge($path); - $this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character'); - $this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces'); - $this->assertStringEndsWith('Bridge', $this->class, 'class name must end with "Bridge"'); - } - - /** - * @dataProvider dataBridgesProvider - */ - public function testClassType($path) { - $this->setBridge($path); - $this->assertInstanceOf(BridgeInterface::class, $this->obj); - } - - /** - * @dataProvider dataBridgesProvider - */ - public function testConstants($path) { - $this->setBridge($path); - - $this->assertIsString($this->obj::NAME, 'class::NAME'); - $this->assertNotEmpty($this->obj::NAME, 'class::NAME'); - $this->assertIsString($this->obj::URI, 'class::URI'); - $this->assertNotEmpty($this->obj::URI, 'class::URI'); - $this->assertIsString($this->obj::DESCRIPTION, 'class::DESCRIPTION'); - $this->assertNotEmpty($this->obj::DESCRIPTION, 'class::DESCRIPTION'); - $this->assertIsString($this->obj::MAINTAINER, 'class::MAINTAINER'); - $this->assertNotEmpty($this->obj::MAINTAINER, 'class::MAINTAINER'); - - $this->assertIsArray($this->obj::PARAMETERS, 'class::PARAMETERS'); - $this->assertIsInt($this->obj::CACHE_TIMEOUT, 'class::CACHE_TIMEOUT'); - $this->assertGreaterThanOrEqual(0, $this->obj::CACHE_TIMEOUT, 'class::CACHE_TIMEOUT'); - } - - /** - * @dataProvider dataBridgesProvider - */ - public function testParameters($path) { - $this->setBridge($path); - - $multiMinimum = 2; - if (isset($this->obj::PARAMETERS['global'])) { - ++$multiMinimum; - } - $multiContexts = (count($this->obj::PARAMETERS) >= $multiMinimum); - $paramsSeen = array(); - - $allowedTypes = array( - 'text', - 'number', - 'list', - 'checkbox' - ); - - foreach($this->obj::PARAMETERS as $context => $params) { - if ($multiContexts) { - $this->assertIsString($context, 'invalid context name'); - - $this->assertNotEmpty($context, 'The context name cannot be empty'); - } - - if (empty($params)) { - continue; - } - - foreach ($paramsSeen as $seen) { - $this->assertNotEquals($seen, $params, 'same set of parameters not allowed'); - } - $paramsSeen[] = $params; - - foreach ($params as $field => $options) { - $this->assertIsString($field, $field . ': invalid id'); - $this->assertNotEmpty($field, $field . ':empty id'); - - $this->assertIsString($options['name'], $field . ': invalid name'); - $this->assertNotEmpty($options['name'], $field . ': empty name'); - - if (isset($options['type'])) { - $this->assertIsString($options['type'], $field . ': invalid type'); - $this->assertContains($options['type'], $allowedTypes, $field . ': unknown type'); - - if ($options['type'] == 'list') { - $this->assertArrayHasKey('values', $options, $field . ': missing list values'); - $this->assertIsArray($options['values'], $field . ': invalid list values'); - $this->assertNotEmpty($options['values'], $field . ': empty list values'); - - foreach ($options['values'] as $valueName => $value) { - $this->assertIsString($valueName, $field . ': invalid value name'); - } - } - } - - if (isset($options['required'])) { - $this->assertIsBool($options['required'], $field . ': invalid required'); - - if($options['required'] === true && isset($options['type'])) { - switch($options['type']) { - case 'list': - case 'checkbox': - $this->assertArrayNotHasKey( - 'required', - $options, - $field . ': "required" attribute not supported for ' . $options['type'] - ); - break; - } - } - } - - if (isset($options['title'])) { - $this->assertIsString($options['title'], $field . ': invalid title'); - $this->assertNotEmpty($options['title'], $field . ': empty title'); - } - - if (isset($options['pattern'])) { - $this->assertIsString($options['pattern'], $field . ': invalid pattern'); - $this->assertNotEquals('', $options['pattern'], $field . ': empty pattern'); - } - - if (isset($options['exampleValue'])) { - if (is_string($options['exampleValue'])) - $this->assertNotEquals('', $options['exampleValue'], $field . ': empty exampleValue'); - } - - if (isset($options['defaultValue'])) { - if (is_string($options['defaultValue'])) - $this->assertNotEquals('', $options['defaultValue'], $field . ': empty defaultValue'); - } - } - } - - foreach($this->obj::TEST_DETECT_PARAMETERS as $url => $params) { - $this->assertEquals($this->obj->detectParameters($url), $params); - } - - $this->assertTrue(true); - } - - /** - * @dataProvider dataBridgesProvider - */ - public function testVisibleMethods($path) { - $allowedBridgeAbstract = get_class_methods(BridgeAbstract::class); - sort($allowedBridgeAbstract); - $allowedFeedExpander = get_class_methods(FeedExpander::class); - sort($allowedFeedExpander); - - $this->setBridge($path); - - $methods = get_class_methods($this->obj); - sort($methods); - if ($this->obj instanceof FeedExpander) { - $this->assertEquals($allowedFeedExpander, $methods); - } else { - $this->assertEquals($allowedBridgeAbstract, $methods); - } - } - - /** - * @dataProvider dataBridgesProvider - */ - public function testMethodValues($path) { - $this->setBridge($path); - - $value = $this->obj->getDescription(); - $this->assertIsString($value, '$class->getDescription()'); - $this->assertNotEmpty($value, '$class->getDescription()'); - - $value = $this->obj->getMaintainer(); - $this->assertIsString($value, '$class->getMaintainer()'); - $this->assertNotEmpty($value, '$class->getMaintainer()'); - - $value = $this->obj->getName(); - $this->assertIsString($value, '$class->getName()'); - $this->assertNotEmpty($value, '$class->getName()'); - - $value = $this->obj->getURI(); - $this->assertIsString($value, '$class->getURI()'); - $this->assertNotEmpty($value, '$class->getURI()'); - - $value = $this->obj->getIcon(); - $this->assertIsString($value, '$class->getIcon()'); - } - - /** - * @dataProvider dataBridgesProvider - */ - public function testUri($path) { - $this->setBridge($path); - - $this->checkUrl($this->obj::URI); - $this->checkUrl($this->obj->getURI()); - } - - public function dataBridgesProvider() { - $bridges = array(); - foreach (glob(PATH_LIB_BRIDGES . '*Bridge.php') as $path) { - $bridges[basename($path, '.php')] = array($path); - } - return $bridges; - } - - private function setBridge($path) { - $this->class = '\\' . basename($path, '.php'); - $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); - $this->obj = new $this->class(); - } - - private function checkUrl($url) { - $this->assertNotFalse(filter_var($url, FILTER_VALIDATE_URL), 'no valid URL: ' . $url); - } +class BridgeImplementationTest extends TestCase +{ + private $class; + private $obj; + + /** + * @dataProvider dataBridgesProvider + */ + public function testClassName($path) + { + $this->setBridge($path); + $this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character'); + $this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces'); + $this->assertStringEndsWith('Bridge', $this->class, 'class name must end with "Bridge"'); + } + + /** + * @dataProvider dataBridgesProvider + */ + public function testClassType($path) + { + $this->setBridge($path); + $this->assertInstanceOf(BridgeInterface::class, $this->obj); + } + + /** + * @dataProvider dataBridgesProvider + */ + public function testConstants($path) + { + $this->setBridge($path); + + $this->assertIsString($this->obj::NAME, 'class::NAME'); + $this->assertNotEmpty($this->obj::NAME, 'class::NAME'); + $this->assertIsString($this->obj::URI, 'class::URI'); + $this->assertNotEmpty($this->obj::URI, 'class::URI'); + $this->assertIsString($this->obj::DESCRIPTION, 'class::DESCRIPTION'); + $this->assertNotEmpty($this->obj::DESCRIPTION, 'class::DESCRIPTION'); + $this->assertIsString($this->obj::MAINTAINER, 'class::MAINTAINER'); + $this->assertNotEmpty($this->obj::MAINTAINER, 'class::MAINTAINER'); + + $this->assertIsArray($this->obj::PARAMETERS, 'class::PARAMETERS'); + $this->assertIsInt($this->obj::CACHE_TIMEOUT, 'class::CACHE_TIMEOUT'); + $this->assertGreaterThanOrEqual(0, $this->obj::CACHE_TIMEOUT, 'class::CACHE_TIMEOUT'); + } + + /** + * @dataProvider dataBridgesProvider + */ + public function testParameters($path) + { + $this->setBridge($path); + + $multiMinimum = 2; + if (isset($this->obj::PARAMETERS['global'])) { + ++$multiMinimum; + } + $multiContexts = (count($this->obj::PARAMETERS) >= $multiMinimum); + $paramsSeen = []; + + $allowedTypes = [ + 'text', + 'number', + 'list', + 'checkbox' + ]; + + foreach ($this->obj::PARAMETERS as $context => $params) { + if ($multiContexts) { + $this->assertIsString($context, 'invalid context name'); + + $this->assertNotEmpty($context, 'The context name cannot be empty'); + } + + if (empty($params)) { + continue; + } + + foreach ($paramsSeen as $seen) { + $this->assertNotEquals($seen, $params, 'same set of parameters not allowed'); + } + $paramsSeen[] = $params; + + foreach ($params as $field => $options) { + $this->assertIsString($field, $field . ': invalid id'); + $this->assertNotEmpty($field, $field . ':empty id'); + + $this->assertIsString($options['name'], $field . ': invalid name'); + $this->assertNotEmpty($options['name'], $field . ': empty name'); + + if (isset($options['type'])) { + $this->assertIsString($options['type'], $field . ': invalid type'); + $this->assertContains($options['type'], $allowedTypes, $field . ': unknown type'); + + if ($options['type'] == 'list') { + $this->assertArrayHasKey('values', $options, $field . ': missing list values'); + $this->assertIsArray($options['values'], $field . ': invalid list values'); + $this->assertNotEmpty($options['values'], $field . ': empty list values'); + + foreach ($options['values'] as $valueName => $value) { + $this->assertIsString($valueName, $field . ': invalid value name'); + } + } + } + + if (isset($options['required'])) { + $this->assertIsBool($options['required'], $field . ': invalid required'); + + if ($options['required'] === true && isset($options['type'])) { + switch ($options['type']) { + case 'list': + case 'checkbox': + $this->assertArrayNotHasKey( + 'required', + $options, + $field . ': "required" attribute not supported for ' . $options['type'] + ); + break; + } + } + } + + if (isset($options['title'])) { + $this->assertIsString($options['title'], $field . ': invalid title'); + $this->assertNotEmpty($options['title'], $field . ': empty title'); + } + + if (isset($options['pattern'])) { + $this->assertIsString($options['pattern'], $field . ': invalid pattern'); + $this->assertNotEquals('', $options['pattern'], $field . ': empty pattern'); + } + + if (isset($options['exampleValue'])) { + if (is_string($options['exampleValue'])) { + $this->assertNotEquals('', $options['exampleValue'], $field . ': empty exampleValue'); + } + } + + if (isset($options['defaultValue'])) { + if (is_string($options['defaultValue'])) { + $this->assertNotEquals('', $options['defaultValue'], $field . ': empty defaultValue'); + } + } + } + } + + foreach ($this->obj::TEST_DETECT_PARAMETERS as $url => $params) { + $this->assertEquals($this->obj->detectParameters($url), $params); + } + + $this->assertTrue(true); + } + + /** + * @dataProvider dataBridgesProvider + */ + public function testVisibleMethods($path) + { + $allowedBridgeAbstract = get_class_methods(BridgeAbstract::class); + sort($allowedBridgeAbstract); + $allowedFeedExpander = get_class_methods(FeedExpander::class); + sort($allowedFeedExpander); + + $this->setBridge($path); + + $methods = get_class_methods($this->obj); + sort($methods); + if ($this->obj instanceof FeedExpander) { + $this->assertEquals($allowedFeedExpander, $methods); + } else { + $this->assertEquals($allowedBridgeAbstract, $methods); + } + } + + /** + * @dataProvider dataBridgesProvider + */ + public function testMethodValues($path) + { + $this->setBridge($path); + + $value = $this->obj->getDescription(); + $this->assertIsString($value, '$class->getDescription()'); + $this->assertNotEmpty($value, '$class->getDescription()'); + + $value = $this->obj->getMaintainer(); + $this->assertIsString($value, '$class->getMaintainer()'); + $this->assertNotEmpty($value, '$class->getMaintainer()'); + + $value = $this->obj->getName(); + $this->assertIsString($value, '$class->getName()'); + $this->assertNotEmpty($value, '$class->getName()'); + + $value = $this->obj->getURI(); + $this->assertIsString($value, '$class->getURI()'); + $this->assertNotEmpty($value, '$class->getURI()'); + + $value = $this->obj->getIcon(); + $this->assertIsString($value, '$class->getIcon()'); + } + + /** + * @dataProvider dataBridgesProvider + */ + public function testUri($path) + { + $this->setBridge($path); + + $this->checkUrl($this->obj::URI); + $this->checkUrl($this->obj->getURI()); + } + + public function dataBridgesProvider() + { + $bridges = []; + foreach (glob(PATH_LIB_BRIDGES . '*Bridge.php') as $path) { + $bridges[basename($path, '.php')] = [$path]; + } + return $bridges; + } + + private function setBridge($path) + { + $this->class = '\\' . basename($path, '.php'); + $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); + $this->obj = new $this->class(); + } + + private function checkUrl($url) + { + $this->assertNotFalse(filter_var($url, FILTER_VALIDATE_URL), 'no valid URL: ' . $url); + } } diff --git a/tests/Caches/CacheImplementationTest.php b/tests/Caches/CacheImplementationTest.php index 12018685..a3ad5f79 100644 --- a/tests/Caches/CacheImplementationTest.php +++ b/tests/Caches/CacheImplementationTest.php @@ -5,39 +5,44 @@ namespace RssBridge\Tests\Caches; use CacheInterface; use PHPUnit\Framework\TestCase; -class CacheImplementationTest extends TestCase { - private $class; - - /** - * @dataProvider dataCachesProvider - */ - public function testClassName($path) { - $this->setCache($path); - $this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character'); - $this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces'); - $this->assertStringEndsWith('Cache', $this->class, 'class name must end with "Cache"'); - } - - /** - * @dataProvider dataCachesProvider - */ - public function testClassType($path) { - $this->setCache($path); - $this->assertTrue(is_subclass_of($this->class, CacheInterface::class), 'class must be subclass of CacheInterface'); - } - - //////////////////////////////////////////////////////////////////////////// - - public function dataCachesProvider() { - $caches = array(); - foreach (glob(PATH_LIB_CACHES . '*.php') as $path) { - $caches[basename($path, '.php')] = array($path); - } - return $caches; - } - - private function setCache($path) { - $this->class = '\\' . basename($path, '.php'); - $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); - } +class CacheImplementationTest extends TestCase +{ + private $class; + + /** + * @dataProvider dataCachesProvider + */ + public function testClassName($path) + { + $this->setCache($path); + $this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character'); + $this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces'); + $this->assertStringEndsWith('Cache', $this->class, 'class name must end with "Cache"'); + } + + /** + * @dataProvider dataCachesProvider + */ + public function testClassType($path) + { + $this->setCache($path); + $this->assertTrue(is_subclass_of($this->class, CacheInterface::class), 'class must be subclass of CacheInterface'); + } + + //////////////////////////////////////////////////////////////////////////// + + public function dataCachesProvider() + { + $caches = []; + foreach (glob(PATH_LIB_CACHES . '*.php') as $path) { + $caches[basename($path, '.php')] = [$path]; + } + return $caches; + } + + private function setCache($path) + { + $this->class = '\\' . basename($path, '.php'); + $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); + } } diff --git a/tests/Formats/AtomFormatTest.php b/tests/Formats/AtomFormatTest.php index a871ea86..77bb9cbc 100644 --- a/tests/Formats/AtomFormatTest.php +++ b/tests/Formats/AtomFormatTest.php @@ -1,4 +1,5 @@ <?php + /** * AtomFormat - RFC 4287: The Atom Syndication Format * https://tools.ietf.org/html/rfc4287 @@ -10,18 +11,20 @@ require_once __DIR__ . '/BaseFormatTest.php'; use PHPUnit\Framework\TestCase; -class AtomFormatTest extends BaseFormatTest { - private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedAtomFormat/'; +class AtomFormatTest extends BaseFormatTest +{ + private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedAtomFormat/'; - /** - * @dataProvider sampleProvider - * @runInSeparateProcess - */ - public function testOutput(string $name, string $path) { - $data = $this->formatData('Atom', $this->loadSample($path)); - $this->assertNotFalse(simplexml_load_string($data)); + /** + * @dataProvider sampleProvider + * @runInSeparateProcess + */ + public function testOutput(string $name, string $path) + { + $data = $this->formatData('Atom', $this->loadSample($path)); + $this->assertNotFalse(simplexml_load_string($data)); - $expected = self::PATH_EXPECTED . $name . '.xml'; - $this->assertXmlStringEqualsXmlFile($expected, $data); - } + $expected = self::PATH_EXPECTED . $name . '.xml'; + $this->assertXmlStringEqualsXmlFile($expected, $data); + } } diff --git a/tests/Formats/BaseFormatTest.php b/tests/Formats/BaseFormatTest.php index 94da7b04..ace4d3ea 100644 --- a/tests/Formats/BaseFormatTest.php +++ b/tests/Formats/BaseFormatTest.php @@ -5,59 +5,65 @@ namespace RssBridge\Tests\Formats; use PHPUnit\Framework\TestCase; use FormatFactory; -abstract class BaseFormatTest extends TestCase { - protected const PATH_SAMPLES = __DIR__ . '/samples/'; - - /** - * @return array<string, array{string, string}> - */ - public function sampleProvider() { - $samples = []; - foreach (glob(self::PATH_SAMPLES . '*.json') as $path) { - $name = basename($path, '.json'); - $samples[$name] = [ - $name, - $path, - ]; - } - return $samples; - } - - /** - * Cannot be part of the sample returned by sampleProvider since this modifies $_SERVER - * and thus needs to be run in a separate process to avoid side effects. - */ - protected function loadSample(string $path): \stdClass { - $data = json_decode(file_get_contents($path), true); - if (isset($data['meta']) && isset($data['items'])) { - if (!empty($data['server'])) - $this->setServerVars($data['server']); - - $items = array(); - foreach($data['items'] as $item) { - $items[] = new \FeedItem($item); - } - - return (object)array( - 'meta' => $data['meta'], - 'items' => $items, - ); - } else { - $this->fail('invalid test sample: ' . basename($path, '.json')); - } - } - - private function setServerVars(array $list): void { - $_SERVER = array_merge($_SERVER, $list); - } - - protected function formatData(string $formatName, \stdClass $sample): string { - $formatFac = new FormatFactory(); - $format = $formatFac->create($formatName); - $format->setItems($sample->items); - $format->setExtraInfos($sample->meta); - $format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); - - return $format->stringify(); - } +abstract class BaseFormatTest extends TestCase +{ + protected const PATH_SAMPLES = __DIR__ . '/samples/'; + + /** + * @return array<string, array{string, string}> + */ + public function sampleProvider() + { + $samples = []; + foreach (glob(self::PATH_SAMPLES . '*.json') as $path) { + $name = basename($path, '.json'); + $samples[$name] = [ + $name, + $path, + ]; + } + return $samples; + } + + /** + * Cannot be part of the sample returned by sampleProvider since this modifies $_SERVER + * and thus needs to be run in a separate process to avoid side effects. + */ + protected function loadSample(string $path): \stdClass + { + $data = json_decode(file_get_contents($path), true); + if (isset($data['meta']) && isset($data['items'])) { + if (!empty($data['server'])) { + $this->setServerVars($data['server']); + } + + $items = []; + foreach ($data['items'] as $item) { + $items[] = new \FeedItem($item); + } + + return (object)[ + 'meta' => $data['meta'], + 'items' => $items, + ]; + } else { + $this->fail('invalid test sample: ' . basename($path, '.json')); + } + } + + private function setServerVars(array $list): void + { + $_SERVER = array_merge($_SERVER, $list); + } + + protected function formatData(string $formatName, \stdClass $sample): string + { + $formatFac = new FormatFactory(); + $format = $formatFac->create($formatName); + $format->setItems($sample->items); + $format->setExtraInfos($sample->meta); + $format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); + + return $format->stringify(); + } } diff --git a/tests/Formats/FormatImplementationTest.php b/tests/Formats/FormatImplementationTest.php index e4501d68..55c6335f 100644 --- a/tests/Formats/FormatImplementationTest.php +++ b/tests/Formats/FormatImplementationTest.php @@ -2,39 +2,44 @@ use PHPUnit\Framework\TestCase; -class FormatImplementationTest extends TestCase { - private $class; - private $obj; +class FormatImplementationTest extends TestCase +{ + private $class; + private $obj; - /** - * @dataProvider dataFormatsProvider - */ - public function testClassName($path) { - $this->setFormat($path); - $this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character'); - $this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces'); - $this->assertStringEndsWith('Format', $this->class, 'class name must end with "Format"'); - } + /** + * @dataProvider dataFormatsProvider + */ + public function testClassName($path) + { + $this->setFormat($path); + $this->assertTrue($this->class === ucfirst($this->class), 'class name must start with uppercase character'); + $this->assertEquals(0, substr_count($this->class, ' '), 'class name must not contain spaces'); + $this->assertStringEndsWith('Format', $this->class, 'class name must end with "Format"'); + } - /** - * @dataProvider dataFormatsProvider - */ - public function testClassType($path) { - $this->setFormat($path); - $this->assertInstanceOf(FormatInterface::class, $this->obj); - } + /** + * @dataProvider dataFormatsProvider + */ + public function testClassType($path) + { + $this->setFormat($path); + $this->assertInstanceOf(FormatInterface::class, $this->obj); + } - public function dataFormatsProvider() { - $formats = array(); - foreach (glob(PATH_LIB_FORMATS . '*.php') as $path) { - $formats[basename($path, '.php')] = array($path); - } - return $formats; - } + public function dataFormatsProvider() + { + $formats = []; + foreach (glob(PATH_LIB_FORMATS . '*.php') as $path) { + $formats[basename($path, '.php')] = [$path]; + } + return $formats; + } - private function setFormat($path) { - $this->class = basename($path, '.php'); - $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); - $this->obj = new $this->class(); - } + private function setFormat($path) + { + $this->class = basename($path, '.php'); + $this->assertTrue(class_exists($this->class), 'class ' . $this->class . ' doesn\'t exist'); + $this->obj = new $this->class(); + } } diff --git a/tests/Formats/JsonFormatTest.php b/tests/Formats/JsonFormatTest.php index 3b9f8d47..c21d3f34 100644 --- a/tests/Formats/JsonFormatTest.php +++ b/tests/Formats/JsonFormatTest.php @@ -1,4 +1,5 @@ <?php + /** * JsonFormat - JSON Feed Version 1 * https://jsonfeed.org/version/1 @@ -10,18 +11,20 @@ require_once __DIR__ . '/BaseFormatTest.php'; use PHPUnit\Framework\TestCase; -class JsonFormatTest extends BaseFormatTest { - private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedJsonFormat/'; +class JsonFormatTest extends BaseFormatTest +{ + private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedJsonFormat/'; - /** - * @dataProvider sampleProvider - * @runInSeparateProcess - */ - public function testOutput(string $name, string $path) { - $data = $this->formatData('Json', $this->loadSample($path)); - $this->assertNotNull(json_decode($data), 'invalid JSON output: ' . json_last_error_msg()); + /** + * @dataProvider sampleProvider + * @runInSeparateProcess + */ + public function testOutput(string $name, string $path) + { + $data = $this->formatData('Json', $this->loadSample($path)); + $this->assertNotNull(json_decode($data), 'invalid JSON output: ' . json_last_error_msg()); - $expected = self::PATH_EXPECTED . $name . '.json'; - $this->assertJsonStringEqualsJsonFile($expected, $data); - } + $expected = self::PATH_EXPECTED . $name . '.json'; + $this->assertJsonStringEqualsJsonFile($expected, $data); + } } diff --git a/tests/Formats/MrssFormatTest.php b/tests/Formats/MrssFormatTest.php index 6def6afb..af74923e 100644 --- a/tests/Formats/MrssFormatTest.php +++ b/tests/Formats/MrssFormatTest.php @@ -1,4 +1,5 @@ <?php + /** * MrssFormat - RSS 2.0 + Media RSS * http://www.rssboard.org/rss-specification @@ -11,18 +12,20 @@ require_once __DIR__ . '/BaseFormatTest.php'; use PHPUnit\Framework\TestCase; -class MrssFormatTest extends BaseFormatTest { - private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedMrssFormat/'; +class MrssFormatTest extends BaseFormatTest +{ + private const PATH_EXPECTED = self::PATH_SAMPLES . 'expectedMrssFormat/'; - /** - * @dataProvider sampleProvider - * @runInSeparateProcess - */ - public function testOutput(string $name, string $path) { - $data = $this->formatData('Mrss', $this->loadSample($path)); - $this->assertNotFalse(simplexml_load_string($data)); + /** + * @dataProvider sampleProvider + * @runInSeparateProcess + */ + public function testOutput(string $name, string $path) + { + $data = $this->formatData('Mrss', $this->loadSample($path)); + $this->assertNotFalse(simplexml_load_string($data)); - $expected = self::PATH_EXPECTED . $name . '.xml'; - $this->assertXmlStringEqualsXmlFile($expected, $data); - } + $expected = self::PATH_EXPECTED . $name . '.xml'; + $this->assertXmlStringEqualsXmlFile($expected, $data); + } } |