Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
MediaThumbnailEntity example
Context::
createDefaultContext
(
)
)
;
return
$this
->
getContainer
(
)
->
get
(
'media.repository'
)
->
search
(
new
Criteria
(
[
$mediaId
]
)
, Context::
createDefaultContext
(
)
)
->
get
(
$mediaId
)
;
}
private
function
createThumbnailFile
(
MediaEntity
$media
, string
$service
)
: string
{
$generator
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
;
$thumbnail
=
(
new
MediaThumbnailEntity
(
)
)
->
assign
(
[
'width' => 100, 'height' => 200
]
)
;
$thumbnailPath
=
$generator
->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
;
$this
->
getFilesystem
(
$service
)
->
write
(
$thumbnailPath
, 'foo'
)
;
return
$thumbnailPath
;
}
}
public
function
testAbsoluteThumbnailUrl
(
)
: void
{
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'fileName' => 'file.jpg',
]
)
;
$mediaThumbnailEntity
=
new
MediaThumbnailEntity
(
)
;
$mediaThumbnailEntity
->
assign
(
[
'width' => 100,
'height' => 100,
]
)
;
$urlGenerator
=
new
UrlGenerator
(
new
FilenamePathnameStrategy
(
)
,
new
Filesystem
(
new
InMemoryFilesystemAdapter
(
)
,
[
'public_url' => 'http://localhost:8000'
]
)
)
;
static
::
assertSame
(
'http://localhost:8000/thumbnail/d0/b3/24/file.jpg_100x100',
$urlGenerator
->
getAbsoluteThumbnailUrl
(
$mediaEntity
,
$mediaThumbnailEntity
)
)
;
]
,
]
,
]
,
$this
->context
)
;
$media
=
$this
->mediaRepository->
search
(
new
Criteria
(
[
$mediaId
]
)
,
$this
->context
)
->
get
(
$mediaId
)
;
static
::
assertInstanceOf
(
MediaEntity::
class
,
$media
)
;
$urlGenerator
=
$this
->
getContainer
(
)
->
get
(
UrlGeneratorInterface::
class
)
;
$mediaPath
=
$urlGenerator
->
getRelativeMediaUrl
(
$media
)
;
$thumbnailPath
=
$urlGenerator
->
getRelativeThumbnailUrl
(
$media
,
(
new
MediaThumbnailEntity
(
)
)
->
assign
(
[
'width' => 100, 'height' => 200
]
)
)
;
$resource
=
fopen
(
self::FIXTURE_FILE, 'rb'
)
;
static
::
assertNotFalse
(
$resource
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$mediaPath
,
$resource
)
;
$resource
=
fopen
(
self::FIXTURE_FILE, 'rb'
)
;
static
::
assertNotFalse
(
$resource
)
;
$this
->
getPublicFilesystem
(
)
->
writeStream
(
$thumbnailPath
,
$resource
)
;
$this
->mediaRepository->
delete
(
[
[
'id' =>
$mediaId
]
]
,
$this
->context
)
;
if
(
$type
=== null
)
{
throw
MediaException::
mediaTypeNotLoaded
(
$media
->
getId
(
)
)
;
}
try
{
foreach
(
$thumbnailSizes
as
$size
)
{
$thumbnailSize
=
$this
->
calculateThumbnailSize
(
$originalImageSize
,
$size
,
$config
)
;
$thumbnail
=
$this
->
createNewImage
(
$mediaImage
,
$type
,
$originalImageSize
,
$thumbnailSize
)
;
$url
=
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$media
,
(
new
MediaThumbnailEntity
(
)
)
->
assign
(
[
'width' =>
$size
->
getWidth
(
)
, 'height' =>
$size
->
getHeight
(
)
]
)
)
;
$this
->
writeThumbnail
(
$thumbnail
,
$media
,
$url
,
$config
->
getThumbnailQuality
(
)
)
;
$mediaFilesystem
=
$this
->
getFileSystem
(
$media
)
;
if
(
$originalImageSize
===
$thumbnailSize
&&
$mediaFilesystem
->
fileSize
(
$originalUrl
)
<
$mediaFilesystem
->
fileSize
(
$url
)
)
{
$mediaFilesystem
->
write
(
$url
,
$mediaFilesystem
->
read
(
$originalUrl
)
)
;
}
$savedThumbnails
[
]
=
[
'mediaId' =>
$media
->
getId
(
)
,
public
function
testAbsoluteThumbnailUrl
(
)
: void
{
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'fileName' => 'file.jpg',
]
)
;
$mediaThumbnailEntity
=
new
MediaThumbnailEntity
(
)
;
$mediaThumbnailEntity
->
assign
(
[
'width' => 100,
'height' => 100,
]
)
;
$urlGenerator
=
new
UrlGenerator
(
new
FilenamePathnameStrategy
(
)
,
new
Filesystem
(
new
InMemoryFilesystemAdapter
(
)
,
[
'public_url' => 'http://localhost:8000'
]
)
)
;
static
::
assertSame
(
'http://localhost:8000/thumbnail/d0/b3/24/file.jpg_100x100',
$urlGenerator
->
getAbsoluteThumbnailUrl
(
$mediaEntity
,
$mediaThumbnailEntity
)
)
;
use
PHPUnit\Framework\TestCase;
use
Shopware\Core\Content\Media\Aggregate\MediaThumbnail\MediaThumbnailEntity;
/** * @internal */
class
MediaThumbnailStructTest
extends
TestCase
{
public
function
testGetIdentifier
(
)
: void
{
$thumbnail
=
new
MediaThumbnailEntity
(
)
;
$thumbnail
->
setWidth
(
120
)
;
$thumbnail
->
setHeight
(
100
)
;
static
::
assertEquals
(
'120x100',
$thumbnail
->
getIdentifier
(
)
)
;
}
}
static
::
assertEquals
(
[
]
,
$mediaEntity
->
getThumbnails
(
)
->
getElements
(
)
)
;
}
public
function
testItAddsThumbnailUrl
(
)
: void
{
$subscriber
=
$this
->
getContainer
(
)
->
get
(
MediaLoadedSubscriber::
class
)
;
$context
= Context::
createDefaultContext
(
)
;
$mediaId
= '34556f108ab14969a0dcf9d9522fd7df';
$mimeType
= 'image/png';
$thumbnailEntity
=
new
MediaThumbnailEntity
(
)
;
$thumbnailEntity
->
setId
(
$mediaId
)
;
$thumbnailEntity
->
setHeight
(
100
)
;
$thumbnailEntity
->
setWidth
(
100
)
;
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
setId
(
$mediaId
)
;
$mediaEntity
->
setMimeType
(
$mimeType
)
;
$mediaEntity
->
setFileExtension
(
'png'
)
;
$mediaEntity
->
setFileName
(
$mediaId
. '-134578345'
)
;
$mediaEntity
->
setThumbnails
(
new
MediaThumbnailCollection
(
[
$thumbnailEntity
]
)
)
;
$this
->mediaRepository->
update
(
[
[
'id' =>
$png
->
getId
(
)
,
'thumbnails' =>
[
[
'width' => 100,
'height' => 100,
'highDpi' => false,
]
,
]
,
]
]
,
$context
)
;
$oldMediaPath
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$png
)
;
$oldThumbnailPath
=
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$png
,
(
new
MediaThumbnailEntity
(
)
)
->
assign
(
[
'width' => 100, 'height' => 100
]
)
)
;
$this
->
getPublicFilesystem
(
)
->
write
(
$oldMediaPath
, 'test file content'
)
;
$this
->
getPublicFilesystem
(
)
->
write
(
$oldThumbnailPath
, 'test file content'
)
;
$this
->fileSaver->
renameMedia
(
$png
->
getId
(
)
, 'new destination',
$context
)
;
$updatedMedia
=
$this
->mediaRepository->
search
(
new
Criteria
(
[
$png
->
getId
(
)
]
)
,
$context
)
->
get
(
$png
->
getId
(
)
)
;
static
::
assertInstanceOf
(
MediaEntity::
class
,
$updatedMedia
)
;
static
::
assertFalse
(
$this
->
getPublicFilesystem
(
)
->
has
(
$oldMediaPath
)
)
;
static
::
assertTrue
(
$this
->
getPublicFilesystem
(
)
->
has
(
$this
->urlGenerator->
getRelativeMediaUrl
(
$updatedMedia
)
)
)
;
static
::
assertFalse
(
$this
->
getPublicFilesystem
(
)
->
has
(
$oldThumbnailPath
)
)
;