raiolanetworks/plugin-seo-test
Composer 安装命令:
composer require --dev raiolanetworks/plugin-seo-test
包简介
This Composer package provides a seamless integration for testing SEO aspects of your Laravel applications. Compatible with both Pest and PHPUnit, it offers a collection of tools and assertions specifically designed to evaluate on-page SEO elements like meta tags, title tags, canonical URLs, and str
README 文档
README
SEO testing assertions for Laravel applications. Compatible with PHPUnit and Pest.
Test meta tags, titles, canonical URLs, Open Graph, Twitter Cards, robots directives, and more with a fluent API.
Requirements
- PHP 8.2+ (8.3+ required when used with Laravel 13)
- Laravel 11, 12, or 13
- PHPUnit 10, 11, or 12
Table of Contents
Installation
You can install the package via composer:
composer require raiolanetworks/plugin-seo-test --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:
| Method | Returns | Description |
|---|---|---|
title() |
?string |
<title>{this}</title> |
description() |
?string |
<meta name="description" content="{this}"> |
image() |
?Url 🔍 |
<meta name="image" content="{this}"> |
robots() |
Robots 🔍 |
<meta name="robots" content="{this}"> |
canonical() |
?Url 🔍 |
<link rel="canonical" href="{this}"> |
prev() |
?Url 🔍 |
<link rel="prev" href="{this}"> |
next() |
?Url 🔍 |
<link rel="next" href="{this}"> |
openGraph() |
TagCollection 🔍 |
<meta property="og:{key}" content="{value}"> |
twitter() |
TagCollection 🔍 |
<meta name="twitter:{key}" content="{value}"> |
alternateHrefLang() |
AlternateHrefLangCollection 🔍 |
<link name="alternate" hreflang="{hreflang}" href={href}> |
images() |
array<array{src: string, alt: string, title: string}> |
All images in the page. <img src="..."> |
h1s() |
array<string> |
All H1 in the page. <h1>{this}</h1> |
h2s() |
array<string> |
All H2 in the page. <h2>{this}</h2> |
charset() |
?string |
<meta charset="utf-8"> |
The SEOData class is Macroable, so feel free to extend it yourself.
Assertions
| Method | Notes |
|---|---|
assertCanonicalIs(string $expected) |
|
assertCanonicalIsEmpty() |
|
assertRobotsIsEmpty() |
|
assertRobotsIsNoIndexNoFollow() |
Checks that the robots are noindex, nofollow or none |
assertPaginationIsEmpty() |
prev and next are both missing. |
assertAlternateHrefLangIsEmpty() |
|
assertTitleIs(string $expected) |
|
assertTitleContains(string $expected) |
|
assertTitleEndsWith(string $expected) |
|
assertDescriptionIs(string $expected) |
|
assertThereIsOnlyOneH1() |
Make sure there is only one H1 in the entire website. |
assertAllImagesHaveAltText() |
Make sure all images have an alt="..." |
| Suggest your own! | These assertions can help devs to follow the best SEO practices. Make a PR if you think some are missing! |
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()); $json = json_encode($seo);
By default, the SEO tags are serialized using the SimpleSerializer.
You can provide 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 Raiolanetworks\PluginSEOTest\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.
Changelog
See Releases for a list of changes.
Contributing
Please see CONTRIBUTING for details.
Credits
- Martin Gomez
- David Eguiluz
- Based on juampi92/test-seo
License
The MIT License (MIT). Please see License File for more information.
raiolanetworks/plugin-seo-test 适用场景与选型建议
raiolanetworks/plugin-seo-test 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.61k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 09 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「testing」 「phpunit」 「seo」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 raiolanetworks/plugin-seo-test 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 raiolanetworks/plugin-seo-test 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 raiolanetworks/plugin-seo-test 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
The PHP SDK for Checkmango
PHPUnit DbUnit Native Array-based Fixtures
Alfabank REST API integration
统计信息
- 总下载量: 1.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-18