Skip to content

Commit

Permalink
Merge pull request #1960 from hydephp/test-cleanup
Browse files Browse the repository at this point in the history
Internal: Test code refactors and cleanup hydephp/develop@1eb4502
  • Loading branch information
github-actions authored and caendesilva committed Dec 21, 2024
1 parent 1fabbd8 commit fc9b9cc
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 28 deletions.
35 changes: 32 additions & 3 deletions tests/Unit/Facades/AuthorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,37 @@
namespace Hyde\Framework\Testing\Unit\Facades;

use Hyde\Facades\Author;
use Hyde\Testing\UnitTestCase;
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Facades\Author
*/
class AuthorTest extends TestCase
class AuthorTest extends UnitTestCase
{
protected function setUp(): void
{
self::mockConfig(['hyde.authors' => [
Author::create('mr_hyde', 'Mr. Hyde', 'https://hydephp.com'),
]]);
}

public function testCreate()
{
$author = Author::create('john_doe', 'John Doe', 'https://johndoe.com');

$this->assertSame('john_doe', $author->username);
$this->assertSame('John Doe', $author->name);
$this->assertSame('https://johndoe.com', $author->website);
}

$this->assertEquals(Author::create('foo', null, null), Author::create('foo'));
public function testCreateWithOnlyRequiredFields()
{
$author = Author::create('john_doe');

$this->assertSame('john_doe', $author->username);
$this->assertSame('john_doe', $author->name);
$this->assertNull($author->website);
}

public function testGet()
Expand All @@ -31,7 +45,15 @@ public function testGet()
$this->assertSame('mr_hyde', $author->username);
$this->assertSame('Mr. Hyde', $author->name);
$this->assertSame('https://hydephp.com', $author->website);
}

public function testGetWithNotSetUsername()
{
$this->assertEquals(Author::create('foo'), Author::get('foo'));
}

public function testGetAliasesPostAuthor()
{
$this->assertEquals(PostAuthor::get('foo'), Author::get('foo'));
}

Expand All @@ -40,5 +62,12 @@ public function testAll()
$authors = Author::all();
$this->assertCount(1, $authors);
$this->assertContainsOnlyInstancesOf(PostAuthor::class, $authors);
$this->assertEquals(Author::get('mr_hyde'), $authors->first());
}

public function testAllWithNoAuthors()
{
self::mockConfig(['hyde.authors' => []]);
$this->assertEmpty(Author::all());
}
}
52 changes: 39 additions & 13 deletions tests/Unit/Pages/PageModelGetAllFilesHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,85 @@

namespace Hyde\Framework\Testing\Unit\Pages;

use Hyde\Facades\Filesystem;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Testing\UnitTestCase;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Testing\TestCase;
use Mockery\ExpectationInterface;

/**
* @see \Hyde\Pages\Concerns\HydePage::files()
*/
class PageModelGetAllFilesHelperTest extends TestCase
class PageModelGetAllFilesHelperTest extends UnitTestCase
{
protected static bool $needsConfig = true;

/** @var \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */
protected $filesystem;

protected function setUp(): void
{
self::setupKernel();

$this->filesystem = $this->mockFilesystemStrict()
->shouldReceive('missing')->withAnyArgs()->andReturn(false)->byDefault()
->shouldReceive('get')->withAnyArgs()->andReturn('foo')->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.html'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.blade.php'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_posts/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_docs/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault();
}

protected function tearDown(): void
{
$this->verifyMockeryExpectations();
}

public function testBladePageGetHelperReturnsBladePageArray()
{
$this->shouldReceiveGlob('_pages/{*,**/*}.blade.php')->andReturn(['_pages/test-page.blade.php']);

$array = BladePage::files();
$this->assertCount(2, $array);
$this->assertCount(1, $array);
$this->assertIsArray($array);
$this->assertEquals(['404', 'index'], $array);
$this->assertEquals(['test-page'], $array);
}

public function testMarkdownPageGetHelperReturnsMarkdownPageArray()
{
Filesystem::touch('_pages/test-page.md');
$this->shouldReceiveGlob('_pages/{*,**/*}.md')->andReturn(['_pages/test-page.md']);

$array = MarkdownPage::files();
$this->assertCount(1, $array);
$this->assertIsArray($array);
$this->assertEquals(['test-page'], $array);

Filesystem::unlink('_pages/test-page.md');
}

public function testMarkdownPostGetHelperReturnsMarkdownPostArray()
{
Filesystem::touch('_posts/test-post.md');
$this->shouldReceiveGlob('_posts/{*,**/*}.md')->andReturn(['_posts/test-post.md']);

$array = MarkdownPost::files();
$this->assertCount(1, $array);
$this->assertIsArray($array);
$this->assertEquals(['test-post'], $array);

Filesystem::unlink('_posts/test-post.md');
}

public function testDocumentationPageGetHelperReturnsDocumentationPageArray()
{
Filesystem::touch('_docs/test-page.md');
$this->shouldReceiveGlob('_docs/{*,**/*}.md')->andReturn(['_docs/test-page.md']);

$array = DocumentationPage::files();
$this->assertCount(1, $array);
$this->assertIsArray($array);
$this->assertEquals(['test-page'], $array);
}

Filesystem::unlink('_docs/test-page.md');
protected function shouldReceiveGlob(string $withPath): ExpectationInterface
{
return $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path($withPath), GLOB_BRACE);
}
}
58 changes: 46 additions & 12 deletions tests/Unit/Pages/PageModelGetHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,94 @@

