Skip to content

Commit 6c3fae1

Browse files
committed
Tests added
1 parent 3808348 commit 6c3fae1

File tree

8 files changed

+46
-13
lines changed

8 files changed

+46
-13
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"squizlabs/php_codesniffer": "^3.9",
2020
"phpcompatibility/php-compatibility": "^9.3",
2121
"wp-coding-standards/wpcs": "^3.0",
22-
"smeghead/php-class-diagram": "^1.3"
22+
"smeghead/php-class-diagram": "^1.3",
23+
"phpstan/phpstan-mockery": "^1.1"
2324
},
2425
"autoload": {
2526
"psr-4": {

includes/ContentImport/AttachmentPathFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AttachmentPathFinder extends MslsRegistryInstance {
1414
* @param string $imageSrc
1515
* @param mixed $imageMeta
1616
* @param int $attachmentId
17-
* @return array
17+
* @return array<string, string>
1818
*/
1919
public function filter_srcset( array $sources, $sizeArray, $imageSrc, $imageMeta, $attachmentId ): array {
2020
if ( ! $msls_imported = $this->has_import_data( $attachmentId ) ) {

includes/MslsWidget.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
*/
1616
class MslsWidget extends \WP_Widget {
1717

18-
public $id_base = 'mslswidget';
18+
public const ID_BASE = 'mslswidget';
1919

2020
public function __construct() {
2121
$name = apply_filters(
2222
'msls_widget_title',
2323
__( 'Multisite Language Switcher', 'multisite-language-switcher' )
2424
);
2525

26-
parent::__construct( $this->id_base, $name, array( 'show_instance_in_rest' => true ) );
26+
parent::__construct( self::ID_BASE, $name, array( 'show_instance_in_rest' => true ) );
2727
}
2828

2929
public static function init(): void {

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ parameters:
66
- includes
77
excludePaths:
88
- vendor
9-
- tests
109
bootstrapFiles:
1110
- tests/phpstan/bootstrap.php

phpunit.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?xml version="1.0"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/phpunit/bootstrap.php" backupGlobals="true" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="tests/phpunit/bootstrap.php"
5+
backupGlobals="true"
6+
colors="true"
7+
displayDetailsOnTestsThatTriggerWarnings="true"
8+
cacheDirectory=".phpunit.cache">
39
<php>
410
<const name="MSLS_PLUGIN_PATH" value="multisite-language-switcher/MultisiteLanguageSwitcher.php"/>
511
<const name="MSLS_PLUGIN__FILE__" value="/var/www/html/wp-content/plugins/multisite-language-switcher/MultisiteLanguageSwitcher.php"/>

tests/phpunit/TestMslsPostTag.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public function test_edit_input(): void {
9090
$taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post' );
9191
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_tag' ) );
9292

93-
$term = \Mockery::mock( \WP_Term::class );
93+
$term = \Mockery::mock( \WP_Term::class );
94+
$term->name = 'test-term-name';
9495

9596
Functions\expect( 'msls_content_types' )->atLeast()->once()->andReturn( $taxonomy );
9697
Functions\expect( 'get_queried_object_id' )->atLeast()->once()->andReturn( 42 );
@@ -115,7 +116,7 @@ public function test_edit_input(): void {
115116
</th>
116117
<td>
117118
<input type="hidden" id="msls_id_" name="msls_input_de_DE" value="42"/>
118-
<input class="msls_title" id="msls_title_" name="msls_title_" type="text" value=""/>
119+
<input class="msls_title" id="msls_title_" name="msls_title_" type="text" value="test-term-name"/>
119120
</td>
120121
</tr><tr class="form-field">
121122
<th scope="row">

tests/phpunit/TestMslsWidget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function test_update(): void {
6565
}
6666

6767
public function test_form(): void {
68-
$expected = '<p><label for="title">Title:</label> <input class="widefat" id="title" name="title" type="text" value="" /></p>';
68+
$expected = '<p><label for="field-id-title">Title:</label> <input class="widefat" id="field-id-title" name="field-name-title" type="text" value="" /></p>';
6969

7070
$this->expectOutputString( $expected );
7171

tests/phpunit/WP_Widget.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,43 @@
22

33
namespace lloc\MslsTests;
44

5+
#[AllowDynamicProperties]
56
class WP_Widget {
67

8+
public $id_base;
79

10+
/**
11+
* PHP5 constructor.
12+
*
13+
* @since 2.8.0
14+
*
15+
* @param string $id_base Base ID for the widget, lowercase and unique. If left empty,
16+
* a portion of the widget's PHP class name will be used. Has to be unique.
17+
* @param string $name Name for the widget displayed on the configuration page.
18+
* @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for
19+
* information on accepted arguments. Default empty array.
20+
* @param array $control_options Optional. Widget control options. See wp_register_widget_control() for
21+
* information on accepted arguments. Default empty array.
22+
*/
823
public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
924
}
1025

11-
public function get_field_id( $id ) {
12-
return $id;
26+
27+
/**
28+
* @param string $field_name Field name.
29+
*
30+
* @return string ID attribute for `$field_name`.
31+
*/
32+
public function get_field_id( string $field_name ) {
33+
return sprintf( 'field-id-%s', esc_attr( $field_name ) );
1334
}
1435

15-
public function get_field_name( $name ) {
16-
return $name;
36+
/**
37+
* @param string $field_name Field name.
38+
*
39+
* @return string Name attribute for `$field_name`.
40+
*/
41+
public function get_field_name( $field_name ) {
42+
return sprintf( 'field-name-%s', esc_attr( $field_name ) );
1743
}
1844
}

0 commit comments

Comments
 (0)