raiolanetworks/plugin-seo-test

此Composer包为测试Laravel应用的SEO方面提供无缝集成。兼容Pest和PHPUnit,它提供了一系列工具和断言,专门用于评估页面SEO元素,如元标签、标题标签、规范URL和结构化数据。通过自动化SEO测试,此插件确保您的应用程序始终遵循最佳SEO实践,帮助您在开发周期早期捕捉潜在SEO问题。

1.0.0 2024-09-18 16:16 UTC

This package is auto-updated.

Last update: 2024-09-18 16:22:50 UTC


README

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

此Composer包为测试Laravel应用的SEO方面提供无缝集成。兼容Pest和PHPUnit,它提供了一系列工具和断言,专门用于评估页面SEO元素,如元标签、标题标签、规范URL和结构化数据。通过自动化SEO测试,此插件确保您的应用程序始终遵循最佳SEO实践,帮助您在开发周期早期捕捉潜在SEO问题。

了解我们

安装

安装

您可以通过composer安装此包

composer require raiolanetworks/plugin-seo-test --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 Raiolanetworks\PluginSEOTest\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)。有关更多信息,请参阅许可证文件