Skip to content

Commit d6d5439

Browse files
committed
Updated tests
1 parent 350cbc7 commit d6d5439

12 files changed

Lines changed: 95 additions & 23 deletions

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: install install-php install-js test test-php test-e2e phpstan phpcs lint-php dc-up dc-down wporg-sync release-prepare
1+
.PHONY: install install-php install-js test test-php test-e2e phpstan phpstan-baseline phpcs lint-php dc-up dc-down wporg-sync release-prepare
22

33
install: install-php install-js
44

@@ -19,6 +19,9 @@ test-e2e:
1919
phpstan:
2020
php vendor/bin/phpstan analyse -c phpstan.neon
2121

22+
phpstan-baseline:
23+
php vendor/bin/phpstan analyse -c phpstan.neon --generate-baseline=phpstan-baseline.neon --allow-empty-baseline
24+
2225
phpcs:
2326
php vendor/bin/phpcs --standard=phpcs.xml
2427

phpstan-baseline.neon

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Method FapiConversionPlugin\\Admin\\SettingsPage\:\:renderConsentSection\(\) is unused\.$#'
5+
identifier: method.unused
6+
count: 1
7+
path: src/Admin/SettingsPage.php
8+
9+
-
10+
message: '#^Call to function load_plugin_textdomain\(\) on a separate line has no effect\.$#'
11+
identifier: function.resultUnused
12+
count: 1
13+
path: src/Plugin.php
14+
15+
-
16+
message: '#^Function load_plugin_textdomain invoked with 3 parameters, 0 required\.$#'
17+
identifier: arguments.count
18+
count: 1
19+
path: src/Plugin.php
20+
21+
-
22+
message: '#^Parameter \#2 \$callback of function add_action expects callable\(mixed \.\.\.\)\: mixed, array\{\$this\(FapiConversionPlugin\\Plugin\), ''enqueueAdminAssets''\} given\.$#'
23+
identifier: argument.type
24+
count: 1
25+
path: src/Plugin.php

phpstan-bootstrap.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
if (!defined('ABSPATH')) {
4+
define('ABSPATH', __DIR__ . '/');
5+
}
6+
if (!defined('FAPI_SIGNALS_PATH')) {
7+
define('FAPI_SIGNALS_PATH', __DIR__ . '/');
8+
}
9+
if (!defined('FAPI_SIGNALS_URL')) {
10+
define('FAPI_SIGNALS_URL', 'https://example.org/wp-content/plugins/fapi-signals/');
11+
}
12+
if (!defined('FAPI_SIGNALS_VERSION')) {
13+
define('FAPI_SIGNALS_VERSION', '0.1.0');
14+
}

phpstan.neon

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
14
parameters:
25
level: 6
6+
bootstrapFiles:
7+
- phpstan-bootstrap.php
38
ignoreErrors:
49
- '#Function .* not found\.#'
5-
- '#Instantiated class .* not found\.#'
6-
- '#class\.notFound#'
710
paths:
811
- fapi-signals.php
912
- uninstall.php

src/Admin/SettingsPage.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,6 @@ public function renderPage(): void
120120
<?php
121121
}
122122

