Skip to content

Commit

Permalink
rename AbstractMatchResultMatchResult (#26)
Browse files Browse the repository at this point in the history
add factory method to create `MatchResult`s from id
added tests
  • Loading branch information
untone-survive authored Oct 12, 2023
1 parent 8e74858 commit 5d8bb6c
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/MatchResult/CakePHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class CakePHP extends AbstractMatchResult
class CakePHP extends MatchResult
{
public const ID = 'cakephp';
public const NAME = 'CakePHP';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class CodeIgniter extends AbstractMatchResult
class CodeIgniter extends MatchResult
{
public const ID = 'codeigniter';
public const NAME = 'CodeIgniter';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Composer extends AbstractMatchResult
class Composer extends MatchResult
{
public const ID = 'composer';
public const NAME = 'Composer';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/DotNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class DotNet extends AbstractMatchResult
class DotNet extends MatchResult
{
public const ID = 'dotnet';
public const NAME = '.NET';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Drupal extends AbstractMatchResult
class Drupal extends MatchResult
{
public const ID = 'drupal';
public const NAME = 'Drupal';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Joomla.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Joomla extends AbstractMatchResult
class Joomla extends MatchResult
{
public const ID = 'joomla';
public const NAME = 'Joomla!';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Laravel extends AbstractMatchResult
class Laravel extends MatchResult
{
public const ID = 'laravel';
public const NAME = 'Laravel';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use League\Flysystem\PathTraversalDetected;
use League\Flysystem\WhitespacePathNormalizer;

abstract class AbstractMatchResult implements MatchResultInterface, JsonSerializable
class MatchResult implements MatchResultInterface, JsonSerializable
{
public const ID = null;
public const NAME = null;
Expand Down Expand Up @@ -61,4 +61,37 @@ public function jsonSerialize(): array
'application' => $this->getApplication(),
];
}

public static function createById(
string $id,
?string $path = null,
?string $version = null,
?string $application = null
): MatchResultInterface {
$classname = match ($id) {
CakePHP::ID => CakePHP::class,
CodeIgniter::ID => CodeIgniter::class,
Composer::ID => Composer::class,
DotNet::ID => DotNet::class,
Drupal::ID => Drupal::class,
Joomla::ID => Joomla::class,
Laravel::ID => Laravel::class,
NodeJs::ID => NodeJs::class,
Php::ID => Php::class,
Prestashop::ID => Prestashop::class,
Python::ID => Python::class,
Ruby::ID => Ruby::class,
Symfony::ID => Symfony::class,
Typo3::ID => Typo3::class,
Wordpress::ID => Wordpress::class,
Yii::ID => Yii::class,
default => null,
};

if (!$classname) {
return new EmptyMatchResult();
}

return new $classname(path: $path ?? '', version: $version, application: $application);
}
}
2 changes: 1 addition & 1 deletion src/MatchResult/NodeJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class NodeJs extends AbstractMatchResult
class NodeJs extends MatchResult
{
public const ID = 'nodejs';
public const NAME = 'Node.js';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Php extends AbstractMatchResult
class Php extends MatchResult
{
public const ID = 'php';
public const NAME = 'PHP';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Prestashop.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Prestashop extends AbstractMatchResult
class Prestashop extends MatchResult
{
public const ID = 'prestashop';
public const NAME = 'PrestaShop';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Python extends AbstractMatchResult
class Python extends MatchResult
{
public const ID = 'python';
public const NAME = 'Python';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Ruby.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Ruby extends AbstractMatchResult
class Ruby extends MatchResult
{
public const ID = 'ruby';
public const NAME = 'Ruby';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Symfony extends AbstractMatchResult
class Symfony extends MatchResult
{
public const ID = 'symfony';
public const NAME = 'Symfony';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Typo3.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Typo3 extends AbstractMatchResult
class Typo3 extends MatchResult
{
public const ID = 'typo3';
public const NAME = 'TYPO3';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Wordpress extends AbstractMatchResult
class Wordpress extends MatchResult
{
public const ID = 'wordpress';
public const NAME = 'WordPress';
Expand Down
2 changes: 1 addition & 1 deletion src/MatchResult/Yii.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Plesk\Wappspector\MatchResult;

class Yii extends AbstractMatchResult
class Yii extends MatchResult
{
public const ID = 'yii';
public const NAME = 'Yii';
Expand Down
46 changes: 43 additions & 3 deletions tests/MatchResult/MatchResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Plesk\Wappspector\MatchResult\AbstractMatchResult;
use Plesk\Wappspector\MatchResult\EmptyMatchResult;
use Plesk\Wappspector\MatchResult\MatchResult;
use Plesk\Wappspector\MatchResult\Php;
use Plesk\Wappspector\MatchResult\Python;
use Plesk\Wappspector\MatchResult\Wordpress;

#[CoversClass(AbstractMatchResult::class)]
#[CoversClass(MatchResult::class)]
class MatchResultTest extends TestCase
{
/**
* @dataProvider pathDataProvider
*/
public function testParentDirNormalization($originalPath, $normalizedPath): void
{
$matchResult = new class ($originalPath) extends AbstractMatchResult {
$matchResult = new class ($originalPath) extends MatchResult {
public function getId(): string
{
return 'mock';
Expand Down Expand Up @@ -47,4 +51,40 @@ public static function pathDataProvider(): array
['/parent/../../', '/'],
];
}

public function testFactoryMethodEmptyResult(): void
{
$this->assertInstanceOf(EmptyMatchResult::class, MatchResult::createById('someunknownid'));
}

/**
* @dataProvider idsProvider
*/
public function testFactoryMethod(string $id, string $classname): void
{
$this->assertInstanceOf($classname, MatchResult::createById($id));
}

public static function idsProvider(): array
{
return [
[Wordpress::ID, Wordpress::class],
[Php::ID, Php::class],
[Python::ID, Python::class],
];
}

public function testFactoryMethodArgs(): void
{
$args = [
'path' => 'var/www',
'version' => '1.2.3',
'application' => 'My App',
];
$result = MatchResult::createById('php', ...$args);
$this->assertInstanceOf(Php::class, $result);
$this->assertEquals($args['path'], $result->getPath());
$this->assertEquals($args['version'], $result->getVersion());
$this->assertEquals($args['application'], $result->getApplication());
}
}
4 changes: 2 additions & 2 deletions tests/Matchers/UpLevelMatcherTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Plesk\Wappspector\FileSystemFactory;
use Plesk\Wappspector\Matchers\MatcherInterface;
use Plesk\Wappspector\Matchers\UpLevelMatcherTrait;
use Plesk\Wappspector\MatchResult\AbstractMatchResult;
use Plesk\Wappspector\MatchResult\EmptyMatchResult;
use Plesk\Wappspector\MatchResult\MatchResult;
use Plesk\Wappspector\MatchResult\MatchResultInterface;

#[CoversClass(UpLevelMatcherTrait::class)]
Expand Down Expand Up @@ -48,7 +48,7 @@ protected function doMatch(Filesystem $fs, string $path): MatchResultInterface
foreach ($list as $item) {
/** @var StorageAttributes $item */
if ($item->isFile() && str_ends_with($item->path(), '.md')) {
return new class ($path) extends AbstractMatchResult {
return new class ($path) extends MatchResult {
public function getId(): string
{
return 'markdown';
Expand Down

0 comments on commit 5d8bb6c

Please sign in to comment.