Hello,
I'm new to using multiple projects and would like to start writing tests for my wordpress plugin.

I created a new php console project, added the default config file, and now want to start using the classes defined in the plugin project.

How do I reference the wordpress plugin project in the test project and expose it's contents to the ide?

    RichardAubin Thank you for the question Richard; we're getting it together.

      Hi Richard,

      To reference your WordPress plugin project in your test project and expose its classes to the IDE, you can set up your solution like this:

      1. Create Two Projects

      • MainProject: This is your WordPress plugin project containing the classes you want to test.

      • TestProject: A PHP console project where you write your tests.

      • Add a project reference from TestProject to MainProject so IntelliSense can recognize the plugin's classes.

        Note: If classes from MainProject are not appearing in TestProject, it might be due to a bug in how project references are handled in our new project system. We are working on resolving this.

      2. Configure PHPUnit in TestProject

      • First, you need to install PHPUnit. Right-click on Dependencies in the Solution Explorer and select Install New Composer Packages.

      • Search for phpunit and install it as a development dependency.

      • Add the following to your phpunit.xml file:

          <phpunit
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
              backupGlobals="true"
              backupStaticAttributes="false"
              bootstrap="bootstrap.php">
            <testsuites>
              <testsuite name="My Test Suite">
                <directory>.</directory>
              </testsuite>
            </testsuites>
          </phpunit>

      3. Create a bootstrap.php file in TestProject

      Create a bootstrap.php file with the following contents:

      <?php
      
      set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../MainProject');
      spl_autoload_register();

      4. Write a Test in TestProject

      Create a file like ExampleTest.php with the following contents:

      <?php
      
      class ExampleTest extends \PHPUnit\Framework\TestCase
      {
          public function testAddition()
          {
              $calculator = new Calculator();
              $this->assertEquals(5, $calculator->add(2, 3));
          }
      }

      5. Define the Class in MainProject

      In MainProject, create the Calculator class:

      <?php
      
      class Calculator
      {
          public function add($a, $b)
          {
              return $a + $b;
          }
      }

      This setup ensures that your TestProject has access to the classes in MainProject and that you can easily write tests for your WordPress plugin. Let me know if you encounter any issues!

        Write a Reply...