CakePHP Shell Shell Tests

I want to write unit tests for the CakePHP shell that I create, but they are not mentioned in the testing documentation or baked

--------------------------------------------------------------- Bake Tests Path: /home/brad/sites/farmlogs/app/Test/ --------------------------------------------------------------- --------------------------------------------------------------- Select an object type: --------------------------------------------------------------- 1. Model 2. Controller 3. Component 4. Behavior 5. Helper Enter the type of object to bake a test for or (q)uit (1/2/3/4/5/q) 

Since CakePHP doesn't seem to have a default shell testing setup, what should be the structure of my main shell tests?

+4
source share
2 answers

Judging by the examples in Mark Story AssetCompress and CakeDC Migrations , just map the directory structure for other tests:

 Test/ Case/ Console/ Command/ Task/ MyShellTaskTest.php MyShellTest.php 

Your tests can simply extend the CakeTestCase object, like any other general test:

 class MyShellTest extends CakeTestCase { } 

If necessary, you can redefine the base shell, just like with the Controller test:

 class TestMyShell extends MyShell { } 

Nothing special, just stick to conventions.

+6
source

I think the application /lib/lib/Cake/Console/Command/TestShellTest.php could be a link.

1, create a file in the application /Test/Case/Console/Command/yourfile.php, specify your classes using App::uses('your class', 'relative path').
eg:

 App::uses('yourShellClass', 'Console/Command'); App::uses('ShellDispatcher', 'Console'); App::uses('Shell', 'Console'); 

2, write a layout for class B from your wrapper class A, whose use function returns data in class A. For example:

  class TestYourShellClass extends YourShellClass { public function F1 ($parms){ //if you need use database here, you can //$this->yourModel = ClassRegistry::init('yourModel'); return $this->_F1($parms); } } 

3, write a class A test class, you need to run it at startup. eg:

  class YourShellClassTest extends CakeTestCase { public function setUp() { parent::setUp(); $out = $this->getMock('ConsoleOutput', [], [], '', false); $in = $this->getMock('ConsoleInput', [], [], '', false); $this->Shell = $this->getMock( // this is your class B, which mocks class A. 'TestYourShellClass', ['in', 'out', 'hr', 'help', 'error', 'err', '_stop', 'initialize', '_run', 'clear'], [$out, $out, $in] ); $this->Shell->OptionParser = $this->getMock('ConsoleOptionParser', [], [null, false]); } /** * tear down method. * * @return void */ public function tearDown() { parent::tearDown(); unset($this->Dispatch, $this->Shell); } } 

4, the test function may be like this.

  /** * test _F1. * * @return void */ public function testF1() { $this->Shell->startup(); $return = $this->Shell->F1(); $expectedSellerCount = 14; $this->assertSame($expectedSellerCount, $return); } 

and then you can view the result at http: //yourdomain/test.php

0
source

Source: https://habr.com/ru/post/1434886/


All Articles