Skip to content

Commit e0a33a2

Browse files
authored
fix: issues with component rendering correctly. (#162)
This fixes #153 and adds tests for other components, including finding out the video wasn't rendering correctly.
1 parent 2ce3e28 commit e0a33a2

File tree

5 files changed

+136
-26
lines changed

5 files changed

+136
-26
lines changed

tests/Unit/ImageComponentTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
beforeEach(function () {
4+
$this->app->instance(
5+
\Cloudinary\Cloudinary::class,
6+
new \Cloudinary\Cloudinary('cloudinary://key:secret@demo')
7+
);
8+
});
9+
10+
it('renders image component with both class and alt attributes', function () {
11+
$html = view('cloudinary::components.image', [
12+
'publicId' => 'example',
13+
'class' => 'img-fluid',
14+
'alt' => 'Parallax Background Image',
15+
])->render();
16+
17+
expect($html)->toContain('class="img-fluid"');
18+
expect($html)->toContain('alt="Parallax Background Image"');
19+
});
20+
21+
it('includes crop, width and height transformation when provided', function () {
22+
$html = view('cloudinary::components.image', [
23+
'publicId' => 'example',
24+
'crop' => 'fill',
25+
'width' => 300,
26+
'height' => 200,
27+
])->render();
28+
29+
expect($html)->toContain('w_');
30+
expect($html)->toContain('h_');
31+
expect($html)->toContain('c_');
32+
});
33+
34+
it('includes rotation when rotate attribute is provided', function () {
35+
$html = view('cloudinary::components.image', [
36+
'publicId' => 'example',
37+
'rotate' => 90,
38+
])->render();
39+
40+
expect($html)->toContain('a_');
41+
});
42+
43+
it('applies grayscale effect when requested', function () {
44+
$html = view('cloudinary::components.image', [
45+
'publicId' => 'example',
46+
'grayscale' => true,
47+
])->render();
48+
49+
expect($html)->toContain('e_grayscale');
50+
});
51+
52+
it('applies round corners when requested', function () {
53+
$html = view('cloudinary::components.image', [
54+
'publicId' => 'example',
55+
'roundCorners' => true,
56+
])->render();
57+
58+
expect($html)->toContain('r_');
59+
});
60+
61+
it('preserves multiple classes when provided', function () {
62+
$html = view('cloudinary::components.image', [
63+
'publicId' => 'example',
64+
'class' => 'img-fluid rounded mx-auto',
65+
])->render();
66+
67+
expect($html)->toContain('class="img-fluid rounded mx-auto"');
68+
});

tests/Unit/VideoComponentTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
beforeEach(function () {
4+
$this->app->instance(
5+
\Cloudinary\Cloudinary::class,
6+
new \Cloudinary\Cloudinary('cloudinary://key:secret@demo')
7+
);
8+
});
9+
10+
it('renders video component with default attributes and method', function () {
11+
$html = view('cloudinary::components.video', [
12+
'publicId' => 'example',
13+
'width' => 640,
14+
'height' => 360,
15+
])->render();
16+
17+
expect($html)->toContain('controls');
18+
expect($html)->toContain('preload');
19+
expect($html)->toContain('src="');
20+
});

tests/Unit/WidgetComponentTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
beforeEach(function () {
4+
$this->app->instance(
5+
\Cloudinary\Cloudinary::class,
6+
new \Cloudinary\Cloudinary('cloudinary://key:secret@demo')
7+
);
8+
});
9+
10+
it('renders widget component button with onclick and slot content', function () {
11+
$html = view('cloudinary::components.widget', [
12+
'slot' => 'Upload',
13+
'attributes' => '',
14+
])->render();
15+
16+
expect($html)->toContain('onclick="openWidget()"');
17+
expect($html)->toContain('Upload');
18+
expect($html)->toContain('cloudinary.com');
19+
});

views/components/image.blade.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,22 @@
1515
use Cloudinary\Transformation\ImproveMode;
1616
use Illuminate\Support\Str;
1717
18-
$retrieveFormattedImage = cloudinary()->imageTag($publicId ?? '');
18+
$retrieveFormattedImage = cloudinary()->imageTag($publicId ?? '');
19+
20+
$attrs = [];
1921
2022
/**
2123
* SET ALT if provided
2224
*/
2325
if (isset($alt)) {
24-
$retrieveFormattedImage = cloudinary()
25-
->imageTag($publicId ?? '')
26-
->setAttributes([
27-
'alt' => $alt ?? $publicId,
28-
]);
26+
$attrs['alt'] = $alt ?? $publicId;
2927
}
3028
3129
/**
3230
* SET CLASS if provided
3331
*/
3432
if (isset($class)) {
35-
$retrieveFormattedImage = cloudinary()
36-
->imageTag($publicId ?? '')
37-
->setAttributes([
38-
'class' => $class,
39-
]);
33+
$attrs['class'] = $class;
4034
}
4135
4236
/**
@@ -85,13 +79,11 @@
8579
$cropFactor = 'minimumPad';
8680
}
8781
88-
$retrieveFormattedImage = cloudinary()
89-
->imageTag($publicId ?? '')
90-
->resize(
91-
Resize::$cropFactor()
92-
->width($width ?? '')
93-
->height($height ?? ''),
94-
);
82+
$retrieveFormattedImage = $retrieveFormattedImage->resize(
83+
Resize::$cropFactor()
84+
->width($width ?? '')
85+
->height($height ?? ''),
86+
);
9587
9688
/**
9789
* If the attribute is "gravity"
@@ -123,9 +115,7 @@
123115
->gravity(Gravity::focusOn(FocusOn::$gravity()));
124116
}
125117
126-
$retrieveFormattedImage = cloudinary()
127-
->imageTag($publicId ?? '')
128-
->resize($gravityImplementation);
118+
$retrieveFormattedImage = $retrieveFormattedImage->resize($gravityImplementation);
129119
}
130120
}
131121
}
@@ -484,6 +474,11 @@
484474
}
485475
}
486476
487-
echo $retrieveFormattedImage->serialize();
477+
// Apply collected attributes once so we don't overwrite previous attributes.
478+
if (!empty($attrs)) {
479+
$retrieveFormattedImage = $retrieveFormattedImage->setAttributes($attrs);
480+
}
481+
482+
echo $retrieveFormattedImage->serialize();
488483
489484
@endphp

views/components/video.blade.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
@php
22
$defaultFormatMethod = 'scale';
33
$retrieveFormattedVideo = cloudinary()
4-
->videoTag($publicId ?? '')
5-
->setAttributes(['controls', 'loop', 'preload'])
6-
->fallback('Your browser does not support HTML5 video tagsssss.')
7-
->$defaultFormatMethod($width ?? '', $height ?? '');
4+
->videoTag($publicId ?? '')
5+
->setAttributes([
6+
'controls' => true,
7+
'loop' => true,
8+
'preload' => 'auto',
9+
])
10+
->fallback('Your browser does not support HTML5 video tags.')
11+
->$defaultFormatMethod($width ?? '', $height ?? '');
12+
@endphp
13+
14+
@php
15+
echo $retrieveFormattedVideo->serialize();
816
@endphp

0 commit comments

Comments
 (0)