Skip to content

Commit 360ae41

Browse files
committed
Added more tests for the plugin main classes.
1 parent 7efff6e commit 360ae41

File tree

13 files changed

+402
-137
lines changed

13 files changed

+402
-137
lines changed

plugins/hwp-previews/codeception.dist.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ coverage:
4040
c3_url: "%WORDPRESS_URL%/wp-content/plugins/hwp-previews/hwp-previews.php"
4141
include:
4242
- src/*
43-
- /access-functions.php
4443
- /activation.php
4544
- /deactivation.php
4645
exclude:
@@ -49,7 +48,6 @@ coverage:
4948
- /node_modules/*
5049
- /packages/*
5150
- /tests/*
52-
- /vendor-prefixed/*
5351
- /vendor/*
5452
- /src/Templates/*
5553
show_only_summary: false

plugins/hwp-previews/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
"tests",
9494
"artifacts",
9595
"package.json",
96-
"package-lock.json"
96+
"package-lock.json",
97+
"node_modules/"
9798
]
9899
},
99100
"autoload": {

plugins/hwp-previews/hwp-previews.php

Lines changed: 86 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -46,79 +46,103 @@
4646
register_deactivation_hook( __FILE__, 'hwp_previews_deactivation_callback' );
4747
}
4848

49-
/**
50-
* Define plugin constants.
51-
*
52-
* phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
53-
* phpcs:disable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
54-
*/
55-
function hwp_previews_constants(): void {
56-
if ( ! defined( 'HWP_PREVIEWS_VERSION' ) ) {
57-
define( 'HWP_PREVIEWS_VERSION', '0.0.1-beta' );
58-
}
5949

60-
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_DIR' ) ) {
61-
define( 'HWP_PREVIEWS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
62-
}
63-
64-
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_URL' ) ) {
65-
define( 'HWP_PREVIEWS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
66-
}
67-
68-
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_FILE' ) ) {
69-
define( 'HWP_PREVIEWS_PLUGIN_FILE', __FILE__ );
70-
}
71-
72-
if ( ! defined( 'HWP_PREVIEWS_AUTOLOAD' ) ) {
73-
define( 'HWP_PREVIEWS_AUTOLOAD', true );
74-
}
75-
76-
if ( ! defined( 'HWP_PREVIEWS_SETTINGS_GROUP' ) ) {
77-
define( 'HWP_PREVIEWS_SETTINGS_GROUP', 'hwp_previews_settings_group' );
78-
}
50+
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
51+
// phpcs:enable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
7952

