Bake\Shell\Task\TestTask::generateConstructor PHP Метод

generateConstructor() публичный Метод

Generate a constructor code snippet for the type and class name
public generateConstructor ( string $type, string $fullClassName ) : array
$type string The Type of object you are generating tests for eg. controller
$fullClassName string The full classname of the class the test is being generated for.
Результат array Constructor snippets for the thing you are building.
    public function generateConstructor($type, $fullClassName)
    {
        list(, $className) = namespaceSplit($fullClassName);
        $type = strtolower($type);
        $pre = $construct = $post = '';
        if ($type === 'table') {
            $className = str_replace('Table', '', $className);
            $pre = "\$config = TableRegistry::exists('{$className}') ? [] : ['className' => '{$fullClassName}'];";
            $construct = "TableRegistry::get('{$className}', \$config);";
        }
        if ($type === 'behavior' || $type === 'entity' || $type === 'form') {
            $construct = "new {$className}();";
        }
        if ($type === 'helper') {
            $pre = "\$view = new View();";
            $construct = "new {$className}(\$view);";
        }
        if ($type === 'component') {
            $pre = "\$registry = new ComponentRegistry();";
            $construct = "new {$className}(\$registry);";
        }
        if ($type === 'shell') {
            $pre = "\$this->io = \$this->getMockBuilder('Cake\\Console\\ConsoleIo')->getMock();";
            $construct = "new {$className}(\$this->io);";
        }
        if ($type === 'task') {
            $pre = "\$this->io = \$this->getMockBuilder('Cake\\Console\\ConsoleIo')->getMock();\n";
            $construct = "\$this->getMockBuilder('{$fullClassName}')\n";
            $construct .= "            ->setConstructorArgs([\$this->io])\n";
            $construct .= "            ->getMock();";
        }
        if ($type === 'cell') {
            $pre = "\$this->request = \$this->getMockBuilder('Cake\\Network\\Request')->getMock();\n";
            $pre .= "        \$this->response = \$this->getMockBuilder('Cake\\Network\\Response')->getMock();";
            $construct = "new {$className}(\$this->request, \$this->response);";
        }
        if ($type === 'shell_helper') {
            $pre = "\$this->stub = new ConsoleOutput();\n";
            $pre .= "        \$this->io = new ConsoleIo(\$this->stub);";
            $construct = "new {$className}(\$this->io);";
        }
        return [$pre, $construct, $post];
    }

Usage Example

Пример #1
0
 /**
  * test Constructor generation ensure that constructClasses is called for controllers
  *
  * @return void
  */
 public function testGenerateConstructor()
 {
     $result = $this->Task->generateConstructor('controller', 'PostsController');
     $expected = ['', '', ''];
     $this->assertEquals($expected, $result);
     $result = $this->Task->generateConstructor('table', 'App\\Model\\Table\\PostsTable');
     $expected = ["\$config = TableRegistry::exists('Posts') ? [] : ['className' => 'App\\Model\\Table\\PostsTable'];", "TableRegistry::get('Posts', \$config);", ''];
     $this->assertEquals($expected, $result);
     $result = $this->Task->generateConstructor('helper', 'FormHelper');
     $expected = ["\$view = new View();", "new FormHelper(\$view);", ''];
     $this->assertEquals($expected, $result);
     $result = $this->Task->generateConstructor('entity', 'TestBake\\Model\\Entity\\Article');
     $expected = ["", "new Article();", ''];
     $this->assertEquals($expected, $result);
     $result = $this->Task->generateConstructor('shell_helper', 'TestBake\\Shell\\Helper\\ExampleHelper');
     $expected = ["\$this->stub = new ConsoleOutput();\n        \$this->io = new ConsoleIo(\$this->stub);", "new ExampleHelper(\$this->io);", ''];
     $this->assertEquals($expected, $result);
     $result = $this->Task->generateConstructor('form', 'TestBake\\Form\\ExampleForm');
     $expected = ['', "new ExampleForm();", ''];
     $this->assertEquals($expected, $result);
 }