Skip to content

Commit

Permalink
Merge pull request #44 from ryangjchandler/fix/hidden-props-not-storing
Browse files Browse the repository at this point in the history
fix: `$hidden` properties not being stored on disk
  • Loading branch information
ryangjchandler authored Mar 26, 2021
2 parents 048fc17 + 282c2de commit bc3346c
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 41 deletions.
33 changes: 0 additions & 33 deletions .github/workflows/psalm.yml

This file was deleted.

1 change: 1 addition & 0 deletions src/Concerns/Orbital.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function migrate()
$columns = $schema->getColumnListing($table);

$driver->all(static::getOrbitalPath())
->filter()
->map(function ($row) use ($columns) {
return collect($row)
->filter(fn ($_, $key) => in_array($key, $columns))
Expand Down
7 changes: 7 additions & 0 deletions src/Drivers/FileDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ protected function filepath(string $directory, string $key): string
return $directory . DIRECTORY_SEPARATOR . $key . '.' . $this->extension();
}

protected function getModelAttributes(Model $model)
{
return collect($model->getAttributes())
->map(fn ($_, $key) => $model->{$key})
->toArray();
}

abstract protected function extension(): string;

abstract protected function dumpContent(Model $model): string;
Expand Down
10 changes: 8 additions & 2 deletions src/Drivers/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ class Json extends FileDriver
{
protected function dumpContent(Model $model): string
{
$data = array_filter($model->attributesToArray());
$data = array_filter($this->getModelAttributes($model));

return json_encode($data, JSON_PRETTY_PRINT);
}

protected function parseContent(SplFileInfo $file): array
{
return json_decode(file_get_contents($file->getPathname()), true);
$contents = file_get_contents($file->getPathname());

if (! $contents) {
return [];
}

return json_decode($contents, true);
}

protected function extension(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Markdown extends FileDriver

protected function dumpContent(Model $model): string
{
$matter = array_filter($model->attributesToArray(), function ($value, $key) {
$matter = array_filter($this->getModelAttributes($model), function ($value, $key) {
return $key !== 'content' && $value !== null;
}, ARRAY_FILTER_USE_BOTH);

Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/MarkdownJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MarkdownJson extends Markdown
{
protected function dumpContent(Model $model): string
{
$matter = array_filter($model->attributesToArray(), function ($value, $key) {
$matter = array_filter($this->getModelAttributes($model), function ($value, $key) {
return $key !== 'content' && $value !== null;
}, ARRAY_FILTER_USE_BOTH);

Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Yaml extends FileDriver
{
protected function dumpContent(Model $model): string
{
$data = array_filter($model->getAttributes());
$data = array_filter($this->getModelAttributes($model));

return SymfonyYaml::dump($data);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/AdvancedOrbitalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Orbit\Tests;

use Orbit\Tests\Fixtures\Post;
use Orbit\Tests\Fixtures\CustomKey;
use Orbit\Tests\Fixtures\JsonModel;
use Orbit\Tests\Fixtures\MarkdownJsonModel;
Expand All @@ -14,6 +15,7 @@ public function tearDown(): void
CustomKey::all()->each->delete();
JsonModel::all()->each->delete();
YamlModel::all()->each->delete();
Post::all()->each->delete();
}

public function test_it_can_create_files_using_custom_primary_key()
Expand Down Expand Up @@ -67,4 +69,18 @@ public function test_it_can_use_markdown_json_driver()

$this->assertFileExists(__DIR__.'/content/markdown_json_models/'.$md->getKey().'.md');
}

public function test_it_writes_hidden_columns()
{
$post = Post::create([
'title' => 'Ryan',
'slug' => 'ryan',
]);

$this->assertFileExists($path = __DIR__.'/content/posts/'.$post->getKey().'.md');

$contents = file_get_contents($path);

$this->assertStringContainsString('slug', $contents);
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ class Post extends Model

protected $guarded = [];

protected $hidden = [
'slug',
];

public static function schema(Blueprint $table)
{
$table->id();
$table->string('title');
$table->string('slug')->nullable();
$table->text('content')->nullable();
}
}
6 changes: 3 additions & 3 deletions tests/SoftDeletesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function test_it_will_update_deleted_at_when_deleting()

$file = file_get_contents(__DIR__.'/content/soft_deleted_posts/'.$post->id.'.md');

$this->assertStringContainsString(sprintf('deleted_at: \'%s\'', $post->deleted_at->toIsoString()), $file);
$this->assertStringContainsString(sprintf('deleted_at: %s', $post->deleted_at->toIso8601String()), $file);
}

public function test_it_will_delete_file_when_force_deleting()
Expand All @@ -38,7 +38,7 @@ public function test_it_will_delete_file_when_force_deleting()

$file = file_get_contents(__DIR__.'/content/soft_deleted_posts/'.$post->id.'.md');

$this->assertStringContainsString(sprintf('deleted_at: \'%s\'', $post->deleted_at->toIsoString()), $file);
$this->assertStringContainsString(sprintf('deleted_at: %s', $post->deleted_at->toIso8601String()), $file);

$post->forceDelete();

Expand All @@ -57,7 +57,7 @@ public function test_it_will_remove_deleted_at_when_restoring_file()

$file = file_get_contents(__DIR__.'/content/soft_deleted_posts/'.$post->id.'.md');

$this->assertStringContainsString(sprintf('deleted_at: \'%s\'', $post->deleted_at->toIsoString()), $file);
$this->assertStringContainsString(sprintf('deleted_at: %s', $post->deleted_at->toIso8601String()), $file);

$post->restore();

Expand Down

0 comments on commit bc3346c

Please sign in to comment.