Skip to content

Commit

Permalink
Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanThompson committed Jan 20, 2021
1 parent e563b5a commit 7481b92
Show file tree
Hide file tree
Showing 16 changed files with 810 additions and 271 deletions.
36 changes: 19 additions & 17 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="../../../vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="../../../vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">

<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>

<testsuites>
<testsuite name="Streams Core">
<directory suffix="Test.php">tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

<php>
<ini name="display_errors" value="true"/>
<env name="APP_ENV" value="testing"/>
<env name="APP_REFERENCE" value="testing"/>
<env name="VERSION_IMAGES" value="false"/>
<env name="VERSION_ASSETS" value="false"/>
</php>

</phpunit>
35 changes: 29 additions & 6 deletions src/Criteria/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
namespace Streams\Core\Criteria\Adapter;

use Filebase\Database;
use Illuminate\Support\Arr;
use Streams\Core\Entry\Entry;
use Streams\Core\Stream\Stream;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Traits\Macroable;
use Streams\Core\Support\Traits\HasMemory;
use Illuminate\Pagination\LengthAwarePaginator;
use Streams\Core\Entry\Contract\EntryInterface;
use Streams\Core\Criteria\Contract\AdapterInterface;

Expand Down Expand Up @@ -96,10 +93,10 @@ abstract public function save(EntryInterface $entry);
/**
* Delete an entry.
*
* @param EntryInterface $entry
* @param array $parameters
* @return bool
*/
abstract public function delete(EntryInterface $entry);
abstract public function delete(array $parameters = []);

/**
* Truncate all entries.
Expand All @@ -120,14 +117,40 @@ protected function collect($entries)

$collection = new $collection;

if ($entries instanceof Collection) {
$entries = $entries->all();
}

array_map(function ($entry) use ($collection) {
$entry = $this->make($entry);
$collection->put($entry->id, $entry);
}, (array) $entries);
}, $entries);

return $collection;
}

/**
* Return an entry interface from a file.
*
* @param $entry
* @return EntryInterface
*/
protected function make($entry)
{
if ($entry instanceof EntryInterface) {
return $entry;
}

return $this->newInstance(array_merge(
[
'id' => $entry->getId(),
'created_at' => $entry->createdAt(),
'updated_at' => $entry->updatedAt(),
],
$entry->toArray()
));
}

/**
* Return an entry instance.
*
Expand Down
37 changes: 28 additions & 9 deletions src/Criteria/Adapter/DatabaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function get(array $parameters = [])
$method = Str::camel($key);

foreach ($call as $parameters) {
call_user_func_array([$this, $method], array_filter($parameters));
call_user_func_array([$this, $method], $parameters);
}
}

Expand Down Expand Up @@ -143,7 +143,9 @@ public function count(array $parameters = [])
*/
public function create(array $attributes = [])
{
return $this->make($this->query->insert($attributes));
$id = $this->query->insertGetId($attributes);

return $this->make(['id' => $id] + $attributes);
}

/**
Expand All @@ -154,20 +156,37 @@ public function create(array $attributes = [])
*/
public function save($entry)
{
return $this->query->update($entry->getAttributes());
$attributes = $entry->getAttributes();

if (isset($attributes['id'])) {
return $this->query->update($entry->getAttributes());
}

$id = $this->query->insertGetId($entry->getAttributes());

$entry->id = $id;

return true;
}

/**
* Delete an entry.
* Delete results.
*
* @param EntryInterface $entry
* @param array $parameters
* @return bool
*/
public function delete(EntryInterface $entry)
public function delete(array $parameters = [])
{
return $this->query
->get($entry->id)
->delete();
foreach ($parameters as $key => $call) {

$method = Str::camel($key);

foreach ($call as $parameters) {
call_user_func_array([$this, $method], $parameters);
}
}

return $this->query->delete();
}

/**
Expand Down
34 changes: 28 additions & 6 deletions src/Criteria/Adapter/EloquentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,23 @@ public function save($entry)
}

/**
* Delete an entry.
* Delete results.
*
* @param EntryInterface $entry
* @param array $parameters
* @return bool
*/
public function delete(EntryInterface $entry)
public function delete(array $parameters = [])
{
return $this->query
->get($entry->id)
->delete();
foreach ($parameters as $key => $call) {

$method = Str::camel($key);

foreach ($call as $parameters) {
call_user_func_array([$this, $method], $parameters);
}
}

return $this->query->delete();
}

