3  * SPDX-License-Identifier: GPL-2.0-only
 
   5  * This file is part of Nominatim. (https://nominatim.org)
 
   7  * Copyright (C) 2022 by the Nominatim developer community.
 
   8  * For a full list of authors see the git log.
 
  13 require_once(CONST_LibDir.'/Shell.php');
 
  15 class ShellTest extends \PHPUnit\Framework\TestCase
 
  17     public function testNew()
 
  19         $this->expectException('ArgumentCountError');
 
  20         $this->expectExceptionMessage('Too few arguments to function');
 
  21         $oCmd = new \Nominatim\Shell();
 
  24         $oCmd = new \Nominatim\Shell('wc', '-l', 'file.txt');
 
  31     public function testaddParams()
 
  33         $oCmd = new \Nominatim\Shell('grep');
 
  34         $oCmd->addParams('-a', 'abc')
 
  40             'no escaping needed, chained'
 
  43         $oCmd = new \Nominatim\Shell('grep');
 
  45         $oCmd->addParams(null);
 
  48         $this->assertEmpty($oCmd->aParams);
 
  49         $this->assertSame('grep', $oCmd->escapedCmd(), 'empty params');
 
  51         $oCmd = new \Nominatim\Shell('echo', '-n', 0);
 
  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');
 
  65             "'/path with space/do.php' -a ' b ' --flag 'two words' 'v=1'",
 
  70         $oCmd = new \Nominatim\Shell('grep');
 
  71         $oCmd->addParams(';', '|more&', '2>&1');
 
  74             "grep ';' '|more&' '2>&1'",
 
  76             'escape shell characters'
 
  80     public function testaddEnvPair()
 
  82         $oCmd = new \Nominatim\Shell('date');
 
  84         $oCmd->addEnvPair('one', 'two words')
 
  85              ->addEnvPair('null', null)
 
  86              ->addEnvPair(null, 'null')
 
  87              ->addEnvPair('empty', '')
 
  88              ->addEnvPair('', 'empty');
 
  91             array('one' => 'two words', 'empty' => ''),
 
  95         $oCmd->addEnvPair('one', 'overwrite');
 
  97             array('one' => 'overwrite', 'empty' => ''),
 
 102     public function testClone()
 
 104         $oCmd = new \Nominatim\Shell('wc', '-l', 'file.txt');
 
 105         $oCmd2 = clone $oCmd;
 
 106         $oCmd->addParams('--flag');
 
 107         $oCmd2->addParams('--flag2');
 
 110             "wc -l 'file.txt' --flag",
 
 115             "wc -l 'file.txt' --flag2",
 
 120     public function testRun()
 
 122         $oCmd = new \Nominatim\Shell('echo');
 
 124         $this->assertSame(0, $oCmd->run());
 
 126         // var_dump($sStdout);