-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from beniaminblaszkiewicz/OPSRC-441_feature_se…
…t_template_per_catalog OPSRC-441 Add choices templates
- Loading branch information
Showing
13 changed files
with
344 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file was created by developers working at BitBag | ||
* Do you need more information about us and what we do? Visit our https://bitbag.io website! | ||
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\BitBag\SyliusCatalogPlugin\Choices; | ||
|
||
use BitBag\SyliusCatalogPlugin\Choices\CatalogInterface; | ||
use BitBag\SyliusCatalogPlugin\Choices\CatalogMapper; | ||
use BitBag\SyliusCatalogPlugin\Choices\CatalogMapperInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
final class CatalogMapperSpec extends ObjectBehavior | ||
{ | ||
private const CATALOG_NAME = 'catalog'; | ||
|
||
public function let(): void | ||
{ | ||
$this->beConstructedWith(self::CATALOG_NAME); | ||
} | ||
|
||
public function it_is_initializable(): void | ||
{ | ||
$this->shouldHaveType(CatalogMapper::class); | ||
} | ||
|
||
public function it_implements_catalog_mapper_interface(): void | ||
{ | ||
$this->shouldHaveType(CatalogMapperInterface::class); | ||
} | ||
|
||
public function it_returns_array_of_templates_from_catalog_dir_with_default_one( | ||
SplFileInfo $file1, | ||
SplFileInfo $file2 | ||
): void { | ||
$files = new \ArrayIterator([ | ||
$file1->getWrappedObject(), | ||
$file2->getWrappedObject(), | ||
]); | ||
|
||
$file1->getBasename('.html.twig')->willReturn('one'); | ||
$file1->getBasename()->willReturn('one.html.twig'); | ||
|
||
$file2->getBasename('.html.twig')->willReturn('two'); | ||
$file2->getBasename()->willReturn('two.html.twig'); | ||
|
||
$templates = [ | ||
'one' => self::CATALOG_NAME . '/one.html.twig', | ||
'two' => self::CATALOG_NAME . '/two.html.twig', | ||
]; | ||
|
||
$this->map($files)->shouldReturn(array_merge(CatalogInterface::DEFAULT_TEMPLATE, $templates)); | ||
} | ||
|
||
public function it_returns_array_of_templates_from_catalog_dir_with_first_default( | ||
SplFileInfo $file1, | ||
SplFileInfo $file2, | ||
SplFileInfo $default | ||
): void { | ||
$files = new \ArrayIterator([ | ||
$file1->getWrappedObject(), | ||
$file2->getWrappedObject(), | ||
$default->getWrappedObject(), | ||
]); | ||
|
||
$file1->getBasename('.html.twig')->willReturn('one'); | ||
$file1->getBasename()->willReturn('one.html.twig'); | ||
|
||
$file2->getBasename('.html.twig')->willReturn('two'); | ||
$file2->getBasename()->willReturn('two.html.twig'); | ||
|
||
$default->getBasename('.html.twig')->willReturn('default'); | ||
$default->getBasename()->willReturn('default.html.twig'); | ||
|
||
$templates = [ | ||
'one' => self::CATALOG_NAME . '/one.html.twig', | ||
'two' => self::CATALOG_NAME . '/two.html.twig', | ||
'default' => self::CATALOG_NAME . '/default.html.twig', | ||
]; | ||
|
||
if (in_array(array_key_first(CatalogInterface::DEFAULT_TEMPLATE), array_keys($templates))) { | ||
$this->map($files)->shouldReturn(array_merge(['default' => $templates['default']], $templates)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* another great project. | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on [email protected]. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace spec\BitBag\SyliusCatalogPlugin\Choices; | ||
|
||
use BitBag\SyliusCatalogPlugin\Choices\Catalog; | ||
use BitBag\SyliusCatalogPlugin\Choices\CatalogInterface; | ||
use BitBag\SyliusCatalogPlugin\Choices\CatalogMapperInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Symfony\Component\Finder\Exception\DirectoryNotFoundException; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
final class CatalogSpec extends ObjectBehavior | ||
{ | ||
private const FULL_TEMPLATE_PATH = 'spec/test'; | ||
|
||
public function let(CatalogMapperInterface $catalogMapper, Finder $finder): void | ||
{ | ||
$this->beConstructedWith(self::FULL_TEMPLATE_PATH, $catalogMapper, $finder); | ||
} | ||
|
||
public function it_is_initializable(): void | ||
{ | ||
$this->shouldHaveType(Catalog::class); | ||
} | ||
|
||
public function it_implements_catalog_interface(): void | ||
{ | ||
$this->shouldHaveType(CatalogInterface::class); | ||
} | ||
|
||
public function it_returns_default_template_if_directory_doesnt_exist( | ||
Finder $finder | ||
): void { | ||
$finder->files()->willReturn($finder); | ||
$finder->in(Argument::type('string'))->willReturn($finder); | ||
$finder->name('*.html.twig')->willReturn($finder); | ||
$finder->depth(0)->willThrow(DirectoryNotFoundException::class); | ||
|
||
$this->getTemplates()->shouldReturn(CatalogInterface::DEFAULT_TEMPLATE); | ||
} | ||
|
||
public function it_returns_default_template_if_directory_doesnt_contain_twig_files( | ||
Finder $finder | ||
): void { | ||
if (!is_dir(self::FULL_TEMPLATE_PATH)) { | ||
mkdir(self::FULL_TEMPLATE_PATH, 0777, true); | ||
} | ||
$finder->files()->willReturn($finder); | ||
$finder->in(self::FULL_TEMPLATE_PATH)->willReturn($finder); | ||
$finder->name('*.html.twig')->willReturn($finder); | ||
$finder->depth(0)->willReturn($finder); | ||
|
||
$finder->hasResults()->willReturn(false); | ||
|
||
$this->getTemplates()->shouldReturn(CatalogInterface::DEFAULT_TEMPLATE); | ||
|
||
$this->rrmdir(self::FULL_TEMPLATE_PATH); | ||
} | ||
|
||
private function rrmdir(string $directory): bool | ||
{ | ||
array_map(fn (string $file) => is_dir($file) ? $this->rrmdir($file) : unlink($file), glob($directory . '/' . '*')); | ||
|
||
return rmdir($directory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* another great project. | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on [email protected]. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusCatalogPlugin\Choices; | ||
|
||
interface CatalogInterface | ||
{ | ||
public const DEFAULT_TEMPLATE = ['default' => '@BitBagSyliusCatalogPlugin/Catalog/Templates/showProducts.html.twig']; | ||
|
||
public function getTemplates(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file was created by developers working at BitBag | ||
* Do you need more information about us and what we do? Visit our https://bitbag.io website! | ||
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusCatalogPlugin\Choices; | ||
|
||
final class CatalogMapper implements CatalogMapperInterface | ||
{ | ||
private string $catalogName; | ||
|
||
public function __construct(string $catalogName) | ||
{ | ||
$this->catalogName = $catalogName; | ||
} | ||
|
||
public function map(iterable $files): array | ||
{ | ||
$templates = []; | ||
foreach ($files as $file) { | ||
$templates[$file->getBasename('.html.twig')] = $this->catalogName . '/' . $file->getBasename(); | ||
} | ||
if (in_array(array_key_first(CatalogInterface::DEFAULT_TEMPLATE), array_keys($templates))) { | ||
return array_merge(['default' => $templates['default']], $templates); | ||
} | ||
|
||
return array_merge(CatalogInterface::DEFAULT_TEMPLATE, $templates); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/* | ||
* This file was created by developers working at BitBag | ||
* Do you need more information about us and what we do? Visit our https://bitbag.io website! | ||
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusCatalogPlugin\Choices; | ||
|
||
interface CatalogMapperInterface | ||
{ | ||
public function map(iterable $files): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.