Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ jobs:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v1
uses: ramsey/composer-install@v2

- name: Run PHPStan
run: ./vendor/bin/phpstan analyze -c phpstan.neon.dist --error-format=github
9 changes: 6 additions & 3 deletions .github/workflows/pint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ name: Fix PHP code style issues

on: [push]

permissions:
contents: write

jobs:
php-code-styling:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

- name: Fix PHP code style issues
uses: aglipanci/laravel-pint-action@1.0.0
uses: aglipanci/laravel-pint-action@2.5

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Fix styling
9 changes: 3 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.0, 8.1, 8.2]
phpunit: [^8.5.23, 9.*, 10.*]
php: [8.2, 8.3, 8.4]
phpunit: [10.*, 11.*]
stability: [prefer-lowest, prefer-stable]
exclude:
- php: 8.0
phpunit: 10.*

name: P${{ matrix.php }} - PHPUnit ${{ matrix.phpunit }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
}
],
"require": {
"php": "^8.0.2",
"illuminate/macroable": "^7.0|^8.0|^9.0|^10.0|^11.0",
"phpunit/phpunit": "^8.3|^9.0|^10.0",
"spatie/url": "^1.3.4|^2.0",
"symfony/dom-crawler": "^5.4|^6.1"
"php": "^8.2",
"illuminate/macroable": "^11.0|^12.0",
"phpunit/phpunit": "^10.0|^11.0",
"spatie/url": "^2.0",
"symfony/dom-crawler": "^6.1|^7.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"laravel/pint": "^1.18",
"phpstan/phpstan": "^1.8"
},
"autoload": {
Expand Down
21 changes: 1 addition & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
parameters:
ignoreErrors:
-
message: "#^Method Juampi92\\\\TestSEO\\\\SEOData\\:\\:charset\\(\\) should return string\\|null but returns array\\<string, string\\|null\\>\\|string\\|null\\.$#"
count: 1
path: src/SEOData.php

-
message: "#^Method Juampi92\\\\TestSEO\\\\SEOData\\:\\:description\\(\\) should return string\\|null but returns array\\<string, string\\|null\\>\\|string\\|null\\.$#"
count: 1
path: src/SEOData.php

-
message: "#^Parameter \\#1 \\$content of class Juampi92\\\\TestSEO\\\\Tags\\\\Robots constructor expects string, array\\<string, string\\|null\\>\\|string given\\.$#"
count: 1
path: src/SEOData.php

-
message: "#^Parameter \\#1 \\$url of static method Spatie\\\\Url\\\\Url\\:\\:fromString\\(\\) expects string, array\\<string, string\\|null\\>\\|string given\\.$#"
count: 4
path: src/SEOData.php
ignoreErrors: []
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ parameters:
- src
level: 7
#excludePaths:
checkMissingIterableValueType: false
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- identifier: missingType.iterableValue
17 changes: 11 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
cacheDirectory=".phpunit.cache"
>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
</source>
<testsuites>
<testsuite name="Permissions Test Suite">
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
</php>
</phpunit>
21 changes: 12 additions & 9 deletions src/Parser/HTMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public function grabTextFrom(string $xpath): ?string
}

/**
* @param string|array<string> $attributes
* @return string|array<string, string|null>|null
* @template T of string|array<string>
*
* @param T $attributes
* @return (T is string ? string|null : array<string, string|null>|null)
*/
public function grabAttributeFrom(string $xpath, $attributes)
public function grabAttributeFrom(string $xpath, string|array $attributes)
{
$nodes = $this->crawler->filterXPath($xpath);

Expand All @@ -38,12 +40,12 @@ public function grabAttributeFrom(string $xpath, $attributes)
}

