juampi92/test-seo

Easy way to test your SEO

v1.6 2023-03-16 09:59 UTC

This package is auto-updated.

Last update: 2024-12-10 15:42:01 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

An easy-to-use package for testing SEO. The package allows you to extract SEO tags from a given HTML and verify that the SEO structure is correct.

Installation

You can install the package via composer:

composer require juampi92/test-seo --dev

Usage

// Create TestSEO instance using the response:
$seo = new TestSEO($htmlResponse);

// Perform assertions:
$seo->assertTitleEndsWith(' - My Website');

// Assert the data yourself:
$this->assertEquals(
    'My title - My Website',
    $seo->data->title()
);

Look at the following examples using PHPUnit, Laravel, and Pest.

PHPUnit

public function testLandingPageSEO()
{
    // Arrange
    // ...
    
    // Act
    $response = $client->get('/')->send();

    // Assert
    $this->assertEquals(200, $response->getStatusCode());
    $html = json_decode($response->getBody(true), true);

    $seo = new TestSEO($html);
    
    // Assert
    $seo
        ->assertTitleEndsWith(' - My Website')
        ->assertCanonicalIs('https://www.mywebsite.com/');
}

Laravel

public function test_landing_page_SEO()
{
    // Arrange
    // ...
    
    // Act
    $response = $this->get('/');

    // Assert
    $response->assertStatus(200);

    $seo = new TestSEO($response->getContent());
    
    $seo
        ->assertTitleEndsWith(' - My Website')
        ->assertCanonicalIs('https://www.mywebsite.com/');
}

Pest

test('landing page SEO tags', function () {
    // Arrange
    // ...
    
    // Act
    $response = get('/')->assertStatus(200);
    
    $seo = new TestSEO($response->getContent());
    
    // Assert
    expect($seo->data)
        ->title()->toEndWith(' - My Website')
        ->description()->toBe('This is my description')
        ->canonical()->not()->toBeNull()
        ->robots()->index()->toBeTrue()
        ->robots()->nofollow()->toBeTrue();
});

SEO Data

You can access the SEO Data yourself by accessing the public property TestSEO->data. Here are the available methods:

The SEOData class is Macroable, so feel free to extend it yourself.

Assertions

Snapshots

When it comes to SEO, a snapshot test is a great way to ensure nothing has been changed by accident.

Here is an example:

$seo = new TestSEO($response->getContent(), snapshotSerializer: null);

$json = json_encode($seo);

By default, the SEO tags are serialized using the SimpleSerializer. Make your own serializer by implementing the SnapshotSerializer interface:

$seo = new TestSEO($response->getContent(), new MyCustomSerializer());

$json = json_encode($seo);

Pest Example

use function Spatie\Snapshots\{assertMatchesSnapshot, assertMatchesJsonSnapshot};
use Juampi92\TestSEO\TestSEO;

test('landing page SEO', function () {
    $response = $this->get('/');

    $response->assertStatus(200);

    $seo = new TestSEO($response->getContent());

    assertMatchesJsonSnapshot(json_encode($seo));
});

Note: this example requires spatie/pest-plugin-snapshots.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.