Skip to content

Commit

Permalink
Merge pull request #1609 from hydephp/refactor-and-improve-testing-in…
Browse files Browse the repository at this point in the history
…ternals

Internal: Improve and refactor testing helpers
  • Loading branch information
caendesilva authored Mar 14, 2024
2 parents 0d5fd3b + 91bae19 commit 343c6b7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
9 changes: 5 additions & 4 deletions packages/testing/src/InteractsWithPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@

namespace Hyde\Testing;

use Hyde\Pages\InMemoryPage;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Pages\MarkdownPage;
use Hyde\Support\Facades\Render;
use Hyde\Support\Models\Route;

trait InteractsWithPages
{
protected function mockRoute(?Route $route = null): void
{
Render::share('route', $route ?? (new Route(new MarkdownPage())));
Render::share('route', $route ?? (new Route(new InMemoryPage())));
}

protected function mockPage(?HydePage $page = null, ?string $currentPage = null): void
{
Render::share('page', $page ?? new MarkdownPage());
Render::share('routeKey', $currentPage ?? 'PHPUnit');
Render::share('page', $page ?? new InMemoryPage());
Render::share('routeKey', $currentPage ?? 'foo');
}

protected function mockCurrentPage(string $currentPage): void
{
Render::share('routeKey', $currentPage);
Render::share('route', new Route(new InMemoryPage($currentPage)));
}
}
42 changes: 41 additions & 1 deletion packages/testing/src/Support/TestView.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Hyde\Testing\Support;

use Hyde\Hyde;
use Illuminate\Support\Str;
use JetBrains\PhpStorm\NoReturn;
use Illuminate\Testing\Assert as PHPUnit;

class TestView extends \Illuminate\Testing\TestView
Expand Down Expand Up @@ -41,7 +44,33 @@ public function assertSeeHtmlIgnoringFormatting(string $value): static
*/
public function assertAttributeIs(string $attributeName, string $expectedValue): static
{
PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered);
static::assertHasAttribute($attributeName);

PHPUnit::assertStringContainsString($attributeName.'="'.$expectedValue.'"', $this->rendered, "The attribute '$attributeName' with value '$expectedValue' was not found.");

return $this;
}

/**
* Assert that the HTML attribute is present within the view.
*
* @return $this
*/
public function assertHasAttribute(string $attributeName): static
{
PHPUnit::assertStringContainsString($attributeName.'="', $this->rendered, "The attribute '$attributeName' was not found.");

return $this;
}

/**
* Assert that the HTML attribute is not present within the view.
*
* @return $this
*/
public function assertDoesNotHaveAttribute(string $attributeName): static
{
PHPUnit::assertStringNotContainsString($attributeName.'="', $this->rendered, "The attribute '$attributeName' was found.");

return $this;
}
Expand All @@ -58,6 +87,17 @@ public function assertTextIs(string $value): static
return $this;
}

#[NoReturn]
public function dd(bool $writeHtml = true): void
{
if ($writeHtml) {
$viewName = Str::after(Str::after(basename(class_basename($this->view->getName())), '.'), '.');
file_put_contents(Hyde::path(Str::kebab($viewName.'.html')), $this->rendered);
}

exit(trim($this->rendered)."\n\n");
}

protected function trimNewlinesAndIndentation(string $value): string
{
return str_replace([' ', "\t", "\n", "\r"], '', $value);
Expand Down

0 comments on commit 343c6b7

Please sign in to comment.