surprisehighway / craft-avatax

Calculate and add sales tax to an order's base tax using Avalara's AvaTax service with Craft Commerce 5.
MIT License
7 stars 11 forks source link

After install event breaking automated testing #37

Closed davist11 closed 3 years ago

davist11 commented 3 years ago

We have automated testing setup on our Craft + Commerce install. Once we added this plugin, the test suite no longer runs completely (it doesn't install other plugins we have installed). I commented this out just to test:

https://github.com/surprisehighway/craft-avatax/blob/67c93a8a9c533e6439aa77e324a8341900c9399c/src/Avatax.php#L98-L106

And the suite runs correctly then. I'm not sure the best solution to fix this. Should this be moved up to the top of the method?

https://github.com/surprisehighway/craft-avatax/blob/67c93a8a9c533e6439aa77e324a8341900c9399c/src/Avatax.php#L307-L309

Or maybe check to see if the env is test?

Craft Pro 3.6.11.1 Avatax 2.1.5

davist11 commented 3 years ago

👋

Any potential update here? Been holding off merging this plugin in. Thanks!

imagehat commented 3 years ago

@davist11 - Admittedly I haven't done a deep dive into Craft's testing, but are there any particular errors you're getting that may help track it down?

I'm hesitant to move up the console request check because I think we'd still want to install the Tax Category and override Fields if you're installing the plugin from the console. That check is just to prevent the CP redirect if running in the console.

Are you defining CRAFT_TESTS_PATH in tests/_bootstrap.php? One thought I had was to check for that constant to see if it's running in test mode and then just return. I'm not sure if there's a better way but that might be an easy one to try.

davist11 commented 3 years ago

There isn't really a great error to send you down a path because I just get a generic:

In Schema.php line 678:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'client_test.<nil>_n  
  avigation_navs' doesn't exist                                                
  The SQL being executed was: SELECT *                                         
  FROM `<nil>_navigation_navs`                                                 
  WHERE `<nil>_navigation_navs`.`dateDeleted` IS NULL                          
  ORDER BY `sortOrder`                                                         

In Command.php line 1299:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'client_test.<nil>_n  
  avigation_navs' doesn't exist   

Something in that onAfterInstall() method is preventing other plugins from being installed, but I'm not sure exactly which part. I can dig in and start doing more debugging if that's helpful to find out exactly where it's halting the installation of other plugins.

Are you defining CRAFT_TESTS_PATH in tests/_bootstrap.php

Yep, we have all these constants set in our bootstrap:

define('CRAFT_TESTS_PATH', __DIR__);
define('CRAFT_STORAGE_PATH', __DIR__ . '/_craft/storage');
define('CRAFT_TEMPLATES_PATH', __DIR__ . '/_craft/templates');
define('CRAFT_CONFIG_PATH', __DIR__ . '/_craft/config');
define('CRAFT_MIGRATIONS_PATH', __DIR__ . '/_craft/migrations');
define('CRAFT_TRANSLATIONS_PATH', __DIR__ . '/_craft/translations');
define('CRAFT_VENDOR_PATH', dirname(__DIR__) . '/vendor');

Maybe a FR for Craft would be to have some sort of boolean for when the test suite is running. Another option would be checking to see if the environment == test?

imagehat commented 3 years ago

I see. So it's breaking the next plugin install and maybe causes an error because the Navigation plugin didn't run it's migrations or something.

I wasn't sure if the environment was alway set to test when testing but if that's the case maybe we try adding something like:

if (Craft::$app->config->env === 'test') {
  return;
}

... at the top of this function to just bail:

https://github.com/surprisehighway/craft-avatax/blob/67c93a8a9c533e6439aa77e324a8341900c9399c/src/Avatax.php#L199-L204

davist11 commented 3 years ago

I think so, you can see in the test setup that is sets a default env value of test

https://github.com/craftcms/cms/blob/develop/src/test/TestSetup.php#L262-L281

And yeah, I think adding a check like that makes sense. Want me to manually add it and test? Would be happy to submit a PR.

imagehat commented 3 years ago

Good deal. Yeah if you can test it that would be awesome. Either a PR and I'll merge it or just let me know if the conditional works and I can get it added in and bump the version.

davist11 commented 3 years ago

Success, test suite passes! https://github.com/surprisehighway/craft-avatax/pull/38

imagehat commented 3 years ago

Thanks @davist11! Merged and version bumped.

davist11 commented 3 years ago

Awesome, thank you!