Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
missingFile example
$this
->
expectExceptionMessage
(
MediaException::
mediaNotFound
(
$id
)
->
getMessage
(
)
)
;
$context
= Context::
createDefaultContext
(
)
;
$this
->fileSaver->
renameMedia
(
$id
, 'new file destination',
$context
)
;
}
public
function
testRenameMediaThrowsExceptionIfMediaHasNoFileAttached
(
)
: void
{
$id
= Uuid::
randomHex
(
)
;
$this
->
expectException
(
MediaException::
class
)
;
$this
->
expectExceptionMessage
(
MediaException::
missingFile
(
$id
)
->
getMessage
(
)
)
;
$context
= Context::
createDefaultContext
(
)
;
$this
->mediaRepository->
create
(
[
[
'id' =>
$id
,
]
,
]
,
$context
)
;
$this
->fileSaver->
renameMedia
(
$id
, 'new destination',
$context
)
;
}
$this
->messageBus->
dispatch
(
$message
)
;
}
public
function
renameMedia
(
string
$mediaId
, string
$destination
, Context
$context
)
: void
{
$destination
=
$this
->
validateFileName
(
$destination
)
;
$currentMedia
=
$this
->
findMediaById
(
$mediaId
,
$context
)
;
$fileExtension
=
$currentMedia
->
getFileExtension
(
)
;
if
(
!
$currentMedia
->
hasFile
(
)
|| !
$fileExtension
)
{
throw
MediaException::
missingFile
(
$mediaId
)
;
}
if
(
$destination
===
$currentMedia
->
getFileName
(
)
)
{
return
;
}
$this
->
ensureFileNameIsUnique
(
$currentMedia
,
$destination
,
$fileExtension
,
$context
)
;
$mediaIds
=
[
]
;
if
(
!
empty
(
$media
)
)
{
$mediaIds
=
array_column
(
$media
, 'media'
)
;
$this
->mediaRepository->
create
(
$mediaIds
,
$context
)
;
foreach
(
$media
as
$item
)
{
if
(
!
$item
[
'mediaFile'
]
instanceof MediaFile
)
{
throw
MediaException::
missingFile
(
$item
[
'media'
]
[
'id'
]
)
;
}
try
{
$this
->fileSaver->
persistFileToMedia
(
$item
[
'mediaFile'
]
,
$item
[
'basename'
]
,
$item
[
'media'
]
[
'id'
]
,
$context
)
;
}
catch
(
MediaException
$e
)
{
if
(
$e
->
getErrorCode
(
)
!== MediaException::MEDIA_DUPLICATED_FILE_NAME
)
{
throw
$e
;
}
$newFileName
=
$this
->fileNameProvider->
provide
(
$item
[
'basename'
]
,
static
::
assertEquals
(
Response::HTTP_CONFLICT,
$exception
->
getStatusCode
(
)
)
;
static
::
assertEquals
(
MediaException::MEDIA_DUPLICATED_FILE_NAME,
$exception
->
getErrorCode
(
)
)
;
static
::
assertEquals
(
'A file with the name "file-name.file-extension" already exists.',
$exception
->
getMessage
(
)
)
;
static
::
assertEquals
(
[
'fileName' =>
$fileName
, 'fileExtension' =>
$fileExtension
]
,
$exception
->
getParameters
(
)
)
;
}
public
function
testMissingFile
(
)
: void
{
$mediaId
= 'media-id';
$exception
= MediaException::
missingFile
(
$mediaId
)
;
static
::
assertEquals
(
Response::HTTP_NOT_FOUND,
$exception
->
getStatusCode
(
)
)
;
static
::
assertEquals
(
MediaException::MEDIA_MISSING_FILE,
$exception
->
getErrorCode
(
)
)
;
static
::
assertEquals
(
'Could not find file for media with id: "media-id"',
$exception
->
getMessage
(
)
)
;
static
::
assertEquals
(
[
'mediaId' =>
$mediaId
]
,
$exception
->
getParameters
(
)
)
;
}
public
function
testMediaFolderIdNotFound
(
)
: void
{
$folderId
= 'folder-id';