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