namespace Hyde\Framework\Testing\Unit\Pages;

use Hyde\Facades\Filesystem;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Testing\TestCase;
use Hyde\Testing\UnitTestCase;
use Mockery\ExpectationInterface;
use Illuminate\Support\Collection;

/**
* @see \Hyde\Pages\Concerns\HydePage::all()
*/
class PageModelGetHelperTest extends TestCase
class PageModelGetHelperTest extends UnitTestCase
{
protected static bool $needsConfig = true;

/** @var \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */
protected $filesystem;

protected function setUp(): void
{
self::setupKernel();

$this->filesystem = $this->mockFilesystemStrict()
->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.html'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.blade.php'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_posts/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault()
->shouldReceive('glob')->once()->with(Hyde::path('_docs/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault();
}

protected function tearDown(): void
{
$this->verifyMockeryExpectations();
}

public function testBladePageGetHelperReturnsBladePageCollection()
{
$this->shouldReceiveGlob('_pages/{*,**/*}.blade.php')->andReturn(['_pages/test-page.blade.php']);
$this->shouldFindFile('_pages/test-page.blade.php');

$collection = BladePage::all();
$this->assertCount(2, $collection);
$this->assertCount(1, $collection);
$this->assertInstanceOf(Collection::class, $collection);
$this->assertContainsOnlyInstancesOf(BladePage::class, $collection);
}

public function testMarkdownPageGetHelperReturnsMarkdownPageCollection()
{
Filesystem::touch('_pages/test-page.md');
$this->shouldReceiveGlob('_pages/{*,**/*}.md')->andReturn(['_pages/test-page.md']);
$this->shouldFindFile('_pages/test-page.md');

$collection = MarkdownPage::all();
$this->assertCount(1, $collection);
$this->assertInstanceOf(Collection::class, $collection);
$this->assertContainsOnlyInstancesOf(MarkdownPage::class, $collection);

Filesystem::unlink('_pages/test-page.md');
}

public function testMarkdownPostGetHelperReturnsMarkdownPostCollection()
{
Filesystem::touch('_posts/test-post.md');
$this->shouldReceiveGlob('_posts/{*,**/*}.md')->andReturn(['_posts/test-post.md']);
$this->shouldFindFile('_posts/test-post.md');

$collection = MarkdownPost::all();
$this->assertCount(1, $collection);
$this->assertInstanceOf(Collection::class, $collection);
$this->assertContainsOnlyInstancesOf(MarkdownPost::class, $collection);

Filesystem::unlink('_posts/test-post.md');
}

public function testDocumentationPageGetHelperReturnsDocumentationPageCollection()
{
Filesystem::touch('_docs/test-page.md');
$this->shouldReceiveGlob('_docs/{*,**/*}.md')->andReturn(['_docs/test-page.md']);
$this->shouldFindFile('_docs/test-page.md');

$collection = DocumentationPage::all();
$this->assertCount(1, $collection);
$this->assertInstanceOf(Collection::class, $collection);
$this->assertContainsOnlyInstancesOf(DocumentationPage::class, $collection);
}

Filesystem::unlink('_docs/test-page.md');
protected function shouldReceiveGlob(string $withPath): ExpectationInterface
{
return $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path($withPath), GLOB_BRACE);
}

protected function shouldFindFile(string $file): void
{
$this->filesystem->shouldReceive('missing')->once()->with(Hyde::path($file))->andReturnFalse();
$this->filesystem->shouldReceive('get')->once()->with(Hyde::path($file))->andReturn('content');
}
}

0 comments on commit fc9b9cc

Please sign in to comment.