From 37e716ad3059710a762ae909174515d97aab8d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Roberto=20P=2E=20Borges?= Date: Sat, 20 Oct 2018 15:52:06 -0300 Subject: [PATCH 1/2] Fix PSR2 --- src/Exceptions/InvalidImageFieldException.php | 2 +- src/ImageUpServiceProvider.php | 4 +- tests/Hooks/CopyImageHook.php | 2 +- tests/Hooks/ResizeToFiftyHook.php | 2 +- tests/ImageResizeTest.php | 36 ++++++----- tests/ImageUpTest.php | 64 +++++++++---------- tests/Models/User.php | 2 + tests/TestCase.php | 3 +- 8 files changed, 59 insertions(+), 56 deletions(-) diff --git a/src/Exceptions/InvalidImageFieldException.php b/src/Exceptions/InvalidImageFieldException.php index d57c49c..875dd43 100644 --- a/src/Exceptions/InvalidImageFieldException.php +++ b/src/Exceptions/InvalidImageFieldException.php @@ -1,7 +1,7 @@ mergeConfigFrom( - __DIR__.'/../config/imageup.php', 'imageup' + __DIR__.'/../config/imageup.php', + 'imageup' ); } @@ -29,6 +30,5 @@ public function boot() */ public function register() { - } } diff --git a/tests/Hooks/CopyImageHook.php b/tests/Hooks/CopyImageHook.php index 7cdad3a..09e5219 100644 --- a/tests/Hooks/CopyImageHook.php +++ b/tests/Hooks/CopyImageHook.php @@ -15,4 +15,4 @@ public function handle($image) 'public' ); } -} \ No newline at end of file +} diff --git a/tests/Hooks/ResizeToFiftyHook.php b/tests/Hooks/ResizeToFiftyHook.php index 99afa59..552c852 100644 --- a/tests/Hooks/ResizeToFiftyHook.php +++ b/tests/Hooks/ResizeToFiftyHook.php @@ -8,4 +8,4 @@ public function handle($image) { $image->resize(50, 50); } -} \ No newline at end of file +} diff --git a/tests/ImageResizeTest.php b/tests/ImageResizeTest.php index 0ec843c..5718e41 100644 --- a/tests/ImageResizeTest.php +++ b/tests/ImageResizeTest.php @@ -7,8 +7,8 @@ class ImageResizeTest extends TestCase { - protected $testImage = __DIR__ . '/images/test1200x1200.png'; - protected $newImage; + protected $testImage = __DIR__ . '/images/test1200x1200.png'; + protected $newImage; protected $user; protected function setUp() @@ -26,7 +26,7 @@ protected function tearDown() { parent::tearDown(); - if( $this->newImage ) { + if ($this->newImage) { $this->newImage->destroy(); } } @@ -37,7 +37,7 @@ protected function tearDown() * * @test */ - function it_resize_image_based_on_width_given() + public function it_resize_image_based_on_width_given() { $this->newImage = $this->user->resizeImage($this->testImage, ['width' => 300]); @@ -49,7 +49,7 @@ function it_resize_image_based_on_width_given() * * @test */ - function it_resize_image_by_height() + public function it_resize_image_by_height() { $this->newImage = $this->user->resizeImage($this->testImage, ['height' => 200]); @@ -61,14 +61,16 @@ function it_resize_image_by_height() * * @test */ - function it_crops_image_in_given_width_and_height() + public function it_crops_image_in_given_width_and_height() { - $this->newImage = $this->user->resizeImage($this->testImage, + $this->newImage = $this->user->resizeImage( + $this->testImage, [ 'width' => 150, 'height' => 150, 'crop' => true - ]); + ] + ); $this->assertEquals(150, $this->newImage->width()); $this->assertEquals(150, $this->newImage->height()); @@ -79,14 +81,16 @@ function it_crops_image_in_given_width_and_height() * * @test */ - function it_crops_in_x_and_y_if_crop_is_set_to_array_of_coordinates() + public function it_crops_in_x_and_y_if_crop_is_set_to_array_of_coordinates() { - $this->newImage = $this->user->resizeImage($this->testImage, + $this->newImage = $this->user->resizeImage( + $this->testImage, [ 'width' => 100, 'height' => 100, 'crop' => [25, 10] - ]); + ] + ); $this->assertEquals(100, $this->newImage->width()); $this->assertEquals(100, $this->newImage->height()); @@ -97,14 +101,16 @@ function it_crops_in_x_and_y_if_crop_is_set_to_array_of_coordinates() * * @test */ - function it_can_override_the_crop_x_and_y_coordinates() + public function it_can_override_the_crop_x_and_y_coordinates() { - $this->newImage = $this->user->cropTo(10, 0)->resizeImage($this->testImage, + $this->newImage = $this->user->cropTo(10, 0)->resizeImage( + $this->testImage, [ 'width' => 100, 'height' => 100, 'crop' => [25, 10] - ]); + ] + ); $this->assertEquals(100, $this->newImage->width()); $this->assertEquals(100, $this->newImage->height()); @@ -115,7 +121,7 @@ function it_can_override_the_crop_x_and_y_coordinates() * * @test */ - function it_do_not_resize_if_width_and_height_are_not_provided() + public function it_do_not_resize_if_width_and_height_are_not_provided() { $this->newImage = $this->user->resizeImage($this->testImage, []); diff --git a/tests/ImageUpTest.php b/tests/ImageUpTest.php index 66e6a2d..edc4406 100644 --- a/tests/ImageUpTest.php +++ b/tests/ImageUpTest.php @@ -23,10 +23,9 @@ public function setUp() * * @test */ - function it_gets_image_field_options() + public function it_gets_image_field_options() { - $user = new class() extends User - { + $user = new class() extends User { use HasImageUploads; protected static $imageFields = ['avatar' => [ @@ -50,7 +49,7 @@ function it_gets_image_field_options() * * @test */ - function it_throws_exception_if_image_field_not_found() + public function it_throws_exception_if_image_field_not_found() { $user = new User(); @@ -63,7 +62,7 @@ function it_throws_exception_if_image_field_not_found() * * @test */ - function it_sets_image_field_with_options() + public function it_sets_image_field_with_options() { $user = new User(); @@ -78,7 +77,7 @@ function it_sets_image_field_with_options() * * @test */ - function it_sets_image_fields_with_mixed_option_and_without_options() + public function it_sets_image_fields_with_mixed_option_and_without_options() { $user = new User(); @@ -95,7 +94,7 @@ function it_sets_image_fields_with_mixed_option_and_without_options() * * @test */ - function it_sets_image_fields_without_any_options() + public function it_sets_image_fields_without_any_options() { $user = new User(); @@ -112,7 +111,7 @@ function it_sets_image_fields_without_any_options() * * @test */ - function it_returns_first_field_if_no_key_provided() + public function it_returns_first_field_if_no_key_provided() { $user = new User(); $fieldOption = [ @@ -129,7 +128,7 @@ function it_returns_first_field_if_no_key_provided() * * @test */ - function it_returns_field_name_of_first_field() + public function it_returns_field_name_of_first_field() { $user = new User(); $fieldOption = [ @@ -147,7 +146,7 @@ function it_returns_field_name_of_first_field() * * @test */ - function it_uploads_image_and_saves_in_db() + public function it_uploads_image_and_saves_in_db() { $user = $this->createUser(); Storage::fake('public'); @@ -168,7 +167,7 @@ function it_uploads_image_and_saves_in_db() * * @test */ - function it_uploads_image_by_field_name() + public function it_uploads_image_by_field_name() { $user = $this->createUser(); $user->setImagesField(['cover' => ['width' => 100]]); @@ -190,13 +189,12 @@ function it_uploads_image_by_field_name() * * @test */ - function it_gives_image_url_if_image_saved_in_db() + public function it_gives_image_url_if_image_saved_in_db() { - $user = new class extends User - { + $user = new class extends User { use HasImageUploads; - static $imageFields = [ + public static $imageFields = [ 'avatar' => ['placeholder' => '/images/cover-placeholder.png'] ]; }; @@ -218,13 +216,12 @@ function it_gives_image_url_if_image_saved_in_db() * * @test */ - function it_gives_placeholder_image_url_if_file_has_no_image_and_placeholder_option_is_defined() + public function it_gives_placeholder_image_url_if_file_has_no_image_and_placeholder_option_is_defined() { - $user = new class extends User - { + $user = new class extends User { use HasImageUploads; - static $imageFields = [ + public static $imageFields = [ 'cover' => ['placeholder' => '/images/cover-placeholder.png'] ]; }; @@ -245,13 +242,12 @@ function it_gives_placeholder_image_url_if_file_has_no_image_and_placeholder_opt * * @test */ - function it_validate_the_uploaded_file_using_provided_rules() + public function it_validate_the_uploaded_file_using_provided_rules() { - $user = new class extends User - { + $user = new class extends User { use HasImageUploads; - static $imageFields = [ + public static $imageFields = [ 'avatar' => [ 'rules' => 'required|image' ] @@ -281,7 +277,7 @@ function it_validate_the_uploaded_file_using_provided_rules() * * @test */ - function it_uploads_and_resize_image_in_proportion_if_crop_is_not_set() + public function it_uploads_and_resize_image_in_proportion_if_crop_is_not_set() { $user = $this->createUser([], [ 'avatar' => [ @@ -310,7 +306,7 @@ function it_uploads_and_resize_image_in_proportion_if_crop_is_not_set() * * @test */ - function it_upload_and_resize_image_by_given_height() + public function it_upload_and_resize_image_by_given_height() { $user = $this->createUser([], [ 'avatar' => [ @@ -338,7 +334,7 @@ function it_upload_and_resize_image_by_given_height() * * @test */ - function it_uses_disk_specified_in_field_option() + public function it_uses_disk_specified_in_field_option() { $user = $this->createUser([], [ 'avatar' => [ @@ -363,7 +359,7 @@ function it_uses_disk_specified_in_field_option() * * @test */ - function it_uses_path_specified_in_field_option() + public function it_uses_path_specified_in_field_option() { $user = $this->createUser([], [ 'avatar' => [ @@ -389,7 +385,7 @@ function it_uses_path_specified_in_field_option() * * @test */ - function it_auto_uploads_images_if_config_is_set_do_it() + public function it_auto_uploads_images_if_config_is_set_do_it() { Storage::fake('public'); $image = UploadedFile::fake()->image('avatar.jpg')->size(100); @@ -411,7 +407,7 @@ function it_auto_uploads_images_if_config_is_set_do_it() * * @test */ - function it_triggers_before_save_hook_from_a_class() + public function it_triggers_before_save_hook_from_a_class() { $user = $this->createUser([], [ 'avatar' => [ @@ -437,13 +433,13 @@ function it_triggers_before_save_hook_from_a_class() * * @test */ - function it_triggers_before_save_hook_from_a_callback() + public function it_triggers_before_save_hook_from_a_callback() { $user = $this->createUser([], [ 'avatar' => [ 'width' => 100, 'height' => 100, - 'before_save' => function($image) { + 'before_save' => function ($image) { $image->resize(50, 50); }, ] @@ -465,7 +461,7 @@ function it_triggers_before_save_hook_from_a_callback() * * @test */ - function it_triggers_after_save_hook_from_a_class() + public function it_triggers_after_save_hook_from_a_class() { $user = $this->createUser([], [ 'avatar' => [ @@ -493,11 +489,11 @@ function it_triggers_after_save_hook_from_a_class() * * @test */ - function it_triggers_after_save_hook_from_a_callback() + public function it_triggers_after_save_hook_from_a_callback() { $user = $this->createUser([], [ 'avatar' => [ - 'after_save' => function($image) { + 'after_save' => function ($image) { Storage::disk('public')->put( 'uploads/copy_from_hook.jpg', (string)$image->encode(null, 80), diff --git a/tests/Models/User.php b/tests/Models/User.php index 9dba720..e612eb1 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -1,5 +1,7 @@ ['width' => 200], 'cover' => ['width' => 400, 'height' => 400] From 4482284095f5eb0d6903a9df3f9e3c442860bc89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Roberto=20P=2E=20Borges?= Date: Sat, 20 Oct 2018 20:25:58 -0300 Subject: [PATCH 2/2] Fix bugs & improvements tests coverage --- src/HasImageUploads.php | 46 ++++++---- tests/Controllers/UserController.php | 35 +++++++ tests/ImageUpTest.php | 131 +++++++++++++++++++++++++++ tests/TestCase.php | 5 +- 4 files changed, 198 insertions(+), 19 deletions(-) diff --git a/src/HasImageUploads.php b/src/HasImageUploads.php index aa0e209..5587aa4 100644 --- a/src/HasImageUploads.php +++ b/src/HasImageUploads.php @@ -217,14 +217,17 @@ public function getImageFieldOptions($field = null) { // get first option if no field provided if (is_null($field)) { - $options = array_first($this->getDefinedImageFields()); + $imagesFields = $this->getDefinedImageFields(); - if (!$options) { + if (!$imagesFields) { throw new InvalidImageFieldException( 'No image fields are defined in $imageFields array on model.' ); } + $fieldKey = array_first(array_keys($imagesFields)); + $options = is_int($fieldKey) ? [] : array_first($imagesFields); + return $options; } @@ -235,7 +238,7 @@ public function getImageFieldOptions($field = null) ); } - return array_get($this->getDefinedImageFields(), $field); + return array_get($this->getDefinedImageFields(), $field, []); } /** @@ -262,9 +265,13 @@ public function getImageFieldName($field = null) return $field; } + $imagesFields = $this->getDefinedImageFields(); + $fieldKey = array_first(array_keys($imagesFields)); + // return first field name - $fieldKey = array_keys($this->getDefinedImageFields()); - return array_first($fieldKey); + return is_int($fieldKey) + ? $imagesFields[$fieldKey] + : $fieldKey; } /** @@ -466,21 +473,26 @@ protected function validationFactory() */ protected function autoUpload() { - foreach ($this->getDefinedImageFields() as $field => $options) { + foreach ($this->getDefinedImageFields() as $key => $val) { + $field = is_numeric($key) ? $val : $key; + $options = array_wrap($val); + // check if global upload is allowed, then in override in option $autoUploadAllowed = array_get($options, 'auto_upload', $this->canAutoUploadImages()); - if (is_array($options) && count($options) && $autoUploadAllowed) { - // get the input file name - $requestFileName = array_get($options, 'file_input', $field); - - // if request has the file upload it - if (request()->hasFile($requestFileName)) { - $this->uploadImage( - request()->file($requestFileName), - $field - ); - } + if (! $autoUploadAllowed) { + continue; + } + + // get the input file name + $requestFileName = array_get($options, 'file_input', $field); + + // if request has the file upload it + if (request()->hasFile($requestFileName)) { + $this->uploadImage( + request()->file($requestFileName), + $field + ); } } } diff --git a/tests/Controllers/UserController.php b/tests/Controllers/UserController.php index 1a93f5e..6a8eeaf 100644 --- a/tests/Controllers/UserController.php +++ b/tests/Controllers/UserController.php @@ -11,14 +11,49 @@ class UserController extends Controller public function store(Request $request) { $user = new User(); + $fieldOption = [ 'avatar' => ['width' => 200], 'cover' => ['width' => 400, 'height' => 400] ]; + $user->setImagesField($fieldOption); + $user->forceFill($request->all())->save(); + + return $user; + } + + public function storeImagesWithoutOptions(Request $request) + { + $user = new User(); + + $fieldOption = [ + 'avatar', + 'cover' + ]; + $user->setImagesField($fieldOption); $user->forceFill($request->all())->save(); return $user; } + + public function storeImagesWithMixedOptions(Request $request) + { + $user = new User(); + + $fieldOption = [ + 'avatar', + 'cover' => [ + 'width' => 400, + 'height' => 400, + 'auto_upload' => false + ], + ]; + + $user->setImagesField($fieldOption); + $user->forceFill($request->except('cover'))->save(); + + return $user; + } } diff --git a/tests/ImageUpTest.php b/tests/ImageUpTest.php index edc4406..ee1070a 100644 --- a/tests/ImageUpTest.php +++ b/tests/ImageUpTest.php @@ -141,6 +141,26 @@ public function it_returns_field_name_of_first_field() $this->assertEquals('logo', $user->getImageFieldName('logo')); } + /** + * it returns first field without any options + * + * @test + */ + public function it_returns_first_field_without_any_options() + { + $user = new User(); + $fieldOption = [ + 'avatar', + 'logo' => ['width' => 300, 'height' => 300] + ]; + $user->setImagesField($fieldOption); + + $this->assertEquals('avatar', $user->getImageFieldName()); + $this->assertEquals('avatar', $user->getImageFieldName('avatar')); + $this->assertSame([], $user->getImageFieldOptions()); + $this->assertSame([], $user->getImageFieldOptions('avatar')); + } + /** * it uploads image and saves in db * @@ -402,6 +422,117 @@ public function it_auto_uploads_images_if_config_is_set_do_it() Storage::disk('public')->assertExists('uploads/' . $image->hashName()); } + /** + * it auto upload images + * + * @test + */ + public function it_auto_upload_images() + { + Storage::fake('public'); + + $cover = UploadedFile::fake()->image('cover.jpg'); + $avatar = UploadedFile::fake()->image('avatar.jpg'); + + $data = [ + 'name' => 'Saqueib', + 'email' => 'me@example.com', + 'password' => 'secret', + 'avatar' => $avatar, + 'cover' => $cover, + ]; + + $response = $this->post('/test/users', $data); + $user = $response->original; + + $response->assertStatus(200); + + // Assert the file was stored... + Storage::disk('public')->assertExists('uploads/' . $avatar->hashName()); + Storage::disk('public')->assertExists('uploads/' . $cover->hashName()); + + + $this->assertNotNull($user->avatar); + $this->assertNotNull($user->cover); + + $this->assertEquals('uploads/' . $avatar->hashName(), $user->avatar); + $this->assertEquals('uploads/' . $cover->hashName(), $user->cover); + } + + /** + * it auto upload images without options + * + * @test + */ + public function it_auto_upload_images_without_options() + { + Storage::fake('public'); + + $cover = UploadedFile::fake()->image('cover.jpg'); + $avatar = UploadedFile::fake()->image('avatar.jpg'); + + $data = [ + 'name' => 'John Doe', + 'email' => 'john@example.com', + 'password' => 'secret', + 'avatar' => $avatar, + 'cover' => $cover, + ]; + + $response = $this->post('/test/users/uploads/images-without-options', $data); + $user = $response->original; + + $response->assertStatus(200); + + // Assert the file was stored... + Storage::disk('public')->assertExists('uploads/' . $avatar->hashName()); + Storage::disk('public')->assertExists('uploads/' . $cover->hashName()); + + + $this->assertNotNull($user->avatar); + $this->assertNotNull($user->cover); + + $this->assertEquals('uploads/' . $avatar->hashName(), $user->avatar); + $this->assertEquals('uploads/' . $cover->hashName(), $user->cover); + } + + /** + * it auto upload images with mixed options + * + * @test + */ + public function it_auto_upload_images_with_mixed_options() + { + Storage::fake('public'); + + $cover = UploadedFile::fake()->image('cover.jpg'); + $avatar = UploadedFile::fake()->image('avatar.jpg'); + + $data = [ + 'name' => 'Foo Bar', + 'email' => 'foo@bar.com', + 'password' => 'secret', + 'avatar' => $avatar, + 'cover' => $cover, + ]; + + $response = $this->post('/test/users/uploads/images-with-mixed-options', $data); + $user = $response->original; + + $response->assertStatus(200); + + // Assert the file was stored... + Storage::disk('public')->assertExists('uploads/' . $avatar->hashName()); + Storage::disk('public')->assertMissing('uploads/' . $cover->hashName()); + + + $this->assertNotNull($user->avatar); + $this->assertNull($user->cover); + + $this->assertEquals('uploads/' . $avatar->hashName(), $user->avatar); + $this->assertNotEquals('uploads/' . $cover->hashName(), $user->cover); + } + /** * it triggers before save hook from a class. * diff --git a/tests/TestCase.php b/tests/TestCase.php index 1a59702..d89d7da 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -27,7 +27,8 @@ protected function getEnvironmentSetUp($app) // define some route to test auto upload Route::group(['namespace' => 'QCod\ImageUp\Tests\Controllers'], function () { Route::post('test/users', 'UserController@store'); - Route::put('test/users/{id}', 'UserController@update'); + Route::post('test/users/uploads/images-without-options', 'UserController@storeImagesWithoutOptions'); + Route::post('test/users/uploads/images-with-mixed-options', 'UserController@storeImagesWithMixedOptions'); }); } @@ -68,7 +69,7 @@ protected function createUser($attributes = [], $imageFields = null) if (is_null($imageFields)) { $fieldOption = [ - 'avatar' => ['width' => 200], + 'avatar', 'cover' => ['width' => 400, 'height' => 400] ]; $user->setImagesField($fieldOption);