Writing PHPUnit tests just got easier!

We can probably all agree that automated tests are great, but sometimes we put off writing them because the initial first step of getting everything working correctly can be really difficult.  

Good news, Sugar developers!  Writing PHPUnit tests for your customizations just got easier in Sugar Fall '18!  Our engineering team has created a test runner specifically for your custom PHPUnit tests.

If you do not have access to the SugarCRM provided unit test suite, requesting access is easy, all you need to do is send an e-mail to developers@sugarcrm.com with your request. For more information, see this post.

The first thing you'll need to do is add the SugarCRM provided unit test suite to your Sugar directory. Be sure to select the branch in the unit test suite that matches the version of your Sugar instance (needs to be 8.2 or higher). Follow the instructions in the branch's readme to add the tests to your Sugar directory, install the necessary dependencies, and update the file permissions.  

Now you're ready to write your tests!  Add your new PHPUnit test classes to the custom/tests/unit-php directory. You may need to create it yourself if it doesn't already exist.

For example, create a new file named CustomLogicHookTest.php and place it in the custom/tests/unit-php directory.  Paste the following in to the new file:

<?php
    //custom/tests/unit-php/CustomLogicHookTest.php
   
    namespace Sugarcrm\SugarcrmTestsUnit\custom;
   
    use PHPUnit\Framework\TestCase;
   
    /**
     * @coversDefaultClass \CustomLogicHook
     */

    class CustomLogicHookTest extends TestCase
    {
       
        /**
         * @covers ::aFunctionInsideTheCustomLogicHook
         */

        public function testCustomHook()
        {
            // Wow! This is a horrible test.
            // You would never do something like this just to increase your test  
            // coverage, right?
            $this->assertTrue(true);
        }
    }

In a shell, navigate to the Sugar provided unit tests directory by doing something like the following:

cd tests/unit-php

Then you can run the tests by executing

php ../../vendor/bin/phpunit --testsuite custom

or by using gulp (gulp installation instructions are available here)

gulp test:unit:php --suite custom

The above test is extremely simple and doesn't test anything. If you'd like to see an example of a larger test suite that tests Sugar customizations, check out Professor M's School for Gifted Coders.  We've recently updated our PHPUnit test suite so we can use the new test runner.  Check out the pull request associated with this change for the details.

Parents
  • Hello Matt,  I am trying to write unit-php tests for Sugar 12.0.  The goal is to write these using Mocks of everything without the database. 

    In one of our Logic Hooks that I am trying to test, we call the method BeanFactory::getBean('Accounts', $accountId);.  This then invokes AccessControlManager which tries to load data from the database.  Is it possible to either mock completely the BeanFactory to override getBean() OR override the AccessControlManager invocation?

Comment
  • Hello Matt,  I am trying to write unit-php tests for Sugar 12.0.  The goal is to write these using Mocks of everything without the database. 

    In one of our Logic Hooks that I am trying to test, we call the method BeanFactory::getBean('Accounts', $accountId);.  This then invokes AccessControlManager which tries to load data from the database.  Is it possible to either mock completely the BeanFactory to override getBean() OR override the AccessControlManager invocation?

Children
No Data