Google Tag Manager - v1.0.0

Testing

The package ships a Pest test suite in tests/ covering the snippet renderer, API client, fetchers, controllers, Livewire components, service-provider bindings, and CMS-framework widget contracts. Run with:

composer test
# or
./vendor/bin/pest

Writing tests against the package in your app

Stubbing the API client

Bind a fake TagManagerClient from your test:

use ArtisanPackUI\GoogleTagManager\Api\TagManagerClient;

$this->app->instance( TagManagerClient::class, new class extends TagManagerClient {
    public function __construct() {}
    public function isAvailable(): bool { return true; }
    public function listContainers( $connection, $accountId ): array { return []; }
    // ...
} );

The fetchers and controllers pull TagManagerClient from the container, so binding a stub covers every downstream surface.

Stubbing the base package

The package guards its base-package integrations with a static BaseInstalled::check(). Flip it in tests via:

use ArtisanPackUI\GoogleTagManager\Support\BaseInstalled;

BaseInstalled::setForTesting( true );  // pretend base is installed
BaseInstalled::setForTesting( false ); // pretend it isn't
BaseInstalled::reset();                // clear override

The package's own TestCase calls BaseInstalled::reset() in setUp() and tearDown() — mirror that in yours or the flag will leak between tests.

Stubbing a TokenManager

The package's tests/Pest.php exposes two helpers you can lift into your own test suite:

  • makeGtmStubTokenManager( string $token ) — always returns the given token
  • makeGtmThrowingTokenManager( Throwable $e ) — throws on getValidAccessToken

Bind the returned manager in the container as TokenManager::class and every API call in that test will use the stubbed token.

Snippet output

The snippet renderer is a pure function of config. Testing it needs only:

config( [ 'google-tag-manager.snippet.container_id' => 'GTM-TEST' ] );

$html = app( \ArtisanPackUI\GoogleTagManager\Snippet\SnippetRenderer::class )->render();

expect( $html )->toContain( 'GTM-TEST' );

Blade directive compilation

$compiled = app( 'blade.compiler' )->compileString( '@gtmSnippet' );

expect( $compiled )->toContain( 'SnippetRenderer' );

Livewire components

Because the components pull the TagManagerClient from the container, testing them is a matter of binding a fake client and mounting the component:

use Livewire\Livewire;

Livewire::test( 'google-tag-manager::tag-list', [
    'accountId'   => '1',
    'containerId' => '2',
    'workspaceId' => '3',
] )
    ->assertSet( 'baseInstalled', true )
    ->assertSet( 'errorMessage', null );

HTTP endpoints

The routes require auth + a GoogleConnection. Use the package's makeGtmConnectedConnection() helper or your own factory to satisfy those preconditions.

What the shipped test suite covers

  • Unit\SnippetRendererTest — enable/disable, missing container ID, JS/HTML escaping, combined vs split output
  • Unit\ApiDataTest — every DTO's fromArray / trigger-name hydration / null latest version
  • Feature\TagManagerClientTest — list methods (accounts, containers, workspaces, tags, triggers, variables), pagination via nextPageToken, cache TTL, ApiException wrapping (401/404/transport/token refresh), null latest version header, isAvailable() semantics
  • Feature\FetchersTest — full container overview composition, null latest version fallback, tag-list trigger name join, workspace ID validation
  • Feature\ControllersTest — 401/501/422/409/200 across both endpoints
  • Feature\LivewireComponentsTest — mount, refresh, sort (asc/desc/unknown column), pagination clamp, missing-base state
  • Feature\ServiceProviderTest — singleton bindings, scope contribution, route registration, Blade directive compilation
  • Feature\CmsFrameworkWidgetTest — CMS wrapper interface + Livewire inheritance contracts, getWidgetInfo() payload