Added tests for LabelPlaceholder Providers.

This commit is contained in:
Jan Böhmer 2020-04-15 12:48:02 +02:00
parent 7ce5776694
commit 7e39443084
7 changed files with 477 additions and 2 deletions

View file

@ -20,9 +20,13 @@
namespace App\Services\LabelSystem;
use App\Services\LabelSystem\PlaceholderProviders\PlaceholderProviderInterface;
/**
* This service replaces the Placeholders of the user provided lines with the proper informations.
* It uses the PlaceholderProviders provided by PlaceholderProviderInterface classes.
* @package App\Services\LabelSystem
*/
class LabelTextReplacer
{
protected $providers;
@ -32,6 +36,13 @@ class LabelTextReplacer
$this->providers = $providers;
}
/**
* Determine the replacement for a single placeholder. It is iterated over all Replacement Providers.
* If the given string is not a placeholder or the placeholder is not known, it will be returned unchanged.
* @param string $placeholder The placeholder that should be replaced. (e.g. '%%PLACEHOLDER%%')
* @param object $target The object that should be used for the placeholder info source.
* @return string If the placeholder was valid, the replaced info. Otherwise the passed string.
*/
public function handlePlaceholder(string $placeholder, object $target): string
{
foreach ($this->providers as $provider) {
@ -45,10 +56,16 @@ class LabelTextReplacer
return $placeholder;
}
/**
* Replaces all placeholders in the input lines.
* @param string $lines The input lines that should be replaced
* @param object $target The object that should be used as source for the informations.
* @return string The Lines with replaced informations.
*/
public function replace(string $lines, object $target): string
{
$patterns = [
'/(%%.*%%)/' => function ($match) use ($target) {
'/(%%[A-Z_]+%%)/' => function ($match) use ($target) {
return $this->handlePlaceholder($match[0], $target);
},
];

View file

@ -0,0 +1,97 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LabelSystem;
use App\Entity\Parts\Part;
use App\Services\AmountFormatter;
use App\Services\LabelSystem\LabelTextReplacer;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class LabelTextReplacerTest extends WebTestCase
{
/**
* @var LabelTextReplacer
*/
protected $service;
/** @var Part */
protected $target;
protected function setUp(): void
{
parent::setUp();
//Get an service instance.
self::bootKernel();
$this->service = self::$container->get(LabelTextReplacer::class);
$this->target = new Part();
$this->target->setName('Part 1');
$this->target->setDescription('P Description');
$this->target->setComment('P Comment');
}
public function handlePlaceholderDataProvider(): array
{
return [
['Part 1', '%%NAME%%'],
['P Description', '%%DESCRIPTION%%'],
['%%UNKNOWN%%', '%%UNKNOWN%%'],
['%%INVALID', '%%INVALID'],
['%%', '%%'],
['NAME', 'NAME'],
['%%NAME', '%%NAME'],
['Test %%NAME%%', 'Test %%NAME%%'],
];
}
public function replaceDataProvider(): array
{
return [
['Part 1', '%%NAME%%'],
['TestPart 1', 'Test%%NAME%%'],
["P Description\nPart 1", "%%DESCRIPTION_T%%\n%%NAME%%"],
['Part 1 Part 1', '%%NAME%% %%NAME%%'],
['%%UNKNOWN%% Test', '%%UNKNOWN%% Test'],
["%%NAME\n%% %%NAME %%", "%%NAME\n%% %%NAME %%"],
['%%%%', '%%%%'],
['TEST%% %%TEST', 'TEST%% %%TEST']
];
}
/**
* @dataProvider handlePlaceholderDataProvider
*/
public function testHandlePlaceholder(string $expected, string $input): void
{
$this->assertSame($expected, $this->service->handlePlaceholder($input, $this->target));
}
/**
* @dataProvider replaceDataProvider
*/
public function testReplace(string $expected, string $input): void
{
$this->assertSame($expected, $this->service->replace($input, $this->target));
}
}

View file

@ -0,0 +1,68 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Part;
use App\Services\LabelSystem\PlaceholderProviders\AbstractDBElementProvider;
use App\Services\LabelSystem\PlaceholderProviders\GlobalProviders;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class AbstractElementProviderTest extends WebTestCase
{
/** @var AbstractDBElementProvider */
protected $service;
protected $target;
public function setUp(): void
{
self::bootKernel();
$this->service = self::$container->get(AbstractDBElementProvider::class);
$this->target = new class extends AbstractDBElement {
protected $id = 123;
/**
* @inheritDoc
*/
public function getIDString(): string
{
return 'ignore';
}
};
}
public function dataProvider(): array
{
return [
['123', '%%ID%%'],
];
}
/**
* @dataProvider dataProvider
*/
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));
}
}

