Add tests for the StoresMeta Trait and fix the Functional MetaSeo tests
This commit is contained in:
@@ -18,38 +18,64 @@ trait StoresMeta
|
|||||||
/**
|
/**
|
||||||
* Stores all Meta values of a model.
|
* Stores all Meta values of a model.
|
||||||
*
|
*
|
||||||
* @param $meta
|
* @param array $metaData
|
||||||
* @param $type
|
* @param string $metaType
|
||||||
* @param $id
|
* @param string|int $metaId
|
||||||
*
|
* @param string $metaModel
|
||||||
* @throws \Exception
|
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function storeMeta($meta, $type, $id)
|
public function storeMeta($metaData, $metaType, $metaId, $metaModel = Meta::class)
|
||||||
{
|
{
|
||||||
// Validation required instead of type hinting because it could be passed as false or NULL
|
// Validation required instead of type hinting because it could be passed as false or NULL
|
||||||
if (!is_array($meta)) {
|
if (!is_array($metaData)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($meta as $key => $value) {
|
foreach ($metaData as $key => $value) {
|
||||||
$meta = Meta::firstOrNew([
|
$modelInstance = call_user_func(
|
||||||
'key' => $key,
|
[$metaModel, 'firstOrNew'],
|
||||||
'meta_type' => $type,
|
[
|
||||||
'meta_id' => $id,
|
'key' => $key,
|
||||||
]);
|
'meta_type' => $metaType,
|
||||||
|
'meta_id' => $metaId,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$value = $this->removeEmptyValues($value);
|
||||||
if (!empty($value)) {
|
if (!empty($value)) {
|
||||||
$meta->value = $value;
|
$modelInstance->setAttribute('value', $value);
|
||||||
$meta->save();
|
$modelInstance->save();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The value is empty, remove the row
|
// The value is empty, remove the row
|
||||||
if ($meta->exists) {
|
if ($modelInstance->exists) {
|
||||||
$meta->delete();
|
$modelInstance->delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if a Value is empty.
|
||||||
|
*
|
||||||
|
* @param $values
|
||||||
|
*
|
||||||
|
* @return array|mixed
|
||||||
|
*/
|
||||||
|
protected function removeEmptyValues($values)
|
||||||
|
{
|
||||||
|
if (!is_array($values)) {
|
||||||
|
return empty($values) ? null : $values;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($values as $key => $value) {
|
||||||
|
if (!empty($value)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
unset($values[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $values;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,133 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Cachet.
|
||||||
|
*
|
||||||
|
* (c) Alt Three Services Limited
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace CachetHQ\Tests\Cachet\Bus\Traits;
|
||||||
|
|
||||||
|
use CachetHQ\Cachet\Bus\Handlers\Traits\StoresMeta;
|
||||||
|
use CachetHQ\Cachet\Models\Meta;
|
||||||
|
use CachetHQ\Tests\Cachet\AbstractTestCase;
|
||||||
|
use Mockery;
|
||||||
|
use Mockery\MockInterface;
|
||||||
|
|
||||||
|
class StoresMetaTest extends AbstractTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Meta|MockInterface
|
||||||
|
*/
|
||||||
|
protected $metaModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up the testcase by mocking the Meta::class.
|
||||||
|
*/
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
if (!class_exists('Mockery')) {
|
||||||
|
$this->markTestSkipped('This test requires Mockery');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->metaModel = Mockery::mock(Meta::class)->makePartial();
|
||||||
|
$this->app->instance(Meta::class, $this->metaModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Our Mockery expectations should count as assertions to prevent warnings from PHPUnit.
|
||||||
|
*/
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
$this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount());
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Each array value passed to the MetaValues should result in a new model instance.
|
||||||
|
*/
|
||||||
|
public function testStoresMetaWithSimpleMultipleArrays()
|
||||||
|
{
|
||||||
|
$mock = $this->getMockForTrait(StoresMeta::class);
|
||||||
|
$metaData = [
|
||||||
|
'somekey1' => 'somevalue',
|
||||||
|
'somekey2' => 'somevalue',
|
||||||
|
'somekey3' => 'somevalue',
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->metaModel->shouldReceive('firstOrNew')->times(3)->andReturn($this->metaModel);
|
||||||
|
$this->metaModel->shouldReceive('save')->times(3);
|
||||||
|
$this->metaModel->shouldReceive('setAttribute')->times(3)->andReturnUsing(function ($key, $value) {
|
||||||
|
$this->assertEquals('value', $key);
|
||||||
|
$this->assertEquals('somevalue', $value);
|
||||||
|
|
||||||
|
return $this->metaModel;
|
||||||
|
});
|
||||||
|
|
||||||
|
$mock->storeMeta($metaData, 'some_class', 1, $this->metaModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* It will pass nested arrays to the value property of the Meta model.
|
||||||
|
*/
|
||||||
|
public function testStoresNestedArraysAsSingleValue()
|
||||||
|
{
|
||||||
|
$mock = $this->getMockForTrait(StoresMeta::class);
|
||||||
|
$metaData = ['somekey1' => ['subkey' => ['second' => 'key']]];
|
||||||
|
|
||||||
|
$this->metaModel->shouldReceive('firstOrNew')->once()->andReturn($this->metaModel);
|
||||||
|
$this->metaModel->shouldReceive('save')->once();
|
||||||
|
$this->metaModel->shouldReceive('setAttribute')->once()->andReturnUsing(function ($key, $value) {
|
||||||
|
$this->assertEquals('value', $key);
|
||||||
|
$this->assertEquals(['subkey' => ['second' => 'key']], $value);
|
||||||
|
|
||||||
|
return $this->metaModel;
|
||||||
|
});
|
||||||
|
|
||||||
|
$mock->storeMeta($metaData, 'some_class', 1, $this->metaModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a value is empty or null it will be removed.
|
||||||
|
*/
|
||||||
|
public function testEmptyValuesWillBeDeleted()
|
||||||
|
{
|
||||||
|
$mock = $this->getMockForTrait(StoresMeta::class);
|
||||||
|
$metaData = ['somekey1' => ''];
|
||||||
|
|
||||||
|
$this->metaModel->exists = true;
|
||||||
|
$this->metaModel->shouldReceive('firstOrNew')->once()->andReturn($this->metaModel);
|
||||||
|
$this->metaModel->shouldReceive('delete')->once();
|
||||||
|
$this->metaModel->shouldReceive('setAttribute')->never();
|
||||||
|
$this->metaModel->shouldReceive('save')->never();
|
||||||
|
|
||||||
|
$mock->storeMeta($metaData, 'some_class', 1, $this->metaModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a value is empty or null in a nested array it will be removed.
|
||||||
|
*/
|
||||||
|
public function testEmptyNestedArrayKeysAreRemoved()
|
||||||
|
{
|
||||||
|
$mock = $this->getMockForTrait(StoresMeta::class);
|
||||||
|
$metaData = ['somekey1' => ['keyWithValue' => 'value123', 'keyWithoutValue' => null]];
|
||||||
|
|
||||||
|
$this->metaModel->exists = true;
|
||||||
|
$this->metaModel->shouldReceive('firstOrNew')->once()->andReturn($this->metaModel);
|
||||||
|
$this->metaModel->shouldReceive('setAttribute')->once()->andReturnUsing(function ($key, $value) {
|
||||||
|
$this->assertEquals('value', $key);
|
||||||
|
$this->assertEquals(['keyWithValue' => 'value123'], $value);
|
||||||
|
|
||||||
|
return $this->metaModel;
|
||||||
|
});
|
||||||
|
$this->metaModel->shouldReceive('save')->once();
|
||||||
|
$this->metaModel->shouldReceive('delete')->never();
|
||||||
|
|
||||||
|
$mock->storeMeta($metaData, 'some_class', 1, $this->metaModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,12 +13,10 @@ namespace CachetHQ\Tests\Cachet\Functional\Bus\Commands\Incident;
|
|||||||
|
|
||||||
use CachetHQ\Cachet\Bus\Commands\Incident\CreateIncidentCommand;
|
use CachetHQ\Cachet\Bus\Commands\Incident\CreateIncidentCommand;
|
||||||
use CachetHQ\Cachet\Models\Incident;
|
use CachetHQ\Cachet\Models\Incident;
|
||||||
use CachetHQ\Cachet\Models\User;
|
|
||||||
use CachetHQ\Cachet\Presenters\IncidentPresenter;
|
use CachetHQ\Cachet\Presenters\IncidentPresenter;
|
||||||
use CachetHQ\Cachet\Settings\Repository as SettingsRepository;
|
use CachetHQ\Cachet\Settings\Repository as SettingsRepository;
|
||||||
use CachetHQ\Tests\Cachet\AbstractTestCase;
|
use CachetHQ\Tests\Cachet\AbstractTestCase;
|
||||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
use Illuminate\Support\Facades\Auth;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the create incident command test class.
|
* This is the create incident command test class.
|
||||||
@@ -43,8 +41,9 @@ class MetaSeoTest extends AbstractTestCase
|
|||||||
/**
|
/**
|
||||||
* CreateIncidentCommandTest constructor.
|
* CreateIncidentCommandTest constructor.
|
||||||
*
|
*
|
||||||
* @param null $name
|
* @param null $name
|
||||||
* @param array $data
|
* @param array $data
|
||||||
|
* @param string $dataName
|
||||||
*/
|
*/
|
||||||
public function __construct($name = null, array $data = [], $dataName = '')
|
public function __construct($name = null, array $data = [], $dataName = '')
|
||||||
{
|
{
|
||||||
@@ -60,6 +59,7 @@ class MetaSeoTest extends AbstractTestCase
|
|||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->app->make(SettingsRepository::class)->set('app_name', $this->appName);
|
$this->app->make(SettingsRepository::class)->set('app_name', $this->appName);
|
||||||
|
$this->app->config->set('setting.app_name', $this->appName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -153,14 +153,11 @@ class MetaSeoTest extends AbstractTestCase
|
|||||||
/**
|
/**
|
||||||
* @param array $meta
|
* @param array $meta
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Database\Eloquent\Model|static
|
* @return Incident
|
||||||
*/
|
*/
|
||||||
protected function createIncidentWithMeta(array $meta)
|
protected function createIncidentWithMeta(array $meta)
|
||||||
{
|
{
|
||||||
$user = factory(User::class)->create();
|
$this->signIn();
|
||||||
$user->save();
|
|
||||||
|
|
||||||
Auth::login($user);
|
|
||||||
$name = $this->fakerFactory->name;
|
$name = $this->fakerFactory->name;
|
||||||
$message = $this->fakerFactory->sentence;
|
$message = $this->fakerFactory->sentence;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user