Skip to content

Commit 7bbe9bb

Browse files
committed
add PartialPluginInformationTest
Signed-off-by: Chuck Adams <[email protected]>
1 parent 2d7a8c5 commit 7bbe9bb

File tree

2 files changed

+96
-19
lines changed

2 files changed

+96
-19
lines changed

tools/wp-plugin/src/PartialPluginInformation.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Represents as much of a plugins/info/1.2/?action=plugin_information response as we can statically determine,
77
// with null values standing in for fields that must be filled in later. Does no validation or normalization.
88

9-
readonly class PartialPluginInformation
9+
readonly class PartialPluginInformation implements \JsonSerializable
1010
{
1111
public function __construct(
1212
// name, slug, and version are all required. Everything else defaults to null.
@@ -63,12 +63,12 @@ public function __construct(
6363

6464
) {}
6565

66-
public function toArray(): array
66+
public function jsonSerialize(): array
6767
{
6868
return array_filter(get_object_vars($this), static fn($v) => $v !== null);
6969
}
7070

71-
public function fromHeadersAndReadme(
71+
public static function fromHeadersAndReadme(
7272
string $slug,
7373
ParsedPluginHeaders $headers,
7474
ParsedReadme $readme,
@@ -79,23 +79,23 @@ public function fromHeadersAndReadme(
7979
slug : $slug,
8080
version : $headers->version,
8181
author : $headers->author,
82-
author_profile : $headers->author_uri,
82+
author_profile : $headers->author_uri ?: null,
8383
description : $readme->sections['description'] ?? $headers->description,
84-
donate_link : $readme->donate_link,
85-
requires : $headers->requires_wp ?? $readme->requires_wp,
86-
requires_php : $headers->requires_php ?? $readme->requires_php,
87-
short_description: $readme->short_description ?? $headers->description,
88-
sections : $readme->sections,
89-
tags : $readme->tags,
90-
tested : $headers->tested_up_to ?? $readme->tested_up_to,
91-
domain_path : $headers->domain_path,
92-
license : $headers->license ?? $readme->license,
93-
license_uri : $headers->license_uri ?? $readme->license_uri,
94-
network : $headers->network,
95-
plugin_uri : $headers->plugin_uri,
96-
stable_tag : $readme->stable_tag,
97-
text_domain : $headers->text_domain,
98-
update_uri : $headers->update_uri,
84+
donate_link : $readme->donate_link ?: null,
85+
requires : $headers->requires_wp ?? $readme->requires_wp ?: null,
86+
requires_php : $headers->requires_php ?? $readme->requires_php ?: null,
87+
short_description: $readme->short_description ?? $headers->description ?: null,
88+
sections : $readme->sections ?: null,
89+
tags : $readme->tags ?: null,
90+
tested : $headers->tested_up_to ?? $readme->tested_up_to ?: null,
91+
domain_path : $headers->domain_path ?: null,
92+
license : $headers->license ?? $readme->license ?: null,
93+
license_uri : $headers->license_uri ?? $readme->license_uri ?: null,
94+
network : $headers->network ?: null,
95+
plugin_uri : $headers->plugin_uri ?: null,
96+
stable_tag : $readme->stable_tag ?: null,
97+
text_domain : $headers->text_domain ?: null,
98+
update_uri : $headers->update_uri ?: null,
9999
);
100100
// requires_plugins : $headers->requires_plugins, // needs parsing
101101
// contributors : $readme->contributors, // needs db lookups
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
5+
use FAIR\Forge\Tools\WpPlugin\HeaderParser;
6+
use FAIR\Forge\Tools\WpPlugin\PartialPluginInformation;
7+
use FAIR\Forge\Tools\WpPlugin\ReadmeParser;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class PartialPluginInformationTest extends TestCase
11+
{
12+
public function test_parse_hello_dolly(): void
13+
{
14+
$hello_dolly_php = <<<'END'
15+
<?php
16+
/**
17+
* @package Hello_Dolly
18+
* @version 1.7.2
19+
*/
20+
/*
21+
Plugin Name: Hello Dolly
22+
Plugin URI: http://wordpress.org/plugins/hello-dolly/
23+
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
24+
Author: Matt Mullenweg
25+
Version: 1.7.2
26+
Author URI: http://ma.tt/
27+
*/
28+
29+
function hello_dolly_get_lyric() {
30+
/** These are the lyrics to Hello Dolly */
31+
$lyrics = "Hello, Dolly
32+
Well, hello, Dolly
33+
...etc...
34+
END;
35+
36+
$hello_dolly_readme = <<<'END'
37+
=== Hello Dolly ===
38+
Contributors: matt, wordpressdotorg
39+
Stable tag: 1.7.2
40+
Tested up to: 6.9
41+
Requires at least: 4.6
42+
43+
This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong.
44+
45+
== Description ==
46+
47+
This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
48+
49+
Thanks to Sanjib Ahmad for the artwork.
50+
END;
51+
52+
$headers = (new HeaderParser())->parsePluginHeaders($hello_dolly_php);
53+
$readme = (new ReadmeParser())->parse($hello_dolly_readme);
54+
55+
$info = PartialPluginInformation::fromHeadersAndReadme('hello-dolly', $headers, $readme);
56+
$arr = $info->jsonSerialize();
57+
58+
expect($arr)->toBe([
59+
'name' => 'Hello Dolly',
60+
'slug' => 'hello-dolly',
61+
'version' => '1.7.2',
62+
'author' => 'Matt Mullenweg',
63+
'author_profile' => 'http://ma.tt/',
64+
'description' => '<p>This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.</p>
65+
<p>Thanks to Sanjib Ahmad for the artwork.</p>
66+
',
67+
'short_description' => 'This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong.',
68+
'sections' => [
69+
'description' => '<p>This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.</p>
70+
<p>Thanks to Sanjib Ahmad for the artwork.</p>
71+
',
72+
],
73+
'plugin_uri' => 'http://wordpress.org/plugins/hello-dolly/',
74+
'stable_tag' => '1.7.2',
75+
]);
76+
}
77+
}

0 commit comments

Comments
 (0)