Skip to content

Commit

Permalink
Merge pull request #352 from hydephp/merge-author-models
Browse files Browse the repository at this point in the history
Merge deprecated Author helper into Author model hydephp/develop@9b4e676
  • Loading branch information
github-actions committed Aug 6, 2022
1 parent c6af5db commit 2157377
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 56 deletions.
2 changes: 1 addition & 1 deletion config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
|
*/

use Hyde\Framework\Helpers\Author;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Helpers\Meta;
use Hyde\Framework\Models\Author;
use Hyde\Framework\Models\NavItem;

return [
Expand Down
3 changes: 1 addition & 2 deletions src/Concerns/HasAuthor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Hyde\Framework\Concerns;

use Hyde\Framework\Helpers\Author as AuthorHelper;
use Hyde\Framework\Models\Author;

/**
Expand Down Expand Up @@ -34,7 +33,7 @@ public function constructAuthor(): void

protected function findAuthor(string $author): Author
{
return AuthorHelper::get($author);
return Author::get($author);
}

protected function createAuthor(array $data): Author
Expand Down
32 changes: 0 additions & 32 deletions src/Helpers/Author.php

This file was deleted.

21 changes: 21 additions & 0 deletions src/Models/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Hyde\Framework\Models;

use Illuminate\Support\Collection;

/**
* The Post Author Object Model.
*/
Expand Down Expand Up @@ -63,4 +65,23 @@ public function getName(): string
{
return $this->name ?? $this->username;
}

public static function create(string $username, ?string $name = null, ?string $website = null): static
{
return new static($username, [
'name' => $name,
'website'=> $website,
]);
}

public static function all(): Collection
{
return new Collection(config('authors', []));
}

public static function get(string $username): static
{
return static::all()->firstWhere('username', $username)
?? static::create($username);
}
}
39 changes: 19 additions & 20 deletions tests/Feature/AuthorHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@

namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Helpers\Author as AuthorHelper;
use Hyde\Framework\Models\Author as AuthorModel;
use Hyde\Framework\Models\Author;
use Hyde\Testing\TestCase;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;

/**
* Class AuthorHelperTest.
*
* @covers \Hyde\Framework\Helpers\Author
* @covers \Hyde\Framework\Models\Author
*/
class AuthorHelperTest extends TestCase
{
public function test_create_method_creates_new_author_model()
{
$author = AuthorHelper::create('foo');
$author = Author::create('foo');

$this->assertInstanceOf(AuthorModel::class, $author);
$this->assertInstanceOf(Author::class, $author);
}

public function test_create_method_accepts_all_parameters()
{
$author = AuthorHelper::create('foo', 'bar', 'https://example.com');
$author = Author::create('foo', 'bar', 'https://example.com');

$this->assertEquals('foo', $author->username);
$this->assertEquals('bar', $author->name);
Expand All @@ -34,7 +33,7 @@ public function test_create_method_accepts_all_parameters()
public function test_all_method_returns_empty_collection_if_no_authors_are_set_in_config()
{
Config::set('authors', []);
$authors = AuthorHelper::all();
$authors = Author::all();

$this->assertInstanceOf(Collection::class, $authors);
$this->assertCount(0, $authors);
Expand All @@ -43,47 +42,47 @@ public function test_all_method_returns_empty_collection_if_no_authors_are_set_i
public function test_all_method_returns_collection_with_all_authors_defined_in_config()
{
Config::set('authors', [
AuthorHelper::create('foo'),
Author::create('foo'),
]);
$authors = AuthorHelper::all();
$authors = Author::all();

$this->assertInstanceOf(Collection::class, $authors);
$this->assertCount(1, $authors);
$this->assertEquals(AuthorHelper::create('foo'), $authors->first());
$this->assertEquals(Author::create('foo'), $authors->first());
}

public function test_multiple_authors_can_be_defined_in_config()
{
Config::set('authors', [
AuthorHelper::create('foo'),
AuthorHelper::create('bar'),
Author::create('foo'),
Author::create('bar'),
]);
$authors = AuthorHelper::all();
$authors = Author::all();

$this->assertInstanceOf(Collection::class, $authors);
$this->assertCount(2, $authors);
$this->assertEquals(AuthorHelper::create('foo'), $authors->first());
$this->assertEquals(AuthorHelper::create('bar'), $authors->last());
$this->assertEquals(Author::create('foo'), $authors->first());
$this->assertEquals(Author::create('bar'), $authors->last());
}

public function test_get_method_returns_config_defined_author_by_username()
{
Config::set('authors', [
AuthorHelper::create('foo', 'bar'),
Author::create('foo', 'bar'),
]);
$author = AuthorHelper::get('foo');
$author = Author::get('foo');

$this->assertInstanceOf(AuthorModel::class, $author);
$this->assertInstanceOf(Author::class, $author);
$this->assertEquals('foo', $author->username);
$this->assertEquals('bar', $author->name);
}

public function test_get_method_returns_new_author_if_username_not_found_in_config()
{
Config::set('authors', []);
$author = AuthorHelper::get('foo');
$author = Author::get('foo');

$this->assertInstanceOf(AuthorModel::class, $author);
$this->assertInstanceOf(Author::class, $author);
$this->assertEquals('foo', $author->username);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/AuthorPostsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
use Hyde\Framework\Helpers\Author;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Author;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\Config;

Expand Down

0 comments on commit 2157377

Please sign in to comment.