Skip to content

Commit 65d75af

Browse files
authored
WP_CLI tests added (#425)
* WP_CLI tests added * WP_CLI tests added
1 parent 6a8405b commit 65d75af

File tree

7 files changed

+74
-4
lines changed

7 files changed

+74
-4
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"require": {
88
"php": ">=7.4",
99
"composer/installers": "~2.3.0",
10-
"wp-cli/wp-cli-bundle": "*",
1110
"ext-json": "*"
1211
},
1312
"require-dev": {

includes/MslsCli.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
final class MslsCli {
66

7+
/**
8+
* Register the WP-CLI command.
9+
*
10+
* @codeCoverageIgnore
11+
* @return void
12+
*/
713
public static function init(): void {
814
if ( defined( 'WP_CLI' ) && WP_CLI ) {
915
\WP_CLI::add_command( 'msls', new self() );
@@ -32,8 +38,8 @@ public function blog( $args, $assoc_args ): void {
3238

3339
if ( is_null( $blog ) ) {
3440
\WP_CLI::error( sprintf( 'No blog with locale %1$s found!', esc_attr( $locale ) ) );
41+
} else {
42+
\WP_CLI::success( sprintf( 'Blog ID %1$d has locale %2$s!', $blog->userblog_id, esc_attr( $locale ) ) );
3543
}
36-
37-
\WP_CLI::success( sprintf( 'Blog ID %1$d has locale %2$s!', $blog->userblog_id, esc_attr( $locale ) ) );
3844
}
3945
}

tests/phpstan/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php declare(strict_types=1);
22

3+
require_once __DIR__ . '/../phpunit/WP_CLI.php';
4+
5+
class_alias( \lloc\MslsTests\WP_CLI::class, '\WP_CLI' );
6+
37
function is_woocommerce(): bool {
48
return class_exists( 'WooCommerce' );
59
}

tests/phpunit/TestMslsCli.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace lloc\MslsTests;
4+
5+
use Brain\Monkey\Functions;
6+
use lloc\Msls\MslsCli;
7+
8+
final class TestMslsCli extends MslsUnitTestCase {
9+
10+
private function MslsCliFacotry(): MslsCli {
11+
return new MslsCli();
12+
}
13+
14+
public function test_blog_success(): void {
15+
Functions\expect( 'msls_blog' )->once()->with( 'de_DE' )->andReturn( (object) array( 'userblog_id' => 1 ) );
16+
17+
$this->expectOutputString( 'Blog ID 1 has locale de_DE!' );
18+
19+
$this->MslsCliFacotry()->blog( array( 'de_DE' ), array() );
20+
}
21+
22+
public function test_blog_error(): void {
23+
Functions\expect( 'msls_blog' )->once()->with( 'de_DE' )->andReturnNull();
24+
25+
$this->expectOutputString( 'No blog with locale de_DE found!' );
26+
27+
$this->MslsCliFacotry()->blog( array( 'de_DE' ), array() );
28+
}
29+
}

tests/phpunit/TestMslsLink.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Brain\Monkey\Functions;
66
use Brain\Monkey\Filters;
7-
use Hoa\Iterator\Filter;
87
use lloc\Msls\MslsLink;
98

109
final class TestMslsLink extends MslsUnitTestCase {

tests/phpunit/WP_CLI.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace lloc\MslsTests;
4+
5+
class WP_CLI {
6+
7+
/**
8+
* @param string $name
9+
* @param mixed $callable
10+
* @param array $args
11+
*
12+
* @return bool
13+
*/
14+
public static function add_command( $name, $callable, $args = array() ): bool {
15+
return true;
16+
}
17+
18+
/**
19+
* @param string $message
20+
* @param string $exit
21+
*/
22+
public static function error( $message, $exit = true ): void {
23+
echo $message;
24+
}
25+
26+
/**
27+
* @param string $message
28+
*/
29+
public static function success( $message ): void {
30+
echo $message;
31+
}
32+
}

tests/phpunit/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?php
22

33
class_alias( \lloc\MslsTests\WP_Widget::class, '\WP_Widget' );
4+
class_alias( \lloc\MslsTests\WP_CLI::class, '\WP_CLI' );

0 commit comments

Comments
 (0)