Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Sep 26, 2024
1 parent adcd91e commit 6e7787c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 61 deletions.
2 changes: 1 addition & 1 deletion testbench.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
providers:
- Leuverink\AssetInjector\ServiceProvider
# - Leuverink\AssetInjector\ServiceProvider
41 changes: 36 additions & 5 deletions tests/Feature/InjectsAssetsTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

use Leuverink\AssetInjector\Contracts\AssetInjector;
use Tests\Stubs\Implement;
use Tests\Stubs\DisabledImplement;
use Leuverink\AssetInjector\AssetManager;

it('injects assets into response', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html><head></head></html>');

$this->get('test-inject-in-response')
Expand All @@ -11,6 +15,8 @@
});

it('injects assets into head tag', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html><head></head></html>');

$expected = <<< 'HTML'
Expand All @@ -27,6 +33,8 @@
});

it('injects assets into html body when no head tag is present', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html></html>');

$expected = <<< 'HTML'
Expand All @@ -43,6 +51,8 @@
});

it('injects assets into the end of the html body when no head tag is present', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html><p>Hello World</p></html>');

$expected = <<< 'HTML'
Expand All @@ -59,6 +69,8 @@
});

it('doesnt inject assets into responses without a closing html tag', function () {
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => 'OK');

$this->get('test-inject-in-response')
Expand All @@ -67,13 +79,32 @@
});

it('doesnt inject assets when implementation returns false from enabled method', function () {
$this->partialMock(AssetInjector::class)
->shouldReceive('enabled')->once()
->andReturn(false);
AssetManager::register(new Implement);
AssetManager::register(new DisabledImplement);

Route::get('test-inject-in-response', fn () => '<html><head></head></html>');

$this->get('test-inject-in-response')
->assertOk()
->assertDontSee('<!--[TEST_PACKAGE]-->', false);
->assertSee('<!--[TEST_PACKAGE]-->', false)
->assertDontSee('<!--[DISABLED_TEST_PACKAGE]-->', false);
});

it('can inject more than one implement', function () {
AssetManager::register(new Implement);
AssetManager::register(new Implement);

Route::get('test-inject-in-response', fn () => '<html><head></head></html>');

$this->get('test-inject-in-response')
->assertOk()
->assertSee(<<<'HTML'
<!--[TEST_PACKAGE]-->
TEST_PACKAGE_ASSETS_INJECTED
<!--[ENDTEST_PACKAGE]-->
<!--[TEST_PACKAGE]-->
TEST_PACKAGE_ASSETS_INJECTED
<!--[ENDTEST_PACKAGE]-->
HTML, false);
});
45 changes: 2 additions & 43 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,4 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

// expect()->extend('toBeOne', function () {
// return $this->toBe(1);
// });

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

// function something()
// {
// // ..
// }
uses(Tests\TestCase::class)
->in('Feature');
23 changes: 23 additions & 0 deletions tests/Stubs/DisabledImplement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tests\Stubs;

use Leuverink\AssetInjector\Contracts\AssetInjector;

class DisabledImplement implements AssetInjector
{
public function identifier(): string
{
return 'DISABLED_TEST_PACKAGE';
}

public function enabled(): bool
{
return false;
}

public function inject(): string
{
return 'DISABLED_TEST_PACKAGE_ASSETS_INJECTED';
}
}
2 changes: 1 addition & 1 deletion tests/Implement.php → tests/Stubs/Implement.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests;
namespace Tests\Stubs;

use Leuverink\AssetInjector\Contracts\AssetInjector;

Expand Down
11 changes: 0 additions & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@

use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Leuverink\AssetInjector\Contracts\AssetInjector;

abstract class TestCase extends BaseTestCase
{
use WithWorkbench;

protected function setUp(): void
{
parent::setUp();

$this->app->bind(
AssetInjector::class,
Implement::class
);
}
}

0 comments on commit 6e7787c

Please sign in to comment.