diff --git a/src/Services/LabelSystem/LabelTextReplacer.php b/src/Services/LabelSystem/LabelTextReplacer.php index a254ee68..844abbb2 100644 --- a/src/Services/LabelSystem/LabelTextReplacer.php +++ b/src/Services/LabelSystem/LabelTextReplacer.php @@ -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); }, ]; diff --git a/tests/Services/LabelSystem/LabelTextReplacerTest.php b/tests/Services/LabelSystem/LabelTextReplacerTest.php new file mode 100644 index 00000000..4ff381c0 --- /dev/null +++ b/tests/Services/LabelSystem/LabelTextReplacerTest.php @@ -0,0 +1,97 @@ +. + */ + +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)); + } +} diff --git a/tests/Services/LabelSystem/PlaceholderProviders/AbstractElementProviderTest.php b/tests/Services/LabelSystem/PlaceholderProviders/AbstractElementProviderTest.php new file mode 100644 index 00000000..8d5df9d2 --- /dev/null +++ b/tests/Services/LabelSystem/PlaceholderProviders/AbstractElementProviderTest.php @@ -0,0 +1,68 @@ +. + */ + +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)); + } +} diff --git a/tests/Services/LabelSystem/PlaceholderProviders/GlobalProvidersTest.php b/tests/Services/LabelSystem/PlaceholderProviders/GlobalProvidersTest.php new file mode 100644 index 00000000..324e9997 --- /dev/null +++ b/tests/Services/LabelSystem/PlaceholderProviders/GlobalProvidersTest.php @@ -0,0 +1,57 @@ +. + */ + +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)); + } +} diff --git a/tests/Services/LabelSystem/PlaceholderProviders/NamedElementProviderTest.php b/tests/Services/LabelSystem/PlaceholderProviders/NamedElementProviderTest.php new file mode 100644 index 00000000..59f3bf81 --- /dev/null +++ b/tests/Services/LabelSystem/PlaceholderProviders/NamedElementProviderTest.php @@ -0,0 +1,67 @@ +. + */ + +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)); + } +} diff --git a/tests/Services/LabelSystem/PlaceholderProviders/PartProviderTest.php b/tests/Services/LabelSystem/PlaceholderProviders/PartProviderTest.php new file mode 100644 index 00000000..10e68cb7 --- /dev/null +++ b/tests/Services/LabelSystem/PlaceholderProviders/PartProviderTest.php @@ -0,0 +1,95 @@ +. + */ + +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('Bold *Italic*'); + $this->target->setComment('Bold *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%%'], + ['Bold Italic', '%%DESCRIPTION%%'], + ['Bold Italic', '%%DESCRIPTION_T%%'], + ['Bold Italic', '%%COMMENT%%'], + ['Bold Italic', '%%COMMENT_T%%'], + ]; + } + + /** + * @dataProvider dataProvider + */ + public function testReplace(string $expected, string $placeholder): void + { + $this->assertSame($expected, $this->service->replace($placeholder, $this->target)); + } +} diff --git a/tests/Services/LabelSystem/PlaceholderProviders/TimestampableElementProviderTest.php b/tests/Services/LabelSystem/PlaceholderProviders/TimestampableElementProviderTest.php new file mode 100644 index 00000000..c9f09744 --- /dev/null +++ b/tests/Services/LabelSystem/PlaceholderProviders/TimestampableElementProviderTest.php @@ -0,0 +1,74 @@ +. + */ + +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)); + } +}