温馨提示×

phpstorm在centos上的自动化测试工具集成

小樊
46
2025-09-17 18:59:18
栏目: 编程语言

Integrating Automation Testing Tools with PhpStorm on CentOS

PhpStorm provides seamless integration with popular PHP automation testing tools like PHPUnit (unit testing), Codeception (unit/functional/acceptance testing), and static analysis tools (PHPStan, Psalm). Below is a structured guide to setting up these integrations on a CentOS environment.

1. Prerequisites

Before integrating testing tools, ensure your CentOS environment has:

  • PHP (≥7.4) installed with required extensions (php-cli, php-mbstring, php-xml, php-pdo, php-mysqlnd):
    sudo yum install php php-cli php-mbstring php-xml php-pdo php-mysqlnd
    
  • Composer (dependency manager for PHP):
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    
  • PhpStorm installed (download from JetBrains website and extract to a directory).

2. PHPUnit Integration

PHPUnit is the most widely used unit testing framework for PHP. Follow these steps to integrate it with PhpStorm:

a. Install PHPUnit

Use Composer to install PHPUnit locally in your project (recommended) or globally:

  • Local installation (preferable for project-specific dependencies):
    composer require --dev phpunit/phpunit
    
  • Global installation (accessible system-wide):
    sudo pecl install phpunit/phpunit
    echo "extension=phpunit.so" | sudo tee /etc/php.d/phpunit.ini
    sudo systemctl restart httpd  # Restart web server (Apache/Nginx)
    

b. Configure PhpStorm

  1. Open PhpStorm and go to File > Settings > Languages & Frameworks > PHP > Testing.
  2. Under “Test Frameworks,” click the + icon and select PHPUnit.
  3. Choose the PHP interpreter (configured in CLI Interpreter settings) and specify the path to the PHPUnit executable:
    • For local installations: vendor/bin/phpunit (relative to your project root).
    • For global installations: /usr/bin/phpunit.
  4. Click Validate to ensure PhpStorm recognizes PHPUnit.

c. Create and Run Tests

  1. Create a test directory (e.g., tests/) in your project root.
  2. Write a test class (e.g., tests/ExampleTest.php):
    use PHPUnit\Framework\TestCase;
    
    class ExampleTest extends TestCase {
        public function testBasicTest() {
            $this->assertTrue(true);
        }
    }
    
  3. Right-click the test file or method in PhpStorm and select Run ‘PHPUnit’ (or use the green play button in the editor). Results appear in the Run window.

3. Codeception Integration

Codeception supports unit, functional, and acceptance testing with a single tool. Here’s how to integrate it:

a. Install Codeception

Install Codeception via Composer (local or global):

  • Local installation:
    composer require --dev codeception/codeception
    
  • Global installation:
    Download codecept.phar from the Codeception website and save it to your project root.

b. Configure PhpStorm

  1. Generate Configuration:
    Run codecept bootstrap in your project root (via PhpStorm’s built-in terminal) to generate a codeception.yml file.
  2. Set Up Test Frameworks:
    • Go to File > Settings > Languages & Frameworks > PHP > Test Frameworks.
    • Click + and select Codeception.
    • Specify the path to codecept.phar (if globally installed) or vendor/bin/codecept (if local).
    • Select the PHP interpreter and click OK.
  3. Configure Directories:
    Mark the tests/ directory as a “Test Sources Root” (right-click the directory → Mark Directory as → Test Sources Root) to enable code completion.

c. Create and Run Tests

  1. Generate a test (e.g., unit test):
    codecept generate:test unit Example
    
    This creates tests/unit/ExampleTest.php.
  2. Write a test (similar to PHPUnit but with Codeception’s syntax):
    class ExampleTest extends \Codeception\Test\Unit {
        public function testBasicTest() {
            $this->assertTrue(true);
        }
    }
    
  3. Run tests via PhpStorm (right-click → Run ‘Codeception’) or command line (codecept run).

4. Static Analysis Tools (PHPStan, Psalm)

Static analysis tools catch potential errors without executing code. PhpStorm supports both PHPStan and Psalm:

a. Install PHPStan/Psalm

Use Composer to install locally in your project:

composer require --dev phpstan/phpstan
composer require --dev vimeo/psalm

b. Configure PhpStorm

  1. Go to File > Settings > Languages & Frameworks > PHP > Code Sniffer.
  2. Click + to add a new tool:
    • For PHPStan: Select PHPStan and specify the path to vendor/bin/phpstan.
    • For Psalm: Select Psalm and specify the path to vendor/bin/psalm.
  3. Click Validate to ensure PhpStorm recognizes the tool.
  4. Enable real-time analysis in Editor > Inspections (enable “PHPStan” or “Psalm” inspections).

Key Tips for Success

  • Use local Composer installations (in the vendor/bin directory) for project-specific dependencies to avoid conflicts.
  • Mark test directories as “Test Sources Root” to improve code completion and navigation.
  • Configure remote interpreters (if using a remote server) in CLI Interpreter settings to sync environments.
  • Leverage PhpStorm’s Run/Debug Configurations to customize test execution (e.g., passing arguments to PHPUnit/Codeception).

By following these steps, you can fully integrate PHPUnit, Codeception, and static analysis tools with PhpStorm on CentOS, enabling efficient automated testing and code quality management.

0