View file

@ -0,0 +1,57 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use App\Entity\Parts\Part;
use App\Services\LabelSystem\PlaceholderProviders\GlobalProviders;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class GlobalProvidersTest extends WebTestCase
{
/** @var GlobalProviders */
protected $service;
protected $target;
public function setUp(): void
{
self::bootKernel();
$this->service = self::$container->get(GlobalProviders::class);
$this->target = new Part();
}
public function dataProvider(): array
{
return [
['Part-DB', '%%INSTALL_NAME%%'],
['anonymous', '%%USERNAME%%'],
];
}
/**
* @dataProvider dataProvider
*/
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));
}
}

View file

@ -0,0 +1,67 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Parts\Part;
use App\Services\LabelSystem\PlaceholderProviders\GlobalProviders;
use App\Services\LabelSystem\PlaceholderProviders\NamedElementProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class NamedElementProviderTest extends WebTestCase
{
/** @var NamedElementProvider */
protected $service;
protected $target;
public function setUp(): void
{
self::bootKernel();
$this->service = self::$container->get(NamedElementProvider::class);
$this->target = new class implements NamedElementInterface {
/**
* @inheritDoc
*/
public function getName(): string
{
return 'This is my Name';
}
};
}
public function dataProvider(): array
{
return [
['This is my Name', '%%NAME%%']
];
}
/**
* @dataProvider dataProvider
*/
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));
}
}

View file

@ -0,0 +1,95 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use App\Entity\Contracts\TimeStampableInterface;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Part;
use App\Services\LabelSystem\PlaceholderProviders\GlobalProviders;
use App\Services\LabelSystem\PlaceholderProviders\PartProvider;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @group DB
*/
class PartProviderTest extends WebTestCase
{
/** @var PartProvider */
protected $service;
protected $target;
/** @var \Doctrine\ORM\EntityManager */
protected $em;
public function setUp(): void
{
self::bootKernel();
$this->service = self::$container->get(PartProvider::class);
$this->target = new Part();
$this->em = self::$container->get(EntityManagerInterface::class);
$this->target->setCategory($this->em->find(Category::class, 6));
$this->target->setFootprint($this->em->find(Footprint::class, 6));
$this->target->setManufacturer(null);
$this->target->setMass(1234.2);
$this->target->setTags('SMD, Tag1, Tag2');
$this->target->setManufacturerProductNumber('MPN123');
$this->target->setManufacturingStatus('active');
$this->target->setDescription('<b>Bold</b> *Italic*');
$this->target->setComment('<b>Bold</b> *Italic*');
}
public function dataProvider(): array
{
return [
['Node 2.1', '%%CATEGORY%%'],
['Node 2 → Node 2.1', '%%CATEGORY_FULL%%'],
['Node 2.1', '%%FOOTPRINT%%'],
['Node 2 → Node 2.1', '%%FOOTPRINT_FULL%%'],
['', '%%MANUFACTURER%%'],
['', '%%MANUFACTURER_FULL%%'],
['1.2 kg', '%%MASS%%'],
['MPN123', '%%MPN%%'],
['SMD, Tag1, Tag2', '%%TAGS%%'],
['Active', '%%M_STATUS%%'],
['<b>Bold</b> <em>Italic</em>', '%%DESCRIPTION%%'],
['Bold Italic', '%%DESCRIPTION_T%%'],
['<b>Bold</b> <em>Italic</em>', '%%COMMENT%%'],
['Bold Italic', '%%COMMENT_T%%'],
];
}
/**
* @dataProvider dataProvider
*/
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));
}
}

View file

@ -0,0 +1,74 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LabelSystem\PlaceholderProviders;
use App\Entity\Contracts\TimeStampableInterface;
use App\Services\LabelSystem\PlaceholderProviders\GlobalProviders;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class TimestampableElementProviderTest extends WebTestCase
{
/** @var GlobalProviders */
protected $service;
protected $target;
public function setUp(): void
{
self::bootKernel();
$this->service = self::$container->get(GlobalProviders::class);
$this->target = new class implements TimeStampableInterface {
/**
* @inheritDoc
*/
public function getLastModified(): ?DateTime
{
return new \DateTime('2000-01-01');
}
/**
* @inheritDoc
*/
public function getAddedDate(): ?DateTime
{
return new \DateTime('2000-01-01');
}
};
}
public function dataProvider(): array
{
return [
['2000-01-01', '%%LAST_MODIFIED%%'],
['2000-01-01', '%%CREATION_DATE%%'],
];
}
/**
* @dataProvider dataProvider
*/
public function testReplace(string $expected, string $placeholder): void
{
$this->assertSame($expected, $this->service->replace($placeholder, $this->target));
}
}