tests: Use PSR-4-style namespaces (#2778)

We cannot yet switch to namespaces for RSS-Bridge itself but for tests we are not limited by BC.
It does not actually do anything since PHPUnit will search for the test files without the help of the autoloader but it still makes the directory cleaner.
This commit is contained in:
Jan Tojnar 2022-06-07 23:22:33 +02:00 committed by GitHub
parent 90d22f0d80
commit 44e8007d9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 34 additions and 4 deletions

View file

@ -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();
}
}

View file

@ -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();
}
}