80-
if ( ! defined( 'HWP_PREVIEWS_SETTINGS_KEY' ) ) {
81-
define( 'HWP_PREVIEWS_SETTINGS_KEY', 'hwp_previews_settings' );
53+
if ( ! function_exists( 'hwp_previews_init' ) ) {
54+
/**
55+
* Initializes plugin.
56+
*/
57+
function hwp_previews_init(): void {
58+
hwp_previews_constants();
59+
hwp_previews_plugin_init();
60+
hwp_previews_plugin_admin_notice();
8261
}
62+
}
8363

84-
if ( ! defined( 'HWP_PREVIEWS_TEXT_DOMAIN' ) ) {
85-
define( 'HWP_PREVIEWS_TEXT_DOMAIN', 'hwp-previews' );
64+
if ( ! function_exists( 'hwp_previews_constants' ) ) {
65+
/**
66+
* Define plugin constants.
67+
*
68+
* phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
69+
* phpcs:disable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
70+
*/
71+
function hwp_previews_constants(): void {
72+
if ( ! defined( 'HWP_PREVIEWS_VERSION' ) ) {
73+
define( 'HWP_PREVIEWS_VERSION', '0.0.1-beta' );
74+
}
75+
76+
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_DIR' ) ) {
77+
define( 'HWP_PREVIEWS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
78+
}
79+
80+
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_URL' ) ) {
81+
define( 'HWP_PREVIEWS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
82+
}
83+
84+
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_FILE' ) ) {
85+
define( 'HWP_PREVIEWS_PLUGIN_FILE', __FILE__ );
86+
}
87+
88+
if ( ! defined( 'HWP_PREVIEWS_AUTOLOAD' ) ) {
89+
define( 'HWP_PREVIEWS_AUTOLOAD', true );
90+
}
91+
92+
if ( ! defined( 'HWP_PREVIEWS_SETTINGS_GROUP' ) ) {
93+
define( 'HWP_PREVIEWS_SETTINGS_GROUP', 'hwp_previews_settings_group' );
94+
}
95+
96+
if ( ! defined( 'HWP_PREVIEWS_SETTINGS_KEY' ) ) {
97+
define( 'HWP_PREVIEWS_SETTINGS_KEY', 'hwp_previews_settings' );
98+
}
99+
100+
if ( ! defined( 'HWP_PREVIEWS_TEXT_DOMAIN' ) ) {
101+
define( 'HWP_PREVIEWS_TEXT_DOMAIN', 'hwp-previews' );
102+
}
86103
}
87104
}
88105

89-
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
90-
// phpcs:enable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
91-
92-
/**
93-
* Initializes plugin.
94-
*/
95-
function hwp_previews_init(): void {
96-
hwp_previews_constants();
97-
98-
if ( defined( 'HWP_PREVIEWS_PLUGIN_DIR' ) ) {
106+
if ( ! function_exists( 'hwp_previews_plugin_init' ) ) {
107+
/**
108+
* Initialize the HWP Previews plugin.
109+
*/
110+
function hwp_previews_plugin_init(): ?Plugin {
111+
if ( ! defined( 'HWP_PREVIEWS_PLUGIN_DIR' ) ) {
112+
return null;
113+
}
99114
require_once HWP_PREVIEWS_PLUGIN_DIR . 'src/Plugin.php';
100-
Plugin::instance();
101-
102-
return;
115+
return Plugin::instance();
103116
}
117+
}
104118

105119

106-
add_action(
107-
'admin_notices',
108-
static function (): void {
109-
?>
110-
<div class="error notice">
111-
<p>
112-
<?php
113-
echo 'Composer vendor directory must be present for HWP Previews to work.'
114-
?>
115-
</p>
116-
</div>
117-
<?php
118-
},
119-
10,
120-
0
121-
);
120+
if ( ! function_exists( 'hwp_previews_plugin_admin_notice' ) ) {
121+
/**
122+
* Display an admin notice if the plugin is not properly initialized.
123+
*/
124+
function hwp_previews_plugin_admin_notice(): void {
125+
if ( defined( 'HWP_PREVIEWS_PLUGIN_DIR' ) ) {
126+
return;
127+
}
128+
129+
add_action(
130+
'admin_notices',
131+
static function (): void {
132+
?>
133+
<div class="error notice">
134+
<p>
135+
<?php
136+
echo 'Composer vendor directory must be present for HWP Previews to work.'
137+
?>
138+
</p>
139+
</div>
140+
<?php
141+
},
142+
10,
143+
0
144+
);
145+
}
122146
}
123147

124148
/**

plugins/hwp-previews/src/Admin/Settings_Page.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public function __construct() {
5252
/**
5353
* Initializes the settings page.
5454
*/
55-
public static function init(): Settings_Page {
55+
public static function init(): ?Settings_Page {
56+
if ( ! is_admin() ) {
57+
return null;
58+
}
5659
if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, self::class ) ) ) {
5760
self::$instance = new self();
5861
}

plugins/hwp-previews/src/Autoloader.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,30 @@ class Autoloader {
2626
* Attempts to autoload the Composer dependencies.
2727
*/
2828
public static function autoload(): bool {
29-
// If we're not *supposed* to autoload anything, then return true.
30-
if ( defined( 'HWP_PREVIEWS_AUTOLOAD' ) && false === HWP_PREVIEWS_AUTOLOAD ) {
31-
return true;
29+
if ( self::get_is_loaded() ) {
30+
return self::get_is_loaded();
3231
}
3332

34-
if ( self::$is_loaded ) {
35-
return self::$is_loaded;
36-
}
37-
38-
$autoloader = dirname( __DIR__ ) . '/vendor/autoload.php';
33+
$autoloader = self::get_composer_autoloader_path();
3934
self::$is_loaded = self::require_autoloader( $autoloader );
4035

36+
return self::get_is_loaded();
37+
}
38+
39+
/**
40+
* If the autoloader has been loaded.
41+
*/
42+
public static function get_is_loaded(): bool {
4143
return self::$is_loaded;
4244
}
4345

46+
/**
47+
* Returns the path to the Composer autoloader.
48+
*/
49+
public static function get_composer_autoloader_path(): string {
50+
return dirname( __DIR__ ) . '/vendor/autoload.php';
51+
}
52+
4453
/**
4554
* Attempts to load the autoloader file, if it exists.
4655
*

plugins/hwp-previews/src/Plugin.php

Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,68 @@
88
use HWP\Previews\Hooks\Preview_Hooks;
99
use HWP\Previews\Integration\Faust_Integration;
1010

11-
if ( ! class_exists( Plugin::class ) ) :
12-
11+
/**
12+
* Plugin class for HWP Previews.
13+
*
14+
* This class serves as the main entry point for the plugin, handling initialization, action and filter hooks.
15+
*
16+
* @package HWP\Previews
17+
*/
18+
final class Plugin {
1319
/**
14-
* Plugin class for HWP Previews.
15-
*
16-
* This class serves as the main entry point for the plugin, handling initialization, action and filter hooks.
20+
* The instance of the plugin.
1721
*
18-
* @package HWP\Previews
22+
* @var \HWP\Previews\Plugin|null
1923
*/
20-
final class Plugin {
21-
/**
22-
* The instance of the plugin.
23-
*
24-
* @var \HWP\Previews\Plugin|null
25-
*/
26-
protected static ?Plugin $instance = null;
27-
28-
/**
29-
* Constructor
30-
*/
31-
public static function instance(): self {
32-
if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, self::class ) ) ) {
33-
self::$instance = new self();
34-
self::$instance->setup();
35-
}
36-
37-
/**
38-
* Fire off init action.
39-
*
40-
* @param self $instance the instance of the plugin class.
41-
*/
42-
do_action( 'hwp_previews_init', self::$instance );
24+
protected static ?Plugin $instance = null;
4325

44-
return self::$instance;
26+
/**
27+
* Constructor
28+
*/
29+
public static function instance(): self {
30+
if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, self::class ) ) ) {
31+
self::$instance = new self();
32+
self::$instance->setup();
4533
}
4634

4735
/**
48-
* Initialize the plugin admin, frontend & api functionality.
36+
* Fire off init action.
4937
*/
50-
public function setup(): void {
51-
if ( is_admin() ) {
52-
Settings_Page::init();
53-
}
38+
do_action( 'hwp_previews_init', self::$instance );
5439

55-
Preview_Hooks::init();
56-
Faust_Integration::init();
57-
}
40+
return self::$instance;
41+
}
5842

59-
/**
60-
* Throw error on object clone.
61-
* The whole idea of the singleton design pattern is that there is a single object
62-
* therefore, we don't want the object to be cloned.
63-
*
64-
* @codeCoverageIgnore
65-
*
66-
* @return void
67-
*/
68-
public function __clone() {
69-
// Cloning instances of the class is forbidden.
70-
_doing_it_wrong( __FUNCTION__, 'The plugin Plugin class should not be cloned.', '0.0.1' );
71-
}
43+
/**
44+
* Initialize the plugin admin, frontend & api functionality.
45+
*/
46+
public function setup(): void {
47+
Settings_Page::init();
48+
Preview_Hooks::init();
49+
Faust_Integration::init();
50+
}
7251

73-
/**
74-
* Disable unserializing of the class.
75-
*
76-
* @codeCoverageIgnore
77-
*/
78-
public function __wakeup(): void {
79-
// De-serializing instances of the class is forbidden.
80-
_doing_it_wrong( __FUNCTION__, 'De-serializing instances of the plugin Main class is not allowed.', '0.0.1' );
81-
}
52+
/**
53+
* Throw error on object clone.
54+
* The whole idea of the singleton design pattern is that there is a single object
55+
* therefore, we don't want the object to be cloned.
56+
*
57+
* @codeCoverageIgnore
58+
*
59+
* @return void
60+
*/
61+
public function __clone() {
62+
// Cloning instances of the class is forbidden.
63+
_doing_it_wrong( __METHOD__, 'The plugin Plugin class should not be cloned.', '0.0.1' );
64+
}
65+
66+
/**
67+
* Disable unserializing of the class.
68+
*
69+
* @codeCoverageIgnore
70+
*/
71+
public function __wakeup(): void {
72+
// De-serializing instances of the class is forbidden.
73+
_doing_it_wrong( __METHOD__, 'De-serializing instances of the plugin Main class is not allowed.', '0.0.1' );
8274
}
83-
endif;
75+
}

plugins/hwp-previews/tests/bootstrap.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,18 @@
44
*
55
* @package Tests\HWP\Previews
66
*/
7+
8+
// Ensure proper autoloading
9+
if (file_exists(__DIR__ . '/../../vendor/autoload.php')) {
10+
require_once __DIR__ . '/../../vendor/autoload.php';
11+
}
12+
13+
// Load the main plugin file to ensure all classes are available
14+
if (file_exists(__DIR__ . '/../../hwp-previews.php')) {
15+
require_once __DIR__ . '/../../hwp-previews.php';
16+
}
17+
18+
// Load access functions if they exist
19+
if (file_exists(__DIR__ . '/../../access-functions.php')) {
20+
require_once __DIR__ . '/../../access-functions.php';
21+
}

0 commit comments

Comments
 (0)