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: use NumberFormatter;
19:
20: /**
21: * Stub class used by the console integration harness.
22: *
23: * This class enables input to be stubbed and have exceptions
24: * raised when no answer is available.
25: */
26: class ConsoleInput extends ConsoleInputBase
27: {
28: /**
29: * Reply values for ask() and askChoice()
30: *
31: * @var array
32: */
33: protected $replies = [];
34:
35: /**
36: * Current message index
37: *
38: * @var int
39: */
40: protected $currentIndex = -1;
41:
42: /**
43: * Constructor
44: *
45: * @param string[] $replies A list of replies for read()
46: */
47: public function __construct(array $replies)
48: {
49: parent::__construct();
50:
51: $this->replies = $replies;
52: }
53:
54: /**
55: * Read a reply
56: *
57: * @return mixed The value of the reply
58: */
59: public function read()
60: {
61: $this->currentIndex += 1;
62:
63: if (!isset($this->replies[$this->currentIndex])) {
64: $total = count($this->replies);
65: $formatter = new NumberFormatter('en', NumberFormatter::ORDINAL);
66: $nth = $formatter->format($this->currentIndex + 1);
67:
68: $replies = implode(', ', $this->replies);
69: $message = "There are no more input replies available. This is the {$nth} read operation, " .
70: "only {$total} replies were set. The provided replies are: {$replies}";
71: throw new ConsoleException($message);
72: }
73:
74: return $this->replies[$this->currentIndex];
75: }
76:
77: /**
78: * Check if data is available on stdin
79: *
80: * @param int $timeout An optional time to wait for data
81: * @return bool True for data available, false otherwise
82: */
83: public function dataAvailable($timeout = 0)
84: {
85: return true;
86: }
87: }
88: