n98 / magerun2-test-framework

n98-magerun2 命令的测试框架

2.0.2 2022-02-24 11:07 UTC

This package is auto-updated.

Last update: 2024-08-24 16:29:03 UTC


README

此包包含一些PHPUnit测试用例。

先决条件

测试运行器需要一个纯净的Magento 2安装。要运行测试套件,需要设置环境变量 N98_MAGERUN2_TEST_MAGENTO_ROOT,其值为安装的根路径。

所有单元测试都是针对此安装运行的。请勿使用任何生产环境!

测试模块命令

示例测试用例

<?php

namespace Acme\Example\Command;

use N98\Magento\Command\PHPUnit\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

class FooCommandTest extends TestCase
{
    /**
     * @test
     */
    public function testOutput()
    {
        /**
         * Load module config for unit test. In this case the relative
         * path from current test case.
         */
        $this->loadConfigFile(__DIR__ . '/../../n98-magerun2.yaml');

        /**
         * Test if command could be found
         */
        $command = $this->getApplication()->find('foo');

        /**
         * Call command
         */
        $commandTester = new CommandTester($command);
        $commandTester->execute(
            [
            ]
        );
    }
}

测试 dev:console 命令

代码生成命令

创建一个包含代码生成器内容的参考文件。在以下测试用例中,文件位于 "DIR /_files/ExampleSomething.php"。

TestCase 类中的 mockWriterFileWriteFileAssertion 方法模拟模块编写者,并将输出与参考文件进行比较。

<?php

namespace Acme\Example\Command\CodeGenerator;

use N98\Magento\Command\Developer\Console\PHPUnit\TestCase;

class MakeSomethingCommandTest extends TestCase
{
    /**
     * @test
     */
    public function testGenerator()
    {
        $command = new MakeSomethingCommand();

        $commandTester = $this->createCommandTester($command);
        $command->setCurrentModuleName('Acme_Example');

        $writerMock = $this->mockWriterFileWriteFileAssertion(
            __DIR__ . '/_files/ExampleSomething.php'
        );

        $command->setCurrentModuleDirectoryWriter($writerMock);
        $commandTester->execute([
            /* pass your parameters */
        ]);
    }
}

运行PHPUnit测试

N98_MAGERUN2_TEST_MAGENTO_ROOT=/path/to/magento/root ./vendor/bin/phpunit

示例PHPUnit配置

示例模块结构

.
├── README.md
├── composer.json
├── n98-magerun2.yaml
├── phpunit.xml.dist
├── src
│   └── Command
│       └── ExampleCommand.php
└── tests
    └── Command
        └── ExampleCommandTest.php

phpunit.xml.dist

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         bootstrap="vendor/autoload.php"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
  <coverage includeUncoveredFiles="true">
    <include>
      <directory>src</directory>
    </include>
  </coverage>
  <testsuites>
    <testsuite name="n98-magerun2 Project Transfer Tools Commands">
      <directory>tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

composer.json

{
  "name": "acme/example",
  "description": "Some commands",  
  "require-dev": {
    "n98/magerun2": "^4",
    "n98/magerun2-test-framework": "^2",
    "phpunit/phpunit": "^9"
  },
  "autoload-dev": {
    "psr-4": {
      "Acme\\Example\\": "tests"
    }
  },
  "autoload": {
    "psr-4": {
      "Acme\\Example\\": "src"
    }
  }
}