123-
private function checkbox(string $name, bool $checked): string
124-
{
125-
return sprintf(
126-
'<input type="checkbox" name="%s[%s]" value="1" %s>',
127-
esc_attr(Settings::OPTION_KEY),
128-
esc_attr($name),
129-
checked($checked, true, false)
130-
);
131-
}
132-
133123
private function switchMarkup(string $name, bool $checked, string $ariaLabel, bool $defaultWhenNotSet = false): string
134124
{
135125
$out = '<label class="fapi-switch" aria-label="' . esc_attr($ariaLabel) . '">';

src/Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct()
3333

3434
public function init(): void
3535
{
36-
add_action('plugins_loaded', function () {
36+
add_action('plugins_loaded', function (): void {
3737
load_plugin_textdomain(
3838
'fapi-signals',
3939
false,

src/ServerSide/PayloadBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private function buildUserProfile(): array
110110
return [];
111111
}
112112
$user = wp_get_current_user();
113-
if (!$user || !isset($user->ID) || (int) $user->ID <= 0) {
113+
if (!isset($user->ID) || (int) $user->ID <= 0) {
114114
return [];
115115
}
116116

tests/PageViewDispatcherTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,34 @@
22

33
namespace {
44
if (!function_exists('sanitize_text_field')) {
5-
function sanitize_text_field($value)
5+
/** @param string $value */
6+
function sanitize_text_field($value): string
67
{
78
return $value;
89
}
910
}
1011

1112
if (!function_exists('esc_url_raw')) {
12-
function esc_url_raw($value)
13+
/** @param string $value */
14+
function esc_url_raw($value): string
1315
{
1416
return $value;
1517
}
1618
}
1719

1820
if (!function_exists('wp_generate_uuid4')) {
19-
function wp_generate_uuid4()
21+
function wp_generate_uuid4(): string
2022
{
2123
return 'uuid-1';
2224
}
2325
}
2426

2527
if (!function_exists('wp_remote_post')) {
28+
/**
29+
* @param string $url
30+
* @param array<string, mixed> $args
31+
* @return array{response: array{code: int}}
32+
*/
2633
function wp_remote_post($url, $args)
2734
{
2835
$GLOBALS['remotePosts'][] = [$url, $args];
@@ -31,13 +38,19 @@ function wp_remote_post($url, $args)
3138
}
3239

3340
if (!function_exists('wp_json_encode')) {
34-
function wp_json_encode($value)
41+
/** @param mixed $value */
42+
function wp_json_encode($value): string
3543
{
3644
return json_encode($value);
3745
}
3846
}
3947

4048
if (!function_exists('get_option')) {
49+
/**
50+
* @param string $key
51+
* @param mixed $default
52+
* @return mixed
53+
*/
4154
function get_option($key, $default = null)
4255
{
4356
return $GLOBALS['testOptions'][$key] ?? $default;
@@ -46,13 +59,16 @@ function get_option($key, $default = null)
4659

4760
class WP_REST_Request
4861
{
62+
/** @var array<string, mixed> */
4963
private array $params;
5064

65+
/** @param array<string, mixed> $params */
5166
public function __construct(array $params)
5267
{
5368
$this->params = $params;
5469
}
5570

71+
/** @return mixed */
5672
public function get_param(string $key)
5773
{
5874
return $this->params[$key] ?? null;
@@ -61,9 +77,11 @@ public function get_param(string $key)
6177

6278
class WP_REST_Response
6379
{
80+
/** @var array<string, mixed> */
6481
public array $data;
6582
public int $status;
6683

84+
/** @param array<string, mixed> $data */
6785
public function __construct(array $data, int $status)
6886
{
6987
$this->data = $data;
@@ -112,10 +130,12 @@ public function testHandlePageViewSendsPayload(): void
112130
$response = $dispatcher->handlePageView($request);
113131

114132
$this->assertSame('sent', $response->data['status']);
115-
$this->assertSame(1, count($GLOBALS['remotePosts']));
133+
/** @var list<array{0: string, 1: mixed}> $posts */
134+
$posts = $GLOBALS['remotePosts'];
135+
$this->assertSame(1, count($posts));
116136
$this->assertSame(
117137
'https://graph.facebook.com/v18.0/123/events?access_token=token',
118-
$GLOBALS['remotePosts'][0][0]
138+
$posts[0][0]
119139
);
120140
}
121141
}

tests/PayloadBuilderTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function is_user_logged_in(): bool
99
}
1010

1111
if (!function_exists('wp_get_current_user')) {
12+
/** @return \stdClass */
1213
function wp_get_current_user()
1314
{
1415
$user = new stdClass();
@@ -21,6 +22,12 @@ function wp_get_current_user()
2122
}
2223

2324
if (!function_exists('get_user_meta')) {
25+
/**
26+
* @param int $userId
27+
* @param string $key
28+
* @param bool $single
29+
* @return mixed
30+
*/
2431
function get_user_meta($userId, $key, $single = true)
2532
{
2633
return $GLOBALS['testUserMeta'][$key] ?? '';

tests/PluginInitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace {
44
if (!function_exists('add_action')) {
5+
/**
6+
* @param string $hook
7+
* @param callable(mixed...): mixed $callback
8+
*/
59
function add_action($hook, $callback): void
610
{
711
$GLOBALS['testActions'][] = [$hook, $callback];

0 commit comments

Comments
 (0)