]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/ShellTest.php
new PHP Nominatim\Shell class to wrap shell escaping
[nominatim.git] / test / php / Nominatim / ShellTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/Shell.php');
6
7 class ShellTest extends \PHPUnit\Framework\TestCase
8 {
9     public function testNew()
10     {
11         $this->expectException('ArgumentCountError');
12         $this->expectExceptionMessage('Too few arguments to function');
13         $oCmd = new \Nominatim\Shell();
14
15
16         $oCmd = new \Nominatim\Shell('wc', '-l', 'file.txt');
17         $this->assertSame(
18             "wc -l 'file.txt'",
19             $oCmd->escapedCmd()
20         );
21     }
22
23     public function testaddParams()
24     {
25         $oCmd = new \Nominatim\Shell('grep');
26         $oCmd->addParams('-a', 'abc')
27                ->addParams(10);
28
29         $this->assertSame(
30             'grep -a abc 10',
31             $oCmd->escapedCmd(),
32             'no escaping needed, chained'
33         );
34
35         $oCmd = new \Nominatim\Shell('grep');
36         $oCmd->addParams();
37         $oCmd->addParams(null);
38         $oCmd->addParams('');
39
40         $this->assertEmpty($oCmd->aParams);
41         $this->assertSame('grep', $oCmd->escapedCmd(), 'empty params');
42
43         $oCmd = new \Nominatim\Shell('echo', '-n', 0);
44         $this->assertSame(
45             'echo -n 0',
46             $oCmd->escapedCmd(),
47             'zero param'
48         );
49
50         $oCmd = new \Nominatim\Shell('/path with space/do.php');
51         $oCmd->addParams('-a', ' b ');
52         $oCmd->addParams('--flag');
53         $oCmd->addParams('two words');
54         $oCmd->addParams('v=1');
55
56         $this->assertSame(
57             "'/path with space/do.php' -a ' b ' --flag 'two words' 'v=1'",
58             $oCmd->escapedCmd(),
59             'escape whitespace'
60         );
61
62         $oCmd = new \Nominatim\Shell('grep');
63         $oCmd->addParams(';', '|more&', '2>&1');
64
65         $this->assertSame(
66             "grep ';' '|more&' '2>&1'",
67             $oCmd->escapedCmd(),
68             'escape shell characters'
69         );
70     }
71
72     public function testaddEnvPair()
73     {
74         $oCmd = new \Nominatim\Shell('date');
75
76         $oCmd->addEnvPair('one', 'two words')
77              ->addEnvPair('null', null)
78              ->addEnvPair(null, 'null')
79              ->addEnvPair('empty', '')
80              ->addEnvPair('', 'empty');
81
82         $this->assertEquals(
83             array('one' => 'two words', 'empty' => ''),
84             $oCmd->aEnv
85         );
86
87         $oCmd->addEnvPair('one', 'overwrite');
88         $this->assertEquals(
89             array('one' => 'overwrite', 'empty' => ''),
90             $oCmd->aEnv
91         );
92     }
93
94     public function testClone()
95     {
96         $oCmd = new \Nominatim\Shell('wc', '-l', 'file.txt');
97         $oCmd2 = clone $oCmd;
98         $oCmd->addParams('--flag');
99         $oCmd2->addParams('--flag2');
100
101         $this->assertSame(
102             "wc -l 'file.txt' --flag",
103             $oCmd->escapedCmd()
104         );
105
106         $this->assertSame(
107             "wc -l 'file.txt' --flag2",
108             $oCmd2->escapedCmd()
109         );
110     }
111
112     public function testRun()
113     {
114         $oCmd = new \Nominatim\Shell('echo');
115
116         $this->assertSame(0, $oCmd->run());
117
118         // var_dump($sStdout);
119     }
120 }