Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ parameters:
level: 9
paths:
- lib
- tests
scanDirectories:
- vendor/wp-cli/wp-cli/php
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
- vendor/wp-cli/wp-cli-tests/tests/includes/TestCase.php
treatPhpDocTypesAsCertain: false
81 changes: 44 additions & 37 deletions tests/Test_Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ class Test_Arguments extends TestCase
{
/**
* Array of expected settings
* @var array
* @var array<string, mixed>
*/
protected $settings = null;
protected $settings = array();

/**
* Array of flags
* @var array
* @var array<string, array<string, mixed>>
*/
protected $flags = null;
protected $flags = array();

/**
* Array of expected options
* @var array
* @var array<string, array<string, mixed>>
*/
protected $options = null;
protected $options = array();

/**
* Clear the $_SERVER['argv'] array
*/
public static function clearArgv()
public static function clearArgv(): void
{
$_SERVER['argv'] = array();
$_SERVER['argc'] = 0;
Expand All @@ -41,25 +41,27 @@ public static function clearArgv()
/**
* Add one or more element(s) at the end of the $_SERVER['argv'] array
*
* @param array $args: value(s) to add to the argv array
* @param string|array<string> $args value(s) to add to the argv array
*/
public static function pushToArgv($args)
public static function pushToArgv($args): void
{
if (is_string($args)) {
$args = explode(' ', $args);
}

$argv = is_array($_SERVER['argv']) ? $_SERVER['argv'] : array();
foreach ($args as $arg) {
array_push($_SERVER['argv'], $arg);
$argv[] = $arg;
}

$_SERVER['argc'] += count($args);
$_SERVER['argv'] = $argv;
$_SERVER['argc'] = count($argv);
}

/**
* Set up valid flags and options
*/
public function set_up()
public function set_up(): void
{
self::clearArgv();
self::pushToArgv('my_script.php');
Expand Down Expand Up @@ -103,19 +105,19 @@ static function ( $errno, $errstr ) {
/**
* Tear down fixtures
*/
public function tear_down()
public function tear_down(): void
{
$this->flags = null;
$this->options = null;
$this->settings = null;
$this->flags = array();
$this->options = array();
$this->settings = array();
self::clearArgv();
restore_error_handler();
}

/**
* Test adding a flag, getting a flag and getting all flags
*/
public function testAddFlags()
public function testAddFlags(): void
{
$args = new cli\Arguments($this->settings);

Expand All @@ -138,7 +140,7 @@ public function testAddFlags()
/**
* Test adding a option, getting a option and getting all options
*/
public function testAddOptions()
public function testAddOptions(): void
{
$args = new cli\Arguments($this->settings);

Expand All @@ -157,7 +159,7 @@ public function testAddOptions()
/**
* Data provider with valid args and options
*
* @return array set of args and expected parsed values
* @return array<array{array<string>, array<string, string|bool>}> set of args and expected parsed values
*/
public static function settingsWithValidOptions()
{
Expand All @@ -180,7 +182,7 @@ public static function settingsWithValidOptions()
/**
* Data provider with missing options
*
* @return array set of args and expected parsed values
* @return array<array{array<string>, array<string, string|bool>}> set of args and expected parsed values
*/
public static function settingsWithMissingOptions()
{
Expand All @@ -199,7 +201,7 @@ public static function settingsWithMissingOptions()
/**
* Data provider with missing options. The default value should be populated
*
* @return array set of args and expected parsed values
* @return array<array{array<string>, array<string, string|bool>}> set of args and expected parsed values
*/
public static function settingsWithMissingOptionsWithDefault()
{
Expand All @@ -215,6 +217,11 @@ public static function settingsWithMissingOptionsWithDefault()
);
}

/**
* Data provider with no options. The default values should be populated
*
* @return array<array{array<string>, array<string, string|bool>}> set of args and expected parsed values
*/
public static function settingsWithNoOptionsWithDefault()
{
return array(
Expand All @@ -228,10 +235,10 @@ public static function settingsWithNoOptionsWithDefault()
/**
* Generic private testParse method.
*
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
* @param array<string> $cliParams arguments as they appear in the cli
* @param array<string, string|bool> $expectedValues expected values after parsing
*/
private function _testParse($cliParams, $expectedValues)
private function _testParse($cliParams, $expectedValues): void
{
self::pushToArgv($cliParams);

Expand All @@ -250,52 +257,52 @@ private function _testParse($cliParams, $expectedValues)
}

/**
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
* @param array<string> $cliParams arguments as they appear in the cli
* @param array<string, string|bool> $expectedValues expected values after parsing
*
* @dataProvider settingsWithValidOptions
*/
#[DataProvider( 'settingsWithValidOptions' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function testParseWithValidOptions($cliParams, $expectedValues)
public function testParseWithValidOptions($cliParams, $expectedValues): void
{
$this->_testParse($cliParams, $expectedValues);
}

/**
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
* @param array<string> $cliParams arguments as they appear in the cli
* @param array<string, string|bool> $expectedValues expected values after parsing
* @dataProvider settingsWithMissingOptions
*/
#[DataProvider( 'settingsWithMissingOptions' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function testParseWithMissingOptions($cliParams, $expectedValues)
public function testParseWithMissingOptions($cliParams, $expectedValues): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('no value given for --option1');
$this->_testParse($cliParams, $expectedValues);
}

/**
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
* @param array<string> $cliParams arguments as they appear in the cli
* @param array<string, string|bool> $expectedValues expected values after parsing
* @dataProvider settingsWithMissingOptionsWithDefault
*/
#[DataProvider( 'settingsWithMissingOptionsWithDefault' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function testParseWithMissingOptionsWithDefault($cliParams, $expectedValues)
public function testParseWithMissingOptionsWithDefault($cliParams, $expectedValues): void
{
$this->_testParse($cliParams, $expectedValues);
}

/**
* @param array $args arguments as they appear in the cli
* @param array $expectedValues expected values after parsing
* @param array<string> $cliParams arguments as they appear in the cli
* @param array<string, string|bool> $expectedValues expected values after parsing
* @dataProvider settingsWithNoOptionsWithDefault
*/
#[DataProvider( 'settingsWithNoOptionsWithDefault' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound
public function testParseWithNoOptionsWithDefault($cliParams, $expectedValues) {
public function testParseWithNoOptionsWithDefault($cliParams, $expectedValues): void {
$this->_testParse($cliParams, $expectedValues);
}

public function testHelpScreenRender() {
public function testHelpScreenRender(): void {
$args = new \cli\Arguments( $this->settings );
$help = $args->getHelpScreen();

Expand Down
Loading