/**
* @param string|array<string>|null $attribute
* @param string|array<string>|null $attribute
*/
public function grabMultiple(string $xpath, $attribute = null): array
{
$result = [];
$nodes = $this->crawler->filterXPath($xpath);
$nodes = $this->crawler->filterXPath($xpath);

foreach ($nodes as $node) {
$result[] = $attribute !== null ? $this->getArgumentsFromNode($node, $attribute) : $node->textContent;
Expand All @@ -53,11 +55,12 @@ public function grabMultiple(string $xpath, $attribute = null): array
}

/**
* @param DOMElement|DOMNode|null $element
* @param string|array<string> $attributes
* @return string|array<string, string|null>
* @template T of string|array<string>
*
* @param T $attributes
* @return (T is string ? string : array<string, string|null>)
*/
private function getArgumentsFromNode($element, $attributes)
private function getArgumentsFromNode(DOMElement|DOMNode|null $element, string|array $attributes)
{
if (! $element || ! ($element instanceof DOMElement)) {
return [];
Expand Down
23 changes: 12 additions & 11 deletions src/SnapshotFormatters/SimpleSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@

use Raiolanetworks\PluginSEOTest\SEOData;
use Raiolanetworks\PluginSEOTest\Tags\TagCollection;
use Stringable;

class SimpleSerializer implements SnapshotSerializer
{
public function toArray(SEOData $data): array
{
return [
'title' => $data->title(),
'description' => $data->description(),
'robots' => (string) $data->robots(),
'canonical' => $this->formatUrl($data->canonical()),
'pagination' => [
'title' => $data->title(),
'description' => $data->description(),
'robots' => (string) $data->robots(),
'canonical' => $this->formatUrl($data->canonical()),
'pagination' => [
'prev' => $this->formatUrl($data->prev()),
'next' => $this->formatUrl($data->next()),
],
'relAltHreflang' => array_map(
fn (array $item) => [
'hreflang' => $item['hreflang'],
'href' => $this->formatUrl($item['href']),
'href' => $this->formatUrl($item['href']),
],
$data->alternateHrefLang()->jsonSerialize()
),
'h1' => $data->h1s(),
'opengraph' => $this->formatTagCollection($data->openGraph()),
'twitter' => $this->formatTagCollection($data->twitter()),
'h1' => $data->h1s(),
'opengraph' => $this->formatTagCollection($data->openGraph()),
'twitter' => $this->formatTagCollection($data->twitter()),
];
}

Expand Down Expand Up @@ -61,12 +62,12 @@ protected function formatIfUrl(?string $url): ?string
return $this->formatUrl($url);
}

protected function formatUrl(?string $url): ?string
protected function formatUrl(string|Stringable|null $url): ?string
{
if (! $url) {
return null;
}

return preg_replace('/\d+/', '{id}', $url);
return preg_replace('/\d+/', '{id}', (string) $url);
}
}
6 changes: 3 additions & 3 deletions src/Support/ArrayPluck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class ArrayPluck
{
/**
* @param array<array<string, string>> $items
* @param array<array<string, string>> $items
*/
public function __construct(private array $items) {}

Expand All @@ -16,12 +16,12 @@ public function __construct(private array $items) {}
*/
public function __invoke(string $key, string $value): array
{
$array = $this->items;
$array = $this->items;
$results = [];

foreach ($array as $item) {
$itemValue = $item[$value];
$itemKey = $item[$key];
$itemKey = $item[$key];

if (! isset($results[$itemKey])) {
$results[$itemKey] = $itemValue;
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Memo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait Memo
/**
* @template TValue
*
* @param callable(): TValue $value
* @param callable(): TValue $value
* @return TValue
*/
protected function memo(string $key, callable $value): mixed
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/TagCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
public function get(string $property)
{
// Normalize property
$property = $this->prefix . ltrim($property, $this->prefix);
$property = $this->prefix.ltrim($property, $this->prefix);

return $this->metadata[$property] ?? null;
}
Expand Down
11 changes: 7 additions & 4 deletions src/TestSEO.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class TestSEO implements JsonSerializable

public function __construct(string $content, ?SnapshotSerializer $snapshotSerializer = null)
{
$html = new HTMLParser($content);
$this->data = new SEOData($html);
$html = new HTMLParser($content);
$this->data = new SEOData($html);
$this->snapshotSerializer = $snapshotSerializer ?? new SimpleSerializer;
}

Expand Down Expand Up @@ -52,8 +52,8 @@ public function assertRobotsIsNoIndexNoFollow(): self
{
$robots = $this->data->robots();

Assert::assertTrue($robots->noindex(), 'Robots should be noindex and nofollow, but found: ' . (string) $robots);
Assert::assertTrue($robots->nofollow(), 'Robots should be noindex and nofollow, but found: ' . (string) $robots);
Assert::assertTrue($robots->noindex(), 'Robots should be noindex and nofollow, but found: '.(string) $robots);
Assert::assertTrue($robots->nofollow(), 'Robots should be noindex and nofollow, but found: '.(string) $robots);

return $this;
}
Expand Down Expand Up @@ -87,6 +87,9 @@ public function assertTitleContains(string $expected): self
return $this;
}

/**
* @param non-empty-string $expected
*/
public function assertTitleEndsWith(string $expected): self
{
Assert::assertStringEndsWith($expected, $this->data->title());
Expand Down
19 changes: 9 additions & 10 deletions tests/ArrayPluckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace Raiolanetworks\PluginSEOTest\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Raiolanetworks\PluginSEOTest\Support\ArrayPluck;

class ArrayPluckTest extends TestCase
{
/**
* @dataProvider pluckDataProvider
*/
#[DataProvider('pluckDataProvider')]
public function test_should_pluck_array(array $expected, array $items, string $key, string $value): void
{
$this->assertEqualsCanonicalizing(
Expand All @@ -23,21 +22,21 @@ public function test_should_pluck_array(array $expected, array $items, string $k
public static function pluckDataProvider(): array
{
return [
'It plucks the values' => [
'It plucks the values' => [
'expected' => ['foo' => 'bar', 'foo2' => 'bar2'],
'items' => [['name' => 'foo', 'content' => 'bar'], ['name' => 'foo2', 'content' => 'bar2']],
'key' => 'name',
'value' => 'content',
'items' => [['name' => 'foo', 'content' => 'bar'], ['name' => 'foo2', 'content' => 'bar2']],
'key' => 'name',
'value' => 'content',
],
'It groups results into an array when the keys are the same' => [
'expected' => ['foo' => ['bar', 'bar2'], 'foo2' => 'bar3'],
'items' => [
'items' => [
['property' => 'foo', 'content' => 'bar'],
['property' => 'foo', 'content' => 'bar2'],
['property' => 'foo2', 'content' => 'bar3'],
],
'key' => 'property',
'value' => 'content',
'key' => 'property',
'value' => 'content',
],
];
}
Expand Down
Loading