diff --git a/composer.json b/composer.json index f620320..89e9085 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^11.5", + "phpunit/phpunit": "^9.6.22", "rector/rector": "^2.0", "phpstan/phpstan": "^2.1" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..ef708d4 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + tests + src/Field/Configurator + src/Subscriber + + + + + + src + + + src/Field/Configurator + src/Subscriber + src/FileUploadFieldBundle.php + + + diff --git a/src/Field/Configurator/FileConfigurator.php b/src/Field/Configurator/FileConfigurator.php deleted file mode 100644 index 1c6f453..0000000 --- a/src/Field/Configurator/FileConfigurator.php +++ /dev/null @@ -1,91 +0,0 @@ - - */ -#[AutoconfigureTag('ea.field_configurator')] -final readonly class FileConfigurator implements FieldConfiguratorInterface -{ - public function __construct( - private string $projectDir, - ) {} - - public function supports(FieldDto $field, EntityDto $entityDto): bool - { - return FileField::class === $field->getFieldFqcn(); - } - - public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $context): void - { - $configuredBasePath = $field->getCustomOption(ImageField::OPTION_BASE_PATH); - $formattedValue = \is_array($field->getValue()) - ? $this->getImagesPaths($field->getValue(), $configuredBasePath) - : $this->getImagePath($field->getValue(), $configuredBasePath); - $field->setFormattedValue($formattedValue); - - $field->setFormTypeOption('upload_filename', $field->getCustomOption(ImageField::OPTION_UPLOADED_FILE_NAME_PATTERN)); - - if (null === $formattedValue || '' === $formattedValue || (\is_array($formattedValue) && 0 === \count($formattedValue)) || $formattedValue === rtrim($configuredBasePath ?? '', '/')) { - $field->setTemplateName('label/empty'); - } - - if (!\in_array($context->getCrud()->getCurrentPage(), [Crud::PAGE_EDIT, Crud::PAGE_NEW], true)) { - return; - } - - $relativeUploadDir = $field->getCustomOption(ImageField::OPTION_UPLOAD_DIR); - if (null === $relativeUploadDir) { - throw new \InvalidArgumentException(sprintf('The "%s" image field must define the directory where the images are uploaded using the setUploadDir() method.', $field->getProperty())); - } - $relativeUploadDir = u($relativeUploadDir)->trimStart(\DIRECTORY_SEPARATOR)->ensureEnd(\DIRECTORY_SEPARATOR)->toString(); - $isStreamWrapper = filter_var($relativeUploadDir, \FILTER_VALIDATE_URL); - if ($isStreamWrapper) { - $absoluteUploadDir = $relativeUploadDir; - } else { - $absoluteUploadDir = u($relativeUploadDir)->ensureStart($this->projectDir . \DIRECTORY_SEPARATOR)->toString(); - } - $field->setFormTypeOption('upload_dir', $absoluteUploadDir); - - $field->setFormTypeOption('file_constraints', $field->getCustomOption(ImageField::OPTION_FILE_CONSTRAINTS)); - } - - private function getImagesPaths(?array $images, ?string $basePath): array - { - $imagesPaths = []; - foreach ($images as $image) { - $imagesPaths[] = $this->getImagePath($image, $basePath); - } - - return $imagesPaths; - } - - private function getImagePath(?string $imagePath, ?string $basePath): ?string - { - // add the base path only to images that are not absolute URLs (http or https) or protocol-relative URLs (//) - if (null === $imagePath || 0 !== preg_match('/^(http[s]?|\/\/)/i', $imagePath)) { - return $imagePath; - } - - // remove project path from filepath - $imagePath = str_replace($this->projectDir . \DIRECTORY_SEPARATOR . 'public' . \DIRECTORY_SEPARATOR, '', $imagePath); - - return isset($basePath) - ? rtrim($basePath, '/') . '/' . ltrim($imagePath, '/') - : '/' . ltrim($imagePath, '/'); - } -} diff --git a/tests/PhpUnit/Field/Configurator/FileConfiguratorTest.php b/tests/PhpUnit/Field/Configurator/FileConfiguratorTest.php deleted file mode 100644 index e69de29..0000000 diff --git a/tests/PhpUnit/Subscriber/TemplateRegisterSubscriberTest.php b/tests/PhpUnit/Subscriber/TemplateRegisterSubscriberTest.php deleted file mode 100644 index e69de29..0000000 diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..469dcce --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,11 @@ +bootEnv(dirname(__DIR__).'/.env'); +}