From ed361f90ba7d7905f4c6ea474d147fb3919c0fea Mon Sep 17 00:00:00 2001 From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com> Date: Sat, 25 Nov 2023 05:27:36 -0300 Subject: [PATCH] Update docs, clean up tests. (#10) --- README.md | 10 +- src/File.php | 57 ---- src/FilePond.php | 54 ++-- tests/AssetTest.php | 570 ++++++++++++++++++++++++---------------- tests/ExceptionTest.php | 28 -- tests/RenderTest.php | 149 +++++------ tests/Support/main.php | 9 +- tests/TestCase.php | 3 + 8 files changed, 471 insertions(+), 409 deletions(-) delete mode 100644 src/File.php delete mode 100644 tests/ExceptionTest.php diff --git a/README.md b/README.md index ade5e01..83c4e50 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,15 @@ to the require-dev section of your `composer.json` file. ## Usage -### View +### Name + +```php +use Yii2\Extensions\Filepond\FilePond; + +echo FilePond::widget(['name' => 'image_file']); +``` + +### Active Field ```php use Yii2\Extensions\Filepond\FilePond; diff --git a/src/File.php b/src/File.php deleted file mode 100644 index dbfee62..0000000 --- a/src/File.php +++ /dev/null @@ -1,57 +0,0 @@ -attributes; - $attributes['type'] = 'file'; - - if (!array_key_exists('name', $attributes)) { - $name = Utils::generateInputName($this->formModel->formName(), $this->attribute); - } else { - $name = is_string($attributes['name']) ? $attributes['name'] : ''; - } - - $attributes['id'] = Utils::generateInputId($this->formModel->formName(), $this->attribute); - $attributes['name'] = Utils::generateArrayableName($name); - - // input type="file" not supported value attribute. - unset($attributes['value']); - - return HtmlBuilder::create('input', '', $attributes); - } -} diff --git a/src/FilePond.php b/src/FilePond.php index 6b1f590..2518d3f 100644 --- a/src/FilePond.php +++ b/src/FilePond.php @@ -7,7 +7,7 @@ use JsonException; use PHPForge\Html\Helper\CssClass; use PHPForge\Html\Helper\Utils; -use RuntimeException; +use PHPForge\Html\Input; use Yii2\Extensions\FilePond\Asset\FilePondAsset; use Yii2\Extensions\FilePond\Asset\FilePondCdnAsset; use Yii; @@ -57,7 +57,7 @@ final class FilePond extends InputWidget */ public bool $allowPdfPreview = false; /** - * @var string The CSS class for the filepond container. + * @var string The CSS class to add to the root element. */ public string $cssClass = ''; /** @@ -273,8 +273,12 @@ final class FilePond extends InputWidget */ public bool $required = false; + private string $id = ''; + public function init(): void { + parent::init(); + $this->config = array_merge( [ 'acceptedFileTypes' => $this->acceptedFileTypes, @@ -329,18 +333,14 @@ public function init(): void ], $this->config, ); + + $this->id = $this->hasModel() + ? Utils::generateInputId($this->model->formName(), $this->attribute) + : $this->getId() . '-filepond'; } public function run(): string { - if ($this->model === null) { - throw new RuntimeException('The model is not set.'); - } - - if ($this->attribute === null) { - throw new RuntimeException('The attribute is not set.'); - } - $this->registerClientScript(); return $this->renderInputFile(); @@ -357,7 +357,6 @@ private function getScript(): string $closure = "{$this->fileValidateTypeDetectType} {$closure}"; } - $id = Utils::generateInputId($this->model->formName(), $this->attribute); $loadFileDefault = $this->loadFileDefault; $pluginConfig = implode(', ', $this->pluginDefault); $setOptions = json_encode($this->config, JSON_THROW_ON_ERROR); @@ -367,7 +366,7 @@ private function getScript(): string FilePond.setOptions($setOptions) const loadFileDefault = "$loadFileDefault" - const pond = FilePond.create(document.querySelector('input[type="file"][id="$id"]'), {$closure}) + const pond = FilePond.create(document.querySelector('input[type="file"][id="$this->id"]'), {$closure}) if (loadFileDefault !== '') { pond.addFiles(loadFileDefault) @@ -396,22 +395,20 @@ private function registerClientScript(): void */ private function renderInputFile(): string { + $name = $this->name; $options = $this->options; if (isset($options['class']) && str_contains($options['class'], 'form-control')) { $options['class'] = str_replace('form-control', '', $options['class']); } - if (isset($options['placeholder']) && $options['placeholder'] === true) { - unset($options['placeholder']); - } - if (array_key_exists('allowMultiple', $this->config) && $this->config['allowMultiple']) { $options['multiple'] = true; } if (array_key_exists('className', $this->config) && is_string($this->config['className'])) { - CssClass::add($options, $this->config['className']); + $class = str_replace('form-control', '', $this->config['className']); + CssClass::add($options, $class); } if (array_key_exists('required', $this->config) && $this->config['required']) { @@ -420,6 +417,27 @@ private function renderInputFile(): string CssClass::add($options, 'filepond'); - return File::widget($this->model, $this->attribute)->attributes($options)->render(); + $name = match ($this->hasModel()) { + true => Utils::generateArrayableName(Utils::generateInputName($this->model->formName(), $this->attribute)), + default => Utils::generateArrayableName($name), + }; + + // input type="file" not supported value attribute. + unset($options['id'], $options['placeholder'], $options['value']); + + return match ($this->hasModel()) { + true => Input::widget() + ->attributes($options) + ->id($this->id) + ->name($name) + ->type('file') + ->render(), + default => Input::widget() + ->attributes($options) + ->id($this->id) + ->name($name) + ->type('file') + ->render(), + }; } } diff --git a/tests/AssetTest.php b/tests/AssetTest.php index 2524d7e..ede82f6 100644 --- a/tests/AssetTest.php +++ b/tests/AssetTest.php @@ -5,97 +5,81 @@ namespace Yii2\Extensions\FilePond\Tests; use Yii2\Extensions\FilePond\Asset; +use Yii2\Extensions\FilePond\FilePond; use Yii; -use yii\web\AssetBundle; use yii\web\View; final class AssetTest extends TestCase { - public function testFilePondAssetSimpleDependency(): void + public function setup(): void { + parent::setUp(); $this->mockApplication(); - $view = Yii::$app->getView(); + FilePond::$counter = 0; - $this->assertEmpty($view->assetBundles); + $this->view = Yii::$app->getView(); + } - Asset\FilePondAsset::register($view); + public function testFilePondAssetSimpleDependency(): void + { + $this->assertEmpty($this->view->assetBundles); - $this->assertCount(6, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondEncodePlugin::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondImageExifOrientationPlugin::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondImagePreviewPlugin::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondValidateSizePlugin::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondValidateTypePlugin::class, $view->assetBundles); + Asset\FilePondAsset::register($this->view); + + $this->assertCount(6, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateTypePlugin::class, $this->view->assetBundles); } public function testFilePondAssetRegister(): void { - $this->mockApplication(); + $this->assertEmpty($this->view->assetBundles); - $view = new View(); + Asset\FilePondAsset::register($this->view); - $this->assertEmpty($view->assetBundles); + $this->assertCount(6, $this->view->assetBundles); - Asset\FilePondAsset::register($view); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); - $this->assertCount(6, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondAsset::class]); - - $result = $view->renderFile(__DIR__ . '/Support/main.php'); - - $this->assertStringContainsString('filepond.css', $result); - $this->assertStringContainsString('filepond.js', $result); - $this->assertStringContainsString('filepond-plugin-file-encode.js', $result); - $this->assertStringContainsString('filepond-plugin-image-exif-orientation.js', $result); - $this->assertStringContainsString('filepond-plugin-image-preview.js', $result); - $this->assertStringContainsString('filepond-plugin-file-validate-size.js', $result); - $this->assertStringContainsString('filepond-plugin-file-validate-type.js', $result); + $this->assertStringContainsString('/dist/filepond.css', $result); + $this->assertStringContainsString('/dist/filepond.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-encode.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-exif-orientation.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-preview.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-size.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-type.js', $result); } public function testFilePondCdnAssetSimpleDependency(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); + $this->assertEmpty($this->view->assetBundles); - $this->assertEmpty($view->assetBundles); - - Asset\FilePondCdnAsset::register($view); + Asset\FilePondCdnAsset::register($this->view); - $this->assertCount(6, $view->assetBundles); + $this->assertCount(6, $this->view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\Cdn\FilePondEncodePlugin::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\Cdn\FilePondImageExifOrientationPlugin::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\Cdn\FilePondImagePreviewPlugin::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\Cdn\FilePondValidateSizePlugin::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\Cdn\FilePondValidateTypePlugin::class, $view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateTypePlugin::class, $this->view->assetBundles); } public function testFilePondCdnAssetRegister(): void { - $this->mockApplication(); + $this->assertEmpty($this->view->assetBundles); - $view = new View(); + Asset\FilePondCdnAsset::register($this->view); - $this->assertEmpty($view->assetBundles); - - Asset\FilePondCdnAsset::register($view); - - $this->assertCount(6, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondCdnAsset::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\Cdn\FilePondEncodePlugin::class]); - $this->assertInstanceOf( - AssetBundle::class, - $view->assetBundles[Asset\Cdn\FilePondImageExifOrientationPlugin::class], - ); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\Cdn\FilePondImagePreviewPlugin::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\Cdn\FilePondValidateSizePlugin::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\Cdn\FilePondValidateTypePlugin::class]); + $this->assertCount(6, $this->view->assetBundles); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); $this->assertStringContainsString( <<mockApplication(); - - $view = Yii::$app->getView(); - - $this->assertEmpty($view->assetBundles); - - Asset\FilePondImageCropPlugin::register($view); - - $this->assertCount(7, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondImageCropPlugin::class, $view->assetBundles); + $this->assertEmpty($this->view->assetBundles); + + Asset\FilePondImageCropPlugin::register($this->view); + + $this->assertCount(7, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateTypePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImageCropPlugin::class, $this->view->assetBundles); } public function testFilePondImageCropPluginRegister(): void { - $this->mockApplication(); + $this->assertEmpty($this->view->assetBundles); - $view = new View(); - - $this->assertEmpty($view->assetBundles); + Asset\FilePondImageCropPlugin::register($this->view); - Asset\FilePondImageCropPlugin::register($view); + $this->assertCount(7, $this->view->assetBundles); - $this->assertCount(7, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondAsset::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondImageCropPlugin::class]); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); - - $this->assertStringContainsString('filepond.css', $result); - $this->assertStringContainsString('filepond.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-preview.css', $result); + $this->assertStringContainsString('/dist/filepond.css', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-encode.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-exif-orientation.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-preview.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-size.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-type.js', $result); + $this->assertStringContainsString('/dist/filepond.js', $result); $this->assertStringContainsString('filepond-plugin-image-crop.js', $result); } public function testFilePondImageCropPluginCdnSimpleDependency(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - $this->assertEmpty($view->assetBundles); - - Asset\Cdn\FilePondImageCropPlugin::register($view); - - $this->assertCount(7, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\Cdn\FilePondImageCropPlugin::class, $view->assetBundles); + $this->assertEmpty($this->view->assetBundles); + + Asset\Cdn\FilePondImageCropPlugin::register($this->view); + + $this->assertCount(7, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateTypePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImageCropPlugin::class, $this->view->assetBundles); } public function testFilePondImageCropPluginCdnRegister(): void { - $this->mockApplication(); - - $view = new View(); - - $this->assertEmpty($view->assetBundles); + $this->assertEmpty($this->view->assetBundles); - Asset\Cdn\FilePondImageCropPlugin::register($view); + Asset\Cdn\FilePondImageCropPlugin::register($this->view); - $this->assertCount(7, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondCdnAsset::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\Cdn\FilePondImageCropPlugin::class]); + $this->assertCount(7, $this->view->assetBundles); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); + $this->assertStringContainsString( + << + HTML, + $result, + ); $this->assertStringContainsString( << HTML, $result, ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); $this->assertStringContainsString( << @@ -236,53 +253,55 @@ public function testFilePondImageCropPluginCdnRegister(): void public function testFilePondImageTransformPluginSimpleDependency(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - $this->assertEmpty($view->assetBundles); - - Asset\FilePondImageTransformPlugin::register($view); - - $this->assertCount(7, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondImageTransformPlugin::class, $view->assetBundles); + $this->assertEmpty($this->view->assetBundles); + + Asset\FilePondImageTransformPlugin::register($this->view); + + $this->assertCount(7, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateTypePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImageTransformPlugin::class, $this->view->assetBundles); } public function testFilePondImageTransformPluginRegister(): void { - $this->mockApplication(); - - $view = new View(); - - $this->assertEmpty($view->assetBundles); + $this->assertEmpty($this->view->assetBundles); - Asset\FilePondImageTransformPlugin::register($view); + Asset\FilePondImageTransformPlugin::register($this->view); - $this->assertCount(7, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondAsset::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondImageTransformPlugin::class]); + $this->assertCount(7, $this->view->assetBundles); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); - $this->assertStringContainsString('filepond.css', $result); - $this->assertStringContainsString('filepond.js', $result); - $this->assertStringContainsString('filepond-plugin-image-transform.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-preview.css', $result); + $this->assertStringContainsString('/dist/filepond.css', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-encode.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-exif-orientation.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-preview.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-size.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-type.js', $result); + $this->assertStringContainsString('/dist/filepond.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-transform.js', $result); } public function testFilePondImageTransformPluginCdnSimpleDependency(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - $this->assertEmpty($view->assetBundles); - - Asset\Cdn\FilePondImageTransformPlugin::register($view); - - $this->assertCount(7, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\Cdn\FilePondImageTransformPlugin::class, $view->assetBundles); + $this->assertEmpty($this->view->assetBundles); + + Asset\Cdn\FilePondImageTransformPlugin::register($this->view); + + $this->assertCount(7, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateTypePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImageTransformPlugin::class, $this->view->assetBundles); } public function testFilePondImageTransformPluginCdnRegister(): void @@ -296,17 +315,51 @@ public function testFilePondImageTransformPluginCdnRegister(): void Asset\Cdn\FilePondImageTransformPlugin::register($view); $this->assertCount(7, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondCdnAsset::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\Cdn\FilePondImageTransformPlugin::class]); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); + $this->assertStringContainsString( + << + HTML, + $result, + ); $this->assertStringContainsString( << HTML, $result, ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); $this->assertStringContainsString( << @@ -323,77 +376,109 @@ public function testFilePondImageTransformPluginCdnRegister(): void public function testFilePondPdfPreviewPluginSimpleDependency(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - $this->assertEmpty($view->assetBundles); - - Asset\FilePondPdfPreviewPlugin::register($view); - - $this->assertCount(7, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondPdfPreviewPlugin::class, $view->assetBundles); + $this->assertEmpty($this->view->assetBundles); + + Asset\FilePondPdfPreviewPlugin::register($this->view); + + $this->assertCount(7, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateTypePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondPdfPreviewPlugin::class, $this->view->assetBundles); } public function testFilePondPdfPreviewPluginRegister(): void { - $this->mockApplication(); - - $view = new View(); - - $this->assertEmpty($view->assetBundles); + $this->assertEmpty($this->view->assetBundles); - Asset\FilePondPdfPreviewPlugin::register($view); + Asset\FilePondPdfPreviewPlugin::register($this->view); - $this->assertCount(7, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondAsset::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondPdfPreviewPlugin::class]); + $this->assertCount(7, $this->view->assetBundles); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); - $this->assertStringContainsString('filepond.css', $result); - $this->assertStringContainsString('filepond.js', $result); - $this->assertStringContainsString('filepond-plugin-pdf-preview.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-preview.css', $result); + $this->assertStringContainsString('/dist/filepond.css', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-encode.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-exif-orientation.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-preview.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-size.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-type.js', $result); + $this->assertStringContainsString('/dist/filepond.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-pdf-preview.js', $result); } public function testFilePondPdfPreviewPluginCdnSimpleDependency(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - $this->assertEmpty($view->assetBundles); - - Asset\Cdn\FilePondPdfPreviewPlugin::register($view); - - $this->assertCount(7, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\Cdn\FilePondPdfPreviewPlugin::class, $view->assetBundles); + $this->assertEmpty($this->view->assetBundles); + + Asset\Cdn\FilePondPdfPreviewPlugin::register($this->view); + + $this->assertCount(7, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateTypePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondPdfPreviewPlugin::class, $this->view->assetBundles); } public function testFilePondPdfPreviewPluginCdnRegister(): void { - $this->mockApplication(); - - $view = new View(); + $this->assertEmpty($this->view->assetBundles); - $this->assertEmpty($view->assetBundles); - - Asset\Cdn\FilePondPdfPreviewPlugin::register($view); + Asset\Cdn\FilePondPdfPreviewPlugin::register($this->view); - $this->assertCount(7, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondCdnAsset::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\Cdn\FilePondPdfPreviewPlugin::class]); + $this->assertCount(7, $this->view->assetBundles); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); + $this->assertStringContainsString( + << + HTML, + $result, + ); $this->assertStringContainsString( << HTML, $result, ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); $this->assertStringContainsString( << @@ -410,77 +495,108 @@ public function testFilePondPdfPreviewPluginCdnRegister(): void public function testFilePondRenamePluginSimpleDependency(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); + $this->assertEmpty($this->view->assetBundles); - $this->assertEmpty($view->assetBundles); + Asset\FilePondRenamePlugin::register($this->view); - Asset\FilePondRenamePlugin::register($view); + $this->assertCount(7, $this->view->assetBundles); - $this->assertCount(7, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondRenamePlugin::class, $view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondValidateTypePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondRenamePlugin::class, $this->view->assetBundles); } public function testFilePondRenamePluginRegister(): void { - $this->mockApplication(); - - $view = new View(); - - $this->assertEmpty($view->assetBundles); + $this->assertEmpty($this->view->assetBundles); - Asset\FilePondRenamePlugin::register($view); + Asset\FilePondRenamePlugin::register($this->view); - $this->assertCount(7, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondAsset::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondRenamePlugin::class]); + $this->assertCount(7, $this->view->assetBundles); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); - $this->assertStringContainsString('filepond.css', $result); - $this->assertStringContainsString('filepond.js', $result); - $this->assertStringContainsString('filepond-plugin-file-rename.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-preview.css', $result); + $this->assertStringContainsString('/dist/filepond.css', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-encode.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-exif-orientation.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-image-preview.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-size.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-validate-type.js', $result); + $this->assertStringContainsString('/dist/filepond.js', $result); + $this->assertStringContainsString('/dist/filepond-plugin-file-rename.js', $result); } public function testFilePondRenamePluginCdnSimpleDependency(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - $this->assertEmpty($view->assetBundles); - - Asset\Cdn\FilePondRenamePlugin::register($view); - - $this->assertCount(7, $view->assetBundles); - $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $view->assetBundles); - $this->assertArrayHasKey(Asset\Cdn\FilePondRenamePlugin::class, $view->assetBundles); + Asset\Cdn\FilePondRenamePlugin::register($this->view); + + $this->assertCount(7, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\FilePondCdnAsset::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondEncodePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImageExifOrientationPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondImagePreviewPlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateSizePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondValidateTypePlugin::class, $this->view->assetBundles); + $this->assertArrayHasKey(Asset\Cdn\FilePondRenamePlugin::class, $this->view->assetBundles); } public function testFilePondRenamePluginCdnRegister(): void { - $this->mockApplication(); + $this->assertEmpty($this->view->assetBundles); - $view = new View(); - - $this->assertEmpty($view->assetBundles); - - Asset\Cdn\FilePondRenamePlugin::register($view); + Asset\Cdn\FilePondRenamePlugin::register($this->view); - $this->assertCount(7, $view->assetBundles); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\FilePondCdnAsset::class]); - $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[Asset\Cdn\FilePondRenamePlugin::class]); + $this->assertCount(7, $this->view->assetBundles); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => '']); + $this->assertStringContainsString( + << + HTML, + $result, + ); $this->assertStringContainsString( << HTML, $result, ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); + $this->assertStringContainsString( + << + HTML, + $result, + ); $this->assertStringContainsString( << diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php deleted file mode 100644 index 64fffae..0000000 --- a/tests/ExceptionTest.php +++ /dev/null @@ -1,28 +0,0 @@ -expectException(RuntimeException::class); - $this->expectExceptionMessage('The attribute is not set.'); - - FilePond::widget(['model' => new TestForm()]); - } - - public function testNotSetModel(): void - { - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('The model is not set.'); - - FilePond::widget(['attribute' => 'array']); - } -} diff --git a/tests/RenderTest.php b/tests/RenderTest.php index e5be600..bb1867f 100644 --- a/tests/RenderTest.php +++ b/tests/RenderTest.php @@ -13,10 +13,18 @@ */ final class RenderTest extends TestCase { - public function testAllowMultiple(): void + public function setup(): void { + parent::setUp(); $this->mockApplication(); + FilePond::$counter = 0; + + $this->view = Yii::$app->getView(); + } + + public function testAllowMultiple(): void + { $filePond = FilePond::widget( [ 'attribute' => 'array', @@ -29,34 +37,35 @@ public function testAllowMultiple(): void '', $filePond, ); + + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); + + $this->assertStringContainsString('"allowMultiple":true', $result); } public function testCssClass(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - FilePond::widget( + $filePond = FilePond::widget( [ 'attribute' => 'array', - 'cssClass' => 'TestClass', + 'cssClass' => 'test-class', 'model' => new TestForm(), ], ); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $this->assertSame( + '', + $filePond, + ); - $this->assertStringContainsString('"className":"TestClass"', $result); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); + + $this->assertStringContainsString('"className":"test-class"', $result); } public function testConfig(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - FilePond::widget( + $filePond = FilePond::widget( [ 'attribute' => 'array', 'config' => ['forceRevert' => true, 'storeAsFile' => true], @@ -64,35 +73,13 @@ public function testConfig(): void ], ); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); $this->assertStringContainsString('"forceRevert":true,"storeAsFile":true', $result); } - public function testConfigWithClassName(): void - { - $this->mockApplication(); - - $filePond = FilePond::widget( - [ - 'attribute' => 'array', - 'cssClass' => 'test-class', - 'model' => new TestForm(), - ], - ); - - $this->assertSame( - '', - $filePond, - ); - } - public function testFileRename(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - $filePond = FilePond::widget( [ 'attribute' => 'array', @@ -110,7 +97,7 @@ functionFileRename() { $filePond, ); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); $this->assertStringContainsString('functionFileRename()', $result); @@ -138,7 +125,7 @@ functionFileRename() { $filePond, ); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); $this->assertStringContainsString('fileValidateTypeDetectType: (source, type) =>', $result); $this->assertStringContainsString('functionFileRename()', $result); @@ -146,11 +133,7 @@ functionFileRename() { public function testLabelIdle(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - FilePond::widget( + $filePond = FilePond::widget( [ 'attribute' => 'array', 'labelIdle' => 'Drag & Drop or Browse ', @@ -158,7 +141,7 @@ public function testLabelIdle(): void ], ); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); $this->assertStringContainsString( '"labelIdle":"Drag & Drop or Browse <\/span>"', @@ -168,11 +151,7 @@ public function testLabelIdle(): void public function testMaxFiles(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - FilePond::widget( + $filePond = FilePond::widget( [ 'attribute' => 'array', 'maxFiles' => 3, @@ -180,33 +159,31 @@ public function testMaxFiles(): void ], ); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); $this->assertStringContainsString('"maxFiles":3', $result); } public function testName(): void { - $this->mockApplication(); - $filePond = FilePond::widget( [ - 'attribute' => 'array', - 'model' => new TestForm(), - 'options' => ['name' => 'test-name'], + 'name' => 'filepond', ], ); $this->assertSame( - '', + '', $filePond, ); + + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); + + $this->assertStringContainsString('input[type="file"][id="w0-filepond"]', $result); } public function testNotClassFormControl(): void { - $this->mockApplication(); - $filePond = FilePond::widget( [ 'attribute' => 'array', @@ -219,17 +196,28 @@ public function testNotClassFormControl(): void '', $filePond, ); + + $filePond = FilePond::widget( + [ + 'attribute' => 'array', + 'cssClass' => 'form-control', + 'model' => new TestForm(), + ], + ); + + $this->assertSame( + '', + $filePond, + ); } - public function testNotClassPlaceholder(): void + public function testNotPlaceholder(): void { - $this->mockApplication(); - $filePond = FilePond::widget( [ 'attribute' => 'array', 'model' => new TestForm(), - 'options' => ['placeholder' => true], + 'options' => ['placeholder' => 'test-placeholder'], ], ); @@ -241,11 +229,7 @@ public function testNotClassPlaceholder(): void public function testPluingDefault(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - - FilePond::widget( + $filePond = FilePond::widget( [ 'attribute' => 'array', 'maxFiles' => 3, @@ -253,7 +237,7 @@ public function testPluingDefault(): void ], ); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); $this->assertStringContainsString('FilePondPluginFileEncode', $result); $this->assertStringContainsString('FilePondPluginFileValidateSize', $result); @@ -264,8 +248,6 @@ public function testPluingDefault(): void public function testRender(): void { - $this->mockApplication(); - $filePond = FilePond::widget( [ 'attribute' => 'array', @@ -277,14 +259,29 @@ public function testRender(): void '', $filePond, ); + + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); + + $this->assertStringContainsString( + <<jQuery(function ($) { + FilePond.registerPlugin(FilePondPluginFileEncode, FilePondPluginFileValidateSize, FilePondPluginFileValidateType, FilePondPluginImageExifOrientation, FilePondPluginImagePreview) + FilePond.setOptions({"acceptedFileTypes":[],"allowFileRename":false,"allowFileTypeValidation":true,"allowFileValidateSize":true,"allowImageCrop":false,"allowImageExifOrientation":true,"allowImagePreview":true,"allowImageTransform":false,"allowMultiple":false,"className":"","fileValidateTypeLabelExpectedTypes":"Expects {allButLastType} or {lastType}","imageCropAspectRatio":null,"imagePreviewHeight":null,"imagePreviewMarkupShow":true,"imagePreviewMaxFileSize":null,"imagePreviewMaxHeight":256,"imagePreviewMaxInstantPreviewFileSize":null,"imagePreviewMinHeight":44,"imagePreviewTransparencyIndicator":null,"imageTransformAfterCreateBlob":null,"imageTransformBeforeCreateBlob":null,"imageTransformClientTransforms":null,"imageTransformOutputQuality":null,"imageTransformOutputQualityMode":null,"imageTransformOutputStripImageHead":true,"imageTransformVariants":null,"imageTransformVariantsDefaultName":null,"imageTransformVariantsIncludeOriginal":true,"labelFileTypeNotAllowed":"File type not allowed","labelIdle":"Drag & Drop your files or Browse <\/span>","labelMaxFileSize":"Maximum file size is {filesize}","labelMaxFileSizeExceeded":"File is too large","labelMaxTotalFileSize":"Maximum total file size is {filesize}","labelMaxTotalFileSizeExceeded":"Maximum total size exceeded","maxFiles":1,"maxFileSize":null,"maxTotalFileSize":null,"minFileSize":null,"pdfPreviewHeight":320,"pdfComponentExtraParams":"toolbar=0&view=fit&page=1","required":false}) + + const loadFileDefault = "" + const pond = FilePond.create(document.querySelector('input[type="file"][id="testform-array"]'), ) + + if (loadFileDefault !== '') { + pond.addFiles(loadFileDefault) + } + }); + JS, + $result, + ); } public function testRequired(): void { - $this->mockApplication(); - - $view = Yii::$app->getView(); - $filePond = FilePond::widget( [ 'attribute' => 'array', @@ -293,7 +290,7 @@ public function testRequired(): void ], ); - $result = $view->renderFile(__DIR__ . '/Support/main.php'); + $result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]); $this->assertSame( '', diff --git a/tests/Support/main.php b/tests/Support/main.php index 94fe918..04009fa 100644 --- a/tests/Support/main.php +++ b/tests/Support/main.php @@ -4,6 +4,11 @@ /** * @var \yii\web\View $this + * @var string $widget */ -?> -beginPage();?>1head();?>2beginBody();?>3endBody();?>4endPage(); +$this->beginPage(); +$this->head(); +$this->beginBody(); +echo $widget; +$this->endBody(); +$this->endPage(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 7cf2163..98ed89f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,12 +9,15 @@ use yii\di\Container; use yii\i18n\PhpMessageSource; use yii\web\Application; +use yii\web\View; /** * This is the base class for all yii framework unit tests. */ abstract class TestCase extends \PHPUnit\Framework\TestCase { + protected View $view; + /** * Clean up after test. * By default the application created with [[mockApplication]] will be destroyed.