/**
Expand All @@ -196,4 +203,19 @@ protected function make($entry)

return $this->newInstance($entry);
}

/**
* Return an entry instance.
*
* @param array $attributes
* @return EntryInterface
*/
public function newInstance(array $attributes = [])
{
$model = $this->stream->getPrototypeAttribute('source.model');

$model = new $model($attributes);

return $model;
}
}
28 changes: 14 additions & 14 deletions src/Criteria/Adapter/FileAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ public function __construct(Stream $stream)
$format = $source->get('format', 'json');
$path = trim($source->get('path', 'streams/data'), '/\\');

if ($stream->translatable && App::getLocale() != App::getFallbackLocale()) {

$localization = $stream->translatable[App::getLocale()];

$path = trim(Arr::get($localization, 'source.path', $path), '/\\');
}

if ($format == 'json') {

$this->data = json_decode(file_get_contents(base_path($path . '/' . $stream->handle . '.' . $format)), true);
Expand Down Expand Up @@ -145,7 +138,7 @@ public function get(array $parameters = [])
$method = Str::camel($key);

foreach ($call as $parameters) {
call_user_func_array([$this, $method], array_filter($parameters));
call_user_func_array([$this, $method], $parameters);
}
}

Expand Down Expand Up @@ -210,16 +203,23 @@ public function save($entry)
}

/**
* Delete an entry.
* Delete results.
*
* @param $id
* @param array $parameters
* @return bool
*/
public function delete($id)
public function delete(array $parameters = [])
{
return $this->query
->get($id)
->delete();
foreach ($parameters as $key => $call) {

$method = Str::camel($key);

foreach ($call as $parameters) {
call_user_func_array([$this, $method], $parameters);
}
}

return $this->query->delete();
}

/**
Expand Down
53 changes: 16 additions & 37 deletions src/Criteria/Adapter/FilebaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Streams\Core\Criteria\Adapter;

use Filebase\Query;
use Filebase\Database;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand All @@ -17,7 +18,7 @@ class FilebaseAdapter extends AbstractAdapter
/**
* The database query.
*
* @var Database
* @var Database|Query
*/
protected $query;

Expand All @@ -37,13 +38,6 @@ public function __construct(Stream $stream)

$path = $source->get('path', 'streams/data/' . $stream->handle);

if ($stream->translatable && App::getLocale() != App::getFallbackLocale()) {

$localization = $stream->translatable[App::getLocale()];

$path = Arr::get($localization, 'source.path', $path);
}

$this->query = new Database([
'dir' => base_path($path),

Expand Down Expand Up @@ -176,11 +170,7 @@ public function create(array $attributes = [])

$document = $this->query->get($id);

if (!$document->save($attributes)) {
return false;
}

return $this->make($document);
return $this->make($document->save($attributes));
}

/**
Expand All @@ -207,16 +197,23 @@ public function save($entry)
}

/**
* Delete an entry.
* Delete results.
*
* @param $id
* @param array $parameters
* @return bool
*/
public function delete($id)
public function delete(array $parameters = [])
{
return $this->query
->get($id)
->delete();
foreach ($parameters as $key => $call) {

$method = Str::camel($key);

foreach ($call as $parameters) {
call_user_func_array([$this, $method], $parameters);
}
}

return $this->query->delete();
}

/**
Expand All @@ -228,22 +225,4 @@ public function truncate()
{
$this->query->truncate();
}

/**
* Return an entry interface from a file.
*
* @param $entry
* @return EntryInterface
*/
protected function make($entry)
{
return $this->newInstance(array_merge(
[
'id' => $entry->getId(),
'created_at' => $entry->createdAt(),
'updated_at' => $entry->updatedAt(),
],
$entry->toArray()
));
}
}
4 changes: 2 additions & 2 deletions src/Criteria/Contract/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public function save(EntryInterface $entry);
/**
* Delete an entry.
*
* @param EntryInterface $entry
* @param array $parameters
* @return bool
*/
public function delete(EntryInterface $entry);
public function delete(array $parameters = []);

/**
* Delete all entries.
Expand Down
Loading

0 comments on commit 7481b92

Please sign in to comment.