Using the abstract entity class and using its helper methods in tests

parent 37a00fef
...@@ -2,48 +2,9 @@ ...@@ -2,48 +2,9 @@
namespace Modules\Media\Events; namespace Modules\Media\Events;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging; use Modules\Core\Contracts\EntityIsChanging;
final class FileIsCreating implements EntityIsChanging final class FileIsCreating extends AbstractEntityHook implements EntityIsChanging
{ {
/**
* Contains the attributes which can be changed by other listeners
* @var array
*/
private $attributes;
/**
* Contains the original attributes which cannot be changed
* @var array
*/
private $original;
public function __construct(array $attributes)
{
$this->attributes = $attributes;
$this->original = $attributes;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = array_replace_recursive($this->attributes, $attributes);
}
/**
* @return array
*/
public function getOriginal()
{
return $this->original;
}
} }
...@@ -2,53 +2,21 @@ ...@@ -2,53 +2,21 @@
namespace Modules\Media\Events; namespace Modules\Media\Events;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging; use Modules\Core\Contracts\EntityIsChanging;
use Modules\Media\Entities\File; use Modules\Media\Entities\File;
final class FileIsUpdating implements EntityIsChanging final class FileIsUpdating extends AbstractEntityHook implements EntityIsChanging
{ {
/** /**
* @var File * @var File
*/ */
private $file; private $file;
/**
* @var array
*/
private $attributes;
/**
* @var array
*/
private $original;
public function __construct(File $file, array $data) public function __construct(File $file, array $attributes)
{ {
$this->file = $file; $this->file = $file;
$this->attributes = $data; parent::__construct($attributes);
$this->original = $data;
}
/**
* @return array
*/
public function getOriginal()
{
return $this->original;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = array_replace_recursive($this->attributes, $attributes);
} }
/** /**
......
...@@ -138,7 +138,7 @@ class EloquentFileRepositoryTest extends MediaTestCase ...@@ -138,7 +138,7 @@ class EloquentFileRepositoryTest extends MediaTestCase
$file = $this->file->createFromFile(\Illuminate\Http\UploadedFile::fake()->image('myfile.jpg')); $file = $this->file->createFromFile(\Illuminate\Http\UploadedFile::fake()->image('myfile.jpg'));
Event::assertDispatched(FileIsCreating::class, function ($e) use ($file) { Event::assertDispatched(FileIsCreating::class, function ($e) use ($file) {
return $e->getAttributes()['filename'] === $file->filename; return $e->getAttribute('filename') === $file->filename;
}); });
} }
...@@ -183,7 +183,7 @@ class EloquentFileRepositoryTest extends MediaTestCase ...@@ -183,7 +183,7 @@ class EloquentFileRepositoryTest extends MediaTestCase
Event::assertDispatched(FileIsUpdating::class, function ($e) use ($file) { Event::assertDispatched(FileIsUpdating::class, function ($e) use ($file) {
return $e->getFile()->id === $file->id && return $e->getFile()->id === $file->id &&
$e->getAttributes()['en']['description'] === 'My cool file!'; $e->getAttribute('en.description') === 'My cool file!';
}); });
} }
......
...@@ -2,48 +2,9 @@ ...@@ -2,48 +2,9 @@
namespace Modules\Page\Events; namespace Modules\Page\Events;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging; use Modules\Core\Contracts\EntityIsChanging;
class PageIsCreating implements EntityIsChanging class PageIsCreating extends AbstractEntityHook implements EntityIsChanging
{ {
/**
* Contains the attributes which can be changed by other listeners
* @var array
*/
private $attributes;
/**
* Contains the original attributes which cannot be changed
* @var array
*/
private $original;
public function __construct(array $attributes)
{
$this->attributes = $attributes;
$this->original = $attributes;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = array_replace_recursive($this->attributes, $attributes);
}
/**
* @return array
*/
public function getOriginal()
{
return $this->original;
}
} }
...@@ -2,21 +2,12 @@ ...@@ -2,21 +2,12 @@
namespace Modules\Page\Events; namespace Modules\Page\Events;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging; use Modules\Core\Contracts\EntityIsChanging;
use Modules\Page\Entities\Page; use Modules\Page\Entities\Page;
class PageIsUpdating implements EntityIsChanging class PageIsUpdating extends AbstractEntityHook implements EntityIsChanging
{ {
/**
* Contains the attributes which can be changed by other listeners
* @var array
*/
private $attributes;
/**
* Contains the original attributes which cannot be changed
* @var array
*/
private $original;
/** /**
* @var Page * @var Page
*/ */
...@@ -24,33 +15,8 @@ class PageIsUpdating implements EntityIsChanging ...@@ -24,33 +15,8 @@ class PageIsUpdating implements EntityIsChanging
public function __construct(Page $page, array $attributes) public function __construct(Page $page, array $attributes)
{ {
$this->attributes = $attributes;
$this->original = $attributes;
$this->page = $page; $this->page = $page;
} parent::__construct($attributes);
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param array $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = array_replace_recursive($this->attributes, $attributes);
}
/**
* @return array
*/
public function getOriginal()
{
return $this->original;
} }
/** /**
......
...@@ -103,7 +103,7 @@ class EloquentPageRepositoryTest extends BasePageTest ...@@ -103,7 +103,7 @@ class EloquentPageRepositoryTest extends BasePageTest
$page = $this->createPage(); $page = $this->createPage();
Event::assertDispatched(PageIsCreating::class, function ($e) use ($page) { Event::assertDispatched(PageIsCreating::class, function ($e) use ($page) {
return $e->getAttributes()['template'] === $page->template; return $e->getAttribute('template') === $page->template;
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment