CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • ConsoleIntegrationTestCase
  • IntegrationTestCase
  • LegacyCommandRunner
  • LegacyShellDispatcher
  • TestCase
  • TestEmailTransport
  • TestSuite

Traits

  • ConsoleIntegrationTestTrait
  • EmailAssertTrait
  • EmailTrait
  • IntegrationTestTrait
  • StringCompareTrait
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
 11:  * @since         3.7.0
 12:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 13:  */
 14: namespace Cake\TestSuite;
 15: 
 16: use Cake\Console\Command;
 17: use Cake\Console\CommandRunner;
 18: use Cake\Console\ConsoleIo;
 19: use Cake\Console\Exception\StopException;
 20: use Cake\Core\Configure;
 21: use Cake\TestSuite\Constraint\Console\ContentsContain;
 22: use Cake\TestSuite\Constraint\Console\ContentsContainRow;
 23: use Cake\TestSuite\Constraint\Console\ContentsEmpty;
 24: use Cake\TestSuite\Constraint\Console\ContentsNotContain;
 25: use Cake\TestSuite\Constraint\Console\ContentsRegExp;
 26: use Cake\TestSuite\Constraint\Console\ExitCode;
 27: use Cake\TestSuite\Stub\ConsoleInput;
 28: use Cake\TestSuite\Stub\ConsoleOutput;
 29: 
 30: /**
 31:  * A test case class intended to make integration tests of cake console commands
 32:  * easier.
 33:  */
 34: trait ConsoleIntegrationTestTrait
 35: {
 36:     /**
 37:      * Whether or not to use the CommandRunner
 38:      *
 39:      * @var bool
 40:      */
 41:     protected $_useCommandRunner = false;
 42: 
 43:     /**
 44:      * Last exit code
 45:      *
 46:      * @var int|null
 47:      */
 48:     protected $_exitCode;
 49: 
 50:     /**
 51:      * Console output stub
 52:      *
 53:      * @var \Cake\TestSuite\Stub\ConsoleOutput|\PHPUnit_Framework_MockObject_MockObject|null
 54:      */
 55:     protected $_out;
 56: 
 57:     /**
 58:      * Console error output stub
 59:      *
 60:      * @var \Cake\TestSuite\Stub\ConsoleOutput|\PHPUnit_Framework_MockObject_MockObject|null
 61:      */
 62:     protected $_err;
 63: 
 64:     /**
 65:      * Console input mock
 66:      *
 67:      * @var \Cake\Console\ConsoleInput|\PHPUnit_Framework_MockObject_MockObject|null
 68:      */
 69:     protected $_in;
 70: 
 71:     /**
 72:      * Runs cli integration test
 73:      *
 74:      * @param string $command Command to run
 75:      * @param array $input Input values to pass to an interactive shell
 76:      * @return void
 77:      */
 78:     public function exec($command, array $input = [])
 79:     {
 80:         $runner = $this->makeRunner();
 81: 
 82:         $this->_out = new ConsoleOutput();
 83:         $this->_err = new ConsoleOutput();
 84:         $this->_in = new ConsoleInput($input);
 85: 
 86:         $args = $this->commandStringToArgs("cake $command");
 87:         $io = new ConsoleIo($this->_out, $this->_err, $this->_in);
 88: 
 89:         try {
 90:             $this->_exitCode = $runner->run($args, $io);
 91:         } catch (StopException $exception) {
 92:             $this->_exitCode = $exception->getCode();
 93:         }
 94:     }
 95: 
 96:     /**
 97:      * Cleans state to get ready for the next test
 98:      *
 99:      * @after
100:      * @return void
101:      */
102:     public function cleanupConsoleTrait()
103:     {
104:         $this->_exitCode = null;
105:         $this->_out = null;
106:         $this->_err = null;
107:         $this->_in = null;
108:         $this->_useCommandRunner = false;
109:     }
110: 
111:     /**
112:      * Set this test case to use the CommandRunner rather than the legacy
113:      * ShellDispatcher
114:      *
115:      * @return void
116:      */
117:     public function useCommandRunner()
118:     {
119:         $this->_useCommandRunner = true;
120:     }
121: 
122:     /**
123:      * Asserts shell exited with the expected code
124:      *
125:      * @param int $expected Expected exit code
126:      * @param string $message Failure message
127:      * @return void
128:      */
129:     public function assertExitCode($expected, $message = '')
130:     {
131:         $this->assertThat($expected, new ExitCode($this->_exitCode), $message);
132:     }
133: 
134:     /**
135:      * Asserts shell exited with the Command::CODE_SUCCESS
136:      *
137:      * @param string $message Failure message
138:      * @return void
139:      */
140:     public function assertExitSuccess($message = '')
141:     {
142:         $this->assertThat(Command::CODE_SUCCESS, new ExitCode($this->_exitCode), $message);
143:     }
144: 
145:     /**
146:      * Asserts shell exited with Command::CODE_ERROR
147:      *
148:      * @param string $message Failure message
149:      * @return void
150:      */
151:     public function assertExitError($message = '')
152:     {
153:         $this->assertThat(Command::CODE_ERROR, new ExitCode($this->_exitCode), $message);
154:     }
155: 
156:     /**
157:      * Asserts that `stdout` is empty
158:      *
159:      * @param string $message The message to output when the assertion fails.
160:      * @return void
161:      */
162:     public function assertOutputEmpty($message = '')
163:     {
164:         $this->assertThat(null, new ContentsEmpty($this->_out->messages(), 'output'), $message);
165:     }
166: 
167:     /**
168:      * Asserts `stdout` contains expected output
169:      *
170:      * @param string $expected Expected output
171:      * @param string $message Failure message
172:      * @return void
173:      */
174:     public function assertOutputContains($expected, $message = '')
175:     {
176:         $this->assertThat($expected, new ContentsContain($this->_out->messages(), 'output'), $message);
177:     }
178: 
179:     /**
180:      * Asserts `stdout` does not contain expected output
181:      *
182:      * @param string $expected Expected output
183:      * @param string $message Failure message
184:      * @return void
185:      */
186:     public function assertOutputNotContains($expected, $message = '')
187:     {
188:         $this->assertThat($expected, new ContentsNotContain($this->_out->messages(), 'output'), $message);
189:     }
190: 
191:     /**
192:      * Asserts `stdout` contains expected regexp
193:      *
194:      * @param string $pattern Expected pattern
195:      * @param string $message Failure message
196:      * @return void
197:      */
198:     public function assertOutputRegExp($pattern, $message = '')
199:     {
200:         $this->assertThat($pattern, new ContentsRegExp($this->_out->messages(), 'output'), $message);
201:     }
202: 
203:     /**
204:      * Check that a row of cells exists in the output.
205:      *
206:      * @param array $row Row of cells to ensure exist in the output.
207:      * @param string $message Failure message.
208:      * @return void
209:      */
210:     protected function assertOutputContainsRow(array $row, $message = '')
211:     {
212:         $this->assertThat($row, new ContentsContainRow($this->_out->messages(), 'output'), $message);
213:     }
214: 
215:     /**
216:      * Asserts `stderr` contains expected output
217:      *
218:      * @param string $expected Expected output
219:      * @param string $message Failure message
220:      * @return void
221:      */
222:     public function assertErrorContains($expected, $message = '')
223:     {
224:         $this->assertThat($expected, new ContentsContain($this->_err->messages(), 'error output'), $message);
225:     }
226: 
227:     /**
228:      * Asserts `stderr` contains expected regexp
229:      *
230:      * @param string $pattern Expected pattern
231:      * @param string $message Failure message
232:      * @return void
233:      */
234:     public function assertErrorRegExp($pattern, $message = '')
235:     {
236:         $this->assertThat($pattern, new ContentsRegExp($this->_err->messages(), 'error output'), $message);
237:     }
238: 
239:     /**
240:      * Asserts that `stderr` is empty
241:      *
242:      * @param string $message The message to output when the assertion fails.
243:      * @return void
244:      */
245:     public function assertErrorEmpty($message = '')
246:     {
247:         $this->assertThat(null, new ContentsEmpty($this->_err->messages(), 'error output'), $message);
248:     }
249: 
250:     /**
251:      * Builds the appropriate command dispatcher
252:      *
253:      * @return CommandRunner|LegacyCommandRunner
254:      */
255:     protected function makeRunner()
256:     {
257:         if ($this->_useCommandRunner) {
258:             $applicationClassName = Configure::read('App.namespace') . '\Application';
259: 
260:             return new CommandRunner(new $applicationClassName(CONFIG));
261:         }
262: 
263:         return new LegacyCommandRunner();
264:     }
265: 
266:     /**
267:      * Creates an $argv array from a command string
268:      *
269:      * @param string $command Command string
270:      * @return array
271:      */
272:     protected function commandStringToArgs($command)
273:     {
274:         $charCount = strlen($command);
275:         $argv = [];
276:         $arg = '';
277:         $inDQuote = false;
278:         $inSQuote = false;
279:         for ($i = 0; $i < $charCount; $i++) {
280:             $char = substr($command, $i, 1);
281: 
282:             // end of argument
283:             if ($char === ' ' && !$inDQuote && !$inSQuote) {
284:                 if (strlen($arg)) {
285:                     $argv[] = $arg;
286:                 }
287:                 $arg = '';
288:                 continue;
289:             }
290: 
291:             // exiting single quote
292:             if ($inSQuote && $char === "'") {
293:                 $inSQuote = false;
294:                 continue;
295:             }
296: 
297:             // exiting double quote
298:             if ($inDQuote && $char === '"') {
299:                 $inDQuote = false;
300:                 continue;
301:             }
302: 
303:             // entering double quote
304:             if ($char === '"' && !$inSQuote) {
305:                 $inDQuote = true;
306:                 continue;
307:             }
308: 
309:             // entering single quote
310:             if ($char === "'" && !$inDQuote) {
311:                 $inSQuote = true;
312:                 continue;
313:             }
314: 
315:             $arg .= $char;
316:         }
317:         $argv[] = $arg;
318: 
319:         return $argv;
320:     }
321: }
322: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs