2018-08-05 01:39:53 +03:00
|
|
|
<?php
|
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
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"');
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataBridgesProvider
|
|
|
|
*/
|
|
|
|
public function testClassType($path) {
|
|
|
|
$this->setBridge($path);
|
|
|
|
$this->assertInstanceOf(BridgeInterface::class, $this->obj);
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataBridgesProvider
|
|
|
|
*/
|
|
|
|
public function testConstants($path) {
|
|
|
|
$this->setBridge($path);
|
|
|
|
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($this->obj::NAME, 'class::NAME');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($this->obj::NAME, 'class::NAME');
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($this->obj::URI, 'class::URI');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($this->obj::URI, 'class::URI');
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($this->obj::DESCRIPTION, 'class::DESCRIPTION');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($this->obj::DESCRIPTION, 'class::DESCRIPTION');
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($this->obj::MAINTAINER, 'class::MAINTAINER');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($this->obj::MAINTAINER, 'class::MAINTAINER');
|
|
|
|
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsArray($this->obj::PARAMETERS, 'class::PARAMETERS');
|
|
|
|
$this->assertIsInt($this->obj::CACHE_TIMEOUT, 'class::CACHE_TIMEOUT');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertGreaterThanOrEqual(0, $this->obj::CACHE_TIMEOUT, 'class::CACHE_TIMEOUT');
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataBridgesProvider
|
|
|
|
*/
|
|
|
|
public function testParameters($path) {
|
|
|
|
$this->setBridge($path);
|
|
|
|
|
|
|
|
$multiMinimum = 2;
|
2022-04-13 22:04:10 +03:00
|
|
|
if (isset($this->obj::PARAMETERS['global'])) {
|
|
|
|
++$multiMinimum;
|
|
|
|
}
|
2019-01-08 22:02:51 +03:00
|
|
|
$multiContexts = (count($this->obj::PARAMETERS) >= $multiMinimum);
|
|
|
|
$paramsSeen = array();
|
|
|
|
|
|
|
|
$allowedTypes = array(
|
|
|
|
'text',
|
|
|
|
'number',
|
|
|
|
'list',
|
|
|
|
'checkbox'
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach($this->obj::PARAMETERS as $context => $params) {
|
|
|
|
if ($multiContexts) {
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($context, 'invalid context name');
|
2022-05-08 04:55:24 +03:00
|
|
|
|
|
|
|
// Pixiv has a context that is the empty string, for BC.
|
|
|
|
if (! ($this->obj instanceof PixivBridge)) {
|
|
|
|
$this->assertNotEmpty($context, 'The context name cannot be empty');
|
|
|
|
}
|
2019-01-08 22:02:51 +03:00
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2021-08-10 20:37:41 +03:00
|
|
|
if (empty($params)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
foreach ($paramsSeen as $seen) {
|
|
|
|
$this->assertNotEquals($seen, $params, 'same set of parameters not allowed');
|
|
|
|
}
|
|
|
|
$paramsSeen[] = $params;
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
foreach ($params as $field => $options) {
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($field, $field . ': invalid id');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($field, $field . ':empty id');
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($options['name'], $field . ': invalid name');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($options['name'], $field . ': empty name');
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
if (isset($options['type'])) {
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($options['type'], $field . ': invalid type');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertContains($options['type'], $allowedTypes, $field . ': unknown type');
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
if ($options['type'] == 'list') {
|
|
|
|
$this->assertArrayHasKey('values', $options, $field . ': missing list values');
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsArray($options['values'], $field . ': invalid list values');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($options['values'], $field . ': empty list values');
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
foreach ($options['values'] as $valueName => $value) {
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($valueName, $field . ': invalid value name');
|
2019-01-08 22:02:51 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
if (isset($options['required'])) {
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsBool($options['required'], $field . ': invalid required');
|
2019-01-22 20:12:34 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 22:02:51 +03:00
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
if (isset($options['title'])) {
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($options['title'], $field . ': invalid title');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($options['title'], $field . ': empty title');
|
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
if (isset($options['pattern'])) {
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($options['pattern'], $field . ': invalid pattern');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEquals('', $options['pattern'], $field . ': empty pattern');
|
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
if (isset($options['exampleValue'])) {
|
|
|
|
if (is_string($options['exampleValue']))
|
|
|
|
$this->assertNotEquals('', $options['exampleValue'], $field . ': empty exampleValue');
|
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
if (isset($options['defaultValue'])) {
|
|
|
|
if (is_string($options['defaultValue']))
|
|
|
|
$this->assertNotEquals('', $options['defaultValue'], $field . ': empty defaultValue');
|
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 07:19:07 +03:00
|
|
|
foreach($this->obj::TEST_DETECT_PARAMETERS as $url => $params) {
|
|
|
|
$this->assertEquals($this->obj->detectParameters($url), $params);
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertTrue(true);
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
/**
|
|
|
|
* @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);
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|
2019-01-08 22:02:51 +03:00
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataBridgesProvider
|
|
|
|
*/
|
|
|
|
public function testMethodValues($path) {
|
|
|
|
$this->setBridge($path);
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
$value = $this->obj->getDescription();
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($value, '$class->getDescription()');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($value, '$class->getDescription()');
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
$value = $this->obj->getMaintainer();
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($value, '$class->getMaintainer()');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($value, '$class->getMaintainer()');
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
$value = $this->obj->getName();
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($value, '$class->getName()');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($value, '$class->getName()');
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
$value = $this->obj->getURI();
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($value, '$class->getURI()');
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->assertNotEmpty($value, '$class->getURI()');
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
$value = $this->obj->getIcon();
|
2021-12-03 02:12:16 +03:00
|
|
|
$this->assertIsString($value, '$class->getIcon()');
|
2019-01-08 22:02:51 +03:00
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataBridgesProvider
|
|
|
|
*/
|
|
|
|
public function testUri($path) {
|
|
|
|
$this->setBridge($path);
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
$this->checkUrl($this->obj::URI);
|
|
|
|
$this->checkUrl($this->obj->getURI());
|
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
public function dataBridgesProvider() {
|
|
|
|
$bridges = array();
|
|
|
|
foreach (glob(PATH_LIB_BRIDGES . '*.php') as $path) {
|
|
|
|
$bridges[basename($path, '.php')] = array($path);
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|
2019-01-08 22:02:51 +03:00
|
|
|
return $bridges;
|
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
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();
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:02:51 +03:00
|
|
|
private function checkUrl($url) {
|
|
|
|
$this->assertNotFalse(filter_var($url, FILTER_VALIDATE_URL), 'no valid URL: ' . $url);
|
|
|
|
}
|
2018-08-05 01:39:53 +03:00
|
|
|
}
|