Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
b84f1451
Unverified
Commit
b84f1451
authored
Sep 22, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Creating logic to update a folder in db
parent
a8295577
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
103 additions
and
0 deletions
+103
-0
FolderIsUpdating.php
Modules/Media/Events/FolderIsUpdating.php
+10
-0
FolderWasUpdated.php
Modules/Media/Events/FolderWasUpdated.php
+23
-0
EloquentFolderRepository.php
.../Media/Repositories/Eloquent/EloquentFolderRepository.php
+16
-0
EloquentFolderRepositoryTest.php
Modules/Media/Tests/EloquentFolderRepositoryTest.php
+54
-0
No files found.
Modules/Media/Events/FolderIsUpdating.php
0 → 100644
View file @
b84f1451
<?php
namespace
Modules\Media\Events
;
use
Modules\Core\Contracts\EntityIsChanging
;
use
Modules\Core\Events\AbstractEntityHook
;
class
FolderIsUpdating
extends
AbstractEntityHook
implements
EntityIsChanging
{
}
Modules/Media/Events/FolderWasUpdated.php
0 → 100644
View file @
b84f1451
<?php
namespace
Modules\Media\Events
;
use
Modules\Media\Entities\File
;
class
FolderWasUpdated
{
/**
* @var File
*/
public
$folder
;
/**
* @var array
*/
public
$data
;
public
function
__construct
(
File
$folder
,
array
$data
)
{
$this
->
folder
=
$folder
;
$this
->
data
=
$data
;
}
}
Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php
View file @
b84f1451
...
@@ -5,7 +5,9 @@ namespace Modules\Media\Repositories\Eloquent;
...
@@ -5,7 +5,9 @@ namespace Modules\Media\Repositories\Eloquent;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FolderIsCreating
;
use
Modules\Media\Events\FolderIsCreating
;
use
Modules\Media\Events\FolderIsUpdating
;
use
Modules\Media\Events\FolderWasCreated
;
use
Modules\Media\Events\FolderWasCreated
;
use
Modules\Media\Events\FolderWasUpdated
;
use
Modules\Media\Repositories\FolderRepository
;
use
Modules\Media\Repositories\FolderRepository
;
class
EloquentFolderRepository
extends
EloquentBaseRepository
implements
FolderRepository
class
EloquentFolderRepository
extends
EloquentBaseRepository
implements
FolderRepository
...
@@ -36,6 +38,20 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
...
@@ -36,6 +38,20 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return
$folder
;
return
$folder
;
}
}
public
function
update
(
$model
,
$data
)
{
$formattedData
=
[
'filename'
=>
array_get
(
$data
,
'name'
),
'path'
=>
$this
->
getPath
(
$data
),
];
event
(
$event
=
new
FolderIsUpdating
(
$formattedData
));
$model
->
update
(
$event
->
getAttributes
());
event
(
new
FolderWasUpdated
(
$model
,
$formattedData
));
return
$model
;
}
/**
/**
* @param array $data
* @param array $data
* @return string
* @return string
...
...
Modules/Media/Tests/EloquentFolderRepositoryTest.php
View file @
b84f1451
...
@@ -5,7 +5,9 @@ namespace Modules\Media\Tests;
...
@@ -5,7 +5,9 @@ namespace Modules\Media\Tests;
use
Illuminate\Support\Facades\Event
;
use
Illuminate\Support\Facades\Event
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FolderIsCreating
;
use
Modules\Media\Events\FolderIsCreating
;
use
Modules\Media\Events\FolderIsUpdating
;
use
Modules\Media\Events\FolderWasCreated
;
use
Modules\Media\Events\FolderWasCreated
;
use
Modules\Media\Events\FolderWasUpdated
;
use
Modules\Media\Repositories\FolderRepository
;
use
Modules\Media\Repositories\FolderRepository
;
final
class
EloquentFolderRepositoryTest
extends
MediaTestCase
final
class
EloquentFolderRepositoryTest
extends
MediaTestCase
...
@@ -110,4 +112,56 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
...
@@ -110,4 +112,56 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this
->
assertEquals
(
'/assets/media/root-folder/child-folder'
,
$childFolder
->
path
->
getRelativeUrl
());
$this
->
assertEquals
(
'/assets/media/root-folder/child-folder'
,
$childFolder
->
path
->
getRelativeUrl
());
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/root-folder/child-folder'
)));
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/root-folder/child-folder'
)));
}
}
/** @test */
public
function
it_can_update_a_folder_in_database
()
{
$folder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
,
'parent_id'
=>
0
]);
$folder
=
$this
->
folder
->
update
(
$folder
,
[
'name'
=>
'New Name!'
]);
$this
->
assertCount
(
1
,
$this
->
folder
->
all
());
$this
->
assertEquals
(
'New Name!'
,
$folder
->
filename
);
$this
->
assertEquals
(
'/assets/media/new-name'
,
$folder
->
path
->
getRelativeUrl
());
}
/** @test */
public
function
it_triggers_event_when_folder_was_updated
()
{
Event
::
fake
();
$folder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
,
'parent_id'
=>
0
]);
$folder
=
$this
->
folder
->
update
(
$folder
,
[
'name'
=>
'New Name!'
]);
Event
::
assertDispatched
(
FolderWasUpdated
::
class
,
function
(
$e
)
use
(
$folder
)
{
return
$e
->
folder
->
id
===
$folder
->
id
;
});
}
/** @test */
public
function
it_triggers_an_event_when_folder_is_updating
()
{
Event
::
fake
();
$folder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folder
=
$this
->
folder
->
update
(
$folder
,
[
'name'
=>
'New Name!'
]);
Event
::
assertDispatched
(
FolderIsUpdating
::
class
,
function
(
$e
)
use
(
$folder
)
{
return
$e
->
getAttribute
(
'filename'
)
===
$folder
->
filename
;
});
}
/** @test */
public
function
it_can_change_data_when_folder_is_updating
()
{
Event
::
listen
(
FolderIsUpdating
::
class
,
function
(
FolderIsUpdating
$event
)
{
$filename
=
$event
->
getAttribute
(
'filename'
);
$event
->
setAttributes
([
'filename'
=>
strtoupper
(
$filename
)]);
});
$folder
=
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folder
=
$this
->
folder
->
update
(
$folder
,
[
'name'
=>
'New Name!'
]);
$this
->
assertEquals
(
'NEW NAME!'
,
$folder
->
filename
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment