From 4901a4aac0d0b3c21f95251b8ef3fd91ea6d9cb6 Mon Sep 17 00:00:00 2001 From: Maximilian Graf Schimmelmann Date: Wed, 2 Apr 2025 11:03:04 +0200 Subject: [PATCH 1/6] Add tests. --- src/Field/Configurator/FileConfigurator.php | 91 ------------------- .../Configurator/FileConfiguratorTest.php | 0 .../TemplateRegisterSubscriberTest.php | 0 3 files changed, 91 deletions(-) delete mode 100644 src/Field/Configurator/FileConfigurator.php delete mode 100644 tests/PhpUnit/Field/Configurator/FileConfiguratorTest.php delete mode 100644 tests/PhpUnit/Subscriber/TemplateRegisterSubscriberTest.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 From b56a740ebc7637ebae037f82c983ac74545e36e6 Mon Sep 17 00:00:00 2001 From: Maximilian Graf Schimmelmann Date: Wed, 2 Apr 2025 11:04:37 +0200 Subject: [PATCH 2/6] Add phpunit.xml.dist. --- phpunit.xml.dist | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 phpunit.xml.dist diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..fa7a5a0 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + tests + src/DataFixtures + src/Repository + src/Command + + + + + + src + + + src/ + src/DataFixtures + src/Repository + src/Command + src/Controller + src/Kernel.php + + + + + + + + + + From cbda9ec5eec2028a6cae4f9d5a520a889614e8c2 Mon Sep 17 00:00:00 2001 From: Maximilian Graf Schimmelmann Date: Wed, 2 Apr 2025 11:10:59 +0200 Subject: [PATCH 3/6] Adapt phpunit xml and add test bootstrap --- phpunit.xml.dist | 9 ++------- tests/bootstrap.php | 11 +++++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 tests/bootstrap.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fa7a5a0..caa705e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,17 +3,15 @@ + bootstrap="tests/bootstrap.php"> - + @@ -42,7 +40,4 @@ - - - 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'); +} From 31a685f9a6f3b7601a442b7511ec532b4051f404 Mon Sep 17 00:00:00 2001 From: Maximilian Graf Schimmelmann Date: Wed, 2 Apr 2025 11:12:01 +0200 Subject: [PATCH 4/6] Downgrade PhpUnit. --- composer.json | 2 +- phpunit.xml.dist | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) 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 index caa705e..6ec340e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,15 +3,17 @@ + bootstrap="tests/bootstrap.php" +> - + From 4b9419b22c6668b2223e42daeb3fa2538aa56fb9 Mon Sep 17 00:00:00 2001 From: Maximilian Graf Schimmelmann Date: Wed, 2 Apr 2025 11:20:02 +0200 Subject: [PATCH 5/6] Downgrade PhpUnit. --- phpunit.xml.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6ec340e..eff9428 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -39,6 +39,7 @@ + From 060d38e85b0bc8b082150c6e792ccc3f34440bb3 Mon Sep 17 00:00:00 2001 From: Maximilian Graf Schimmelmann Date: Wed, 2 Apr 2025 11:22:24 +0200 Subject: [PATCH 6/6] Downgrade PhpUnit. --- phpunit.xml.dist | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index eff9428..ef708d4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,9 +19,8 @@ tests - src/DataFixtures - src/Repository - src/Command + src/Field/Configurator + src/Subscriber @@ -30,17 +29,9 @@ src - src/ - src/DataFixtures - src/Repository - src/Command - src/Controller - src/Kernel.php + src/Field/Configurator + src/Subscriber + src/FileUploadFieldBundle.php - - - - -