1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHP : Rapid Development Framework (https://cakephp.org) |
4
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
11
|
|
|
* @link https://cakephp.org CakePHP Project |
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
namespace Cake\TestSuite\Stub; |
15
|
|
|
|
16
|
|
|
use Cake\Console\ConsoleInput as ConsoleInputBase; |
17
|
|
|
use Cake\Console\Exception\ConsoleException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Stub class used by the console integration harness. |
21
|
|
|
* |
22
|
|
|
* This class enables input to be stubbed and have exceptions |
23
|
|
|
* raised when no answer is available. |
24
|
|
|
*/ |
25
|
|
|
class ConsoleInput extends ConsoleInputBase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Reply values for ask() and askChoice() |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $replies = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Current message index |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $currentIndex = -1; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param string[] $replies A list of replies for read() |
45
|
|
|
*/ |
46
|
|
|
public function __construct(array $replies) |
47
|
|
|
{ |
48
|
|
|
parent::__construct(); |
49
|
|
|
|
50
|
|
|
$this->replies = $replies; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Read a reply |
55
|
|
|
* |
56
|
|
|
* @return mixed The value of the reply |
57
|
|
|
*/ |
58
|
|
|
public function read() |
59
|
|
|
{ |
60
|
|
|
$this->currentIndex += 1; |
61
|
|
|
|
62
|
|
|
if (!isset($this->replies[$this->currentIndex])) { |
63
|
|
|
$total = count($this->replies); |
64
|
|
|
$replies = implode(', ', $this->replies); |
65
|
|
|
$message = "There are no more input replies available. Only {$total} replies were set, " . |
66
|
|
|
"this is the {$this->currentIndex} read operation. The provided replies are: {$replies}"; |
67
|
|
|
throw new ConsoleException($message); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->replies[$this->currentIndex]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if data is available on stdin |
75
|
|
|
* |
76
|
|
|
* @param int $timeout An optional time to wait for data |
77
|
|
|
* @return bool True for data available, false otherwise |
78
|
|
|
*/ |
79
|
|
|
public function dataAvailable($timeout = 0) |
80
|
|
|
{ |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|