forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgValidationTest.php
More file actions
74 lines (55 loc) · 2.22 KB
/
ArgValidationTest.php
File metadata and controls
74 lines (55 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
use WP_CLI\SynopsisValidator;
use WP_CLI\Tests\TestCase;
class ArgValidationTest extends TestCase {
public function testMissingPositional(): void {
$validator = new SynopsisValidator( '<foo> <bar> [<baz>]' );
$this->assertFalse( $validator->enough_positionals( [] ) );
$this->assertTrue( $validator->enough_positionals( [ 1, 2 ] ) );
$this->assertTrue( $validator->enough_positionals( [ 1, 2, 3, 4 ] ) );
$this->assertEquals( [ 4 ], $validator->unknown_positionals( [ 1, 2, 3, 4 ] ) );
}
public function testRepeatingPositional(): void {
$validator = new SynopsisValidator( '<foo> [<bar>...]' );
$this->assertFalse( $validator->enough_positionals( [] ) );
$this->assertTrue( $validator->enough_positionals( [ 1 ] ) );
$this->assertTrue( $validator->enough_positionals( [ 1, 2, 3 ] ) );
$this->assertEmpty( $validator->unknown_positionals( [ 1, 2, 3 ] ) );
}
public function testUnknownAssocEmpty(): void {
$validator = new SynopsisValidator( '' );
$assoc_args = [
'foo' => true,
'bar' => false,
];
$this->assertEquals( array_keys( $assoc_args ), $validator->unknown_assoc( $assoc_args ) );
}
public function testUnknownAssoc(): void {
$validator = new SynopsisValidator( '--type=<type> [--brand=<brand>] [--flag]' );
$assoc_args = [
'type' => 'analog',
'brand' => true,
'flag' => true,
];
$this->assertEmpty( $validator->unknown_assoc( $assoc_args ) );
$assoc_args['another'] = true;
$this->assertContains( 'another', $validator->unknown_assoc( $assoc_args ) );
}
public function testMissingAssoc(): void {
$validator = new SynopsisValidator( '--type=<type> [--brand=<brand>] [--flag]' );
$assoc_args = [
'brand' => true,
'flag' => true,
];
list( $errors, $to_unset ) = $validator->validate_assoc( $assoc_args );
$this->assertCount( 1, $errors['fatal'] );
$this->assertCount( 1, $errors['warning'] );
}
public function testAssocWithOptionalValue(): void {
$validator = new SynopsisValidator( '[--network[=<id>]]' );
$assoc_args = [ 'network' => true ];
list( $errors, $to_unset ) = $validator->validate_assoc( $assoc_args );
$this->assertCount( 0, $errors['fatal'] );
$this->assertCount( 0, $errors['warning'] );
}
}