aboutsummaryrefslogtreecommitdiff
path: root/tests/Actions
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Actions')
-rw-r--r--tests/Actions/ActionImplementationTest.php59
-rw-r--r--tests/Actions/ListActionTest.php92
2 files changed, 151 insertions, 0 deletions
diff --git a/tests/Actions/ActionImplementationTest.php b/tests/Actions/ActionImplementationTest.php
new file mode 100644
index 00000000..b10d44e5
--- /dev/null
+++ b/tests/Actions/ActionImplementationTest.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace RssBridge\Tests\Actions;
+
+use ActionAbstract;
+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) {
+ $allowedActionAbstract = get_class_methods(ActionAbstract::class);
+ sort($allowedActionAbstract);
+
+ $this->setAction($path);
+
+ $methods = get_class_methods($this->obj);
+ sort($methods);
+
+ $this->assertEquals($allowedActionAbstract, $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();
+ }
+}
diff --git a/tests/Actions/ListActionTest.php b/tests/Actions/ListActionTest.php
new file mode 100644
index 00000000..e5ef9570
--- /dev/null
+++ b/tests/Actions/ListActionTest.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace RssBridge\Tests\Actions;
+
+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();
+ $actionFac->setWorkingDir(PATH_LIB_ACTIONS);
+
+ $action = $actionFac->create('list');
+ $action->setUserData(array()); /* no user data required */
+
+ ob_start();
+ $action->execute();
+ $this->data = ob_get_contents();
+ ob_clean();
+ ob_end_flush();
+ }
+}