juampi92/test-seo

轻松测试您的SEO

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

This package is auto-updated.

Last update: 2024-09-16 13:26:13 UTC


README

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

一个用于测试SEO的简单易用的包。此包允许您从给定的HTML中提取SEO标签,并验证SEO结构是否正确。

安装

您可以通过composer安装此包

composer require juampi92/test-seo --dev

用法

// 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()
);

以下是一些使用PHPUnitLaravelPest的示例。

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数据

您可以通过访问公共属性TestSEO->data来访问SEO数据。以下是一些可用方法

SEOData类是可宏扩展的,因此您可以自由扩展它。

断言

快照

当谈到SEO时,快照测试是一种确保没有意外更改的好方法。

以下是一个示例

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

$json = json_encode($seo);

默认情况下,SEO标签使用SimpleSerializer序列化。您可以通过实现SnapshotSerializer接口来创建自己的序列化器

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

$json = json_encode($seo);

Pest示例

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));
});

注意:此示例需要spatie/pest-plugin-snapshots

贡献

有关详细信息,请参阅CONTRIBUTING

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件