Symfony2 phpunit run tests on different environment - symfony

I'm testing email sending in my symfony2 application, I have custom environment that is properly set with email settings and it's able to actually sent emails. I've created environment test_emails and I want to run my phpunit tests with it.
phpunit doesn't seem to have this functionality, so my only option is probably create custom bootstrap file, but I can't find anywhere how to do it.

The doc about Environments says:
The test environment is used when writing functional tests and is not accessible in the browser directly via a front controller. In other words, unlike the other environments, there is no app_test.php front controller file.
So when testing emails you're supposed to use functional test which is well described in How to Test that an Email is Sent in a Functional Test.
If you want to write a custom bootstrap with some simple logic see: How to Customize the Bootstrap Process before Running Tests
Maybe you can actually customize what environment the test harness is using by creating a custom test case that boots the kernel just like here How can I test a service in symfony2? and then passing an array of options to bootKernel() method. See:
https://github.com/symfony/framework-bundle/blob/master/Test/KernelTestCase.php#L141
https://github.com/symfony/framework-bundle/blob/master/Test/KernelTestCase.php#L168

If your using Symfonys WebtestCase you can set the environment as option:
$client = static::createClient(array('environment' => 'test-emails'));

I've ended up creating command that sends all emails I wanted and I can easily switch between environments.
Example:
php app/console app:emails:test-emails --env=dev

Use different phpunit.xml configuration files for each environment.
.
├── phpunit_test_emails.xml
└── phpunit_other_env.xml
Run the tests with specific configuration:
$ phpunit -c phpunit_test_emails.xml
You can set different PHP configurations in the XML configuration file.
You create a custom bootstrap via the bootstrap attribute where additional initialisations can be done if needed.
<phpunit bootstrap="test/bootstrap.php">
<!-- ... -->
<php>
<includePath>.</includePath>
<ini name="foo" value="bar"/>
<const name="foo" value="bar"/>
<var name="foo" value="bar"/>
<env name="foo" value="bar"/>
<post name="foo" value="bar"/>
<get name="foo" value="bar"/>
<cookie name="foo" value="bar"/>
<server name="foo" value="bar"/>
<files name="foo" value="bar"/>
<request name="foo" value="bar"/>
</php>
</phpunit>
See the PHPUnit Manual for more details.

Related

Symfony 4 phpunit : KERNEL_CLASS env variable error

I'm trying to learn how to make phpunit by testing the User class made by "make:user" but i'm facing this problem when extending "KernelTestCase" and run the test :
LogicException: You must set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel in phpunit.xml / phpunit.xml.dist or override the "App\tests\Entity
UsersTest::createKernel()" or "App\tests\Entity\UsersTest::getKernelClass()" method.
Here is my test, i'm trying to test an Entity :
I'm trying to fix it since 2 days with my friend Google but I didn't found any solution. Can you help me ? Thank you guys !
I just got the same problem.
The solution is to update your ./phpunit.xml.dist file to set the KERNEL_CLASS environment variable :
[...]
<php>
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="7.5" />
<server name="KERNEL_CLASS" value="App\Kernel" /> <--- SET KERNEL_CLASS AT THIS LINE
</php>
[...]
In my case the tests/bootstrap.php file was not in sync with the one of the phpunit-bridge recipe. The recipe was not executed because I updated Symfony 3.4 (without flex) to Symfony 4.4.
After installing the recipe with composer recipes:install symfony/phpunit-bridge --force -v the following files were overridden:
.env.test
phpunit.xml.dist
tests/bootstrap.php
Now the .env.test gets loaded and the KERNEL_CLASS env variable, which is defined there, is respected correctly.

How to Run Tests Against an Existing - not testing - Drupal Database

I need to run tests again an existing database in Drupal 8.
I already tried to replace default sqllite connection info with the right mysql database's info in phpunit.xml
Here's my phpunit.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
bootstrap="test/bootstrap.php"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
strictConfigSchema="false">
<testsuites>
<testsuite name="drupal-composer-project tests">
<directory>./test/</directory>
</testsuite>
</testsuites>
<php>
<!-- Set error reporting to E_ALL. -->
<ini name="error_reporting" value="32767"/>
<!-- Do not limit the amount of memory tests take to run. -->
<ini name="memory_limit" value="-1"/>
<env name="SIMPLETEST_BASE_URL" value="http://localhost"/>
<env name="SIMPLETEST_DB" value="mysql://user:password#mysql/databasename"/>
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value=""/>
</php>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<listeners>
<listener class="\Drupal\Tests\Listeners\DrupalListener">
</listener>
<!-- The Symfony deprecation listener has to come after the Drupal listener -->
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
</listener>
</listeners>
<logging>
<log type="coverage-html" target="../../coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-clover" target="../../clover.xml"/>
<log type="coverage-crap4j" target="../../crap4j.xml"/>
</logging>
</phpunit>
When I log database information, I see that a test prefix is added, and this prefix will create tables inside my existing database. How can I make the tests run directly on my own database and tables ?
If you want to run tests on the existing database, you need to use Drupal Test Traits. Here's some introduction text for this testing framework.
But, as #DirkScholten said, you should avoid run tests on the existing database. An exception is running User Acceptance Tests, they need data from the production database in order to simulate an end-user experience. In this case, ensure that you run tests in isolate environment, so as not tests send some mail to the real user or do some changes via API on real servers.
If you just need some custom settings or field on you Drupal's test instance, then a better option is to create your own installation profile and use them in your tests. Here's instructions.
The third option is to use config_installer and doing tests on custom site configuration. From Drupal 8.6 you don't need this module, it's in the Drupal's core.

How do I target an already existing application pool with webdeploy?

I am trying to make sure that my app gets deployed to a specific application pool that already exists when using Web Deploy. The application pool should be configurable by the user using the GUI when installing app through IIS Manager or by changing the value in the .setparameters.xml file when installing via the commandline from a web package. Inserting the following parameter entry into my parameters.xml doesn't do the trick.
<parameter name="Application Pool" description="Application Pool for this site" tags="iisApp" defaultValue="ASP.NET v4.0">
<parameterEntry kind="providerPath" scope="IisApp" match="applicationPool" />
</parameter>
Is there a straightforward way to accomplish this? If not, how would I go about getting this done?
Here's what I did to set the application pool via command line or SetParameters.xml after lots of reading on SO and elsewhere:
Add a Parameters.xml file to the project.
<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<parameter name="AppPool" defaultValue="ASP.NET 4.0">
<parameterEntry kind="DeploymentObjectAttribute" scope="application" match="applicationPool/#applicationPool" />
</parameter>
</parameters>
Sources:
How to specify MSDeploy parameters from MSbuild
http://vishaljoshi.blogspot.com/2010/07/web-deploy-parameterization-in-action.html
Add two parameters to msbuild when creating the package:
/P:IncludeIisSettings=true
/P:IncludeAppPool=true
Source:
https://stackoverflow.com/a/13678143/448876
Set via SetParameters.xml:
<setParameter name="AppPool" value="Some AppPoolName"/>
OR
Using command line parameter (msdeploy or *.deploy.cmd):
"-setParam:'AppPool'='Some AppPoolName'"

Looking for the exact config.xml location for PHPUnit testing in Magento

I'm currently trying to get PHPUnit working with together with Magento. On some places in the web people recommend the extension from EcomDev so I tried it out.
I build an example setup like written in this tutorial, I just replaced EcomDev_Example_... with Test_JustTest_...
However, it doesn't work as it should and I guess I put the data in the wrong config.xml. At the moment I've written down the module name in /app/code/local/Test/JustTest/etc/config.xml and in the config.xml of the extension to just try something different. Well, I can run PHPUnit but it always tells me that there's no test to run.
I spent a lot of time on Google not finding a more detailed example
Im faced with the same problem, but now I get it worked.
I created app/code/local/Namespace/Module/etc/config.xml with:
<?xml version="1.0"?>
<config>
<phpunit>
<suite>
<modules>
<Namespace_Module/>
</modules>
</suite>
</phpunit>
<modules>
<namespace_module>
<version>0.1</version>
</namespace_module>
</modules>
<global>
<models>
<eav>
<rewrite>
<entity_increment_numeric>Namespace_Module_Model_Entity_Increment_Numeric</entity_increment_numeric>
</rewrite>
</eav>
</models>
</global>
</config>
In fact the name of these are not Namespace_Module, I just replaced to show you. Its a module that overrides numeric model from magento, but it has no big differences.
See that my test is under
app/code/local/Namespace/Module/Test/Model/Entity/Increment/Numeric.php
And looks like:
<?php
class Namespace_Module_Test_Model_Entity_Increment_Numeric extends EcomDev_PHPUnit_Test_Case
{
/**
* Test Next Id Never Returns zero
*
* #test
*/
public function testGetNextIdNeverReturnsZero(){
$this->assertTrue(true);
}
}
After all set, you should run your tests using the following command on console/terminal:
phpunit UnitTests.php
Remember that you should enable your module by adding Namespace_Module.xml at app/etc/modules as any other model. My mistake was that i named the folder as Tests instead of Test in my model. I dont think you are doing the same thing...
Anyway, I hope it helps. For more information, the manual could help you a lot.

How do I enable Directory Browsing for an virtual web directory using wix?

I want to enable "Directory Browsing" for the for the following virtual web directory using WIX.
<iis:WebVirtualDir Id="LogsVirDir" Alias="Logs" Directory="ESGLOGFILES" />
How do I accomplish this using WIX?
Wouldn't a simpler solution be to use the web.config system.webserver property like :
<directoryBrowse enabled="true"/>
Based on my research Wix currently does not have any capability to enable Directory Browsing using the standard set of actions. The one way I have found to do this is using a combination of Wix Custom Actions and IIS's Appcmd.exe. Note this command will create a web.config file if one does not exist.
<CustomAction Id="EnableDirectoryBrowsing"
Execute="deferred"
ExeCommand='[WindowsFolder]system32\inetsrv\APPCMD.EXE set config "ESG Website/logs" /section:directoryBrowse /enabled:true'
Directory="TARGETDIR"
Return="check"
Impersonate="no"/>
<InstallExecuteSequence>
<Custom Action="EnableDirectoryBrowsing" Before="InstallFinalize">Not Installed</Custom>
</InstallExecuteSequence>
Im using wix v3.8
try adding ConfigurableDirectory in your Feature
ex: <Feature Id='TestName' Title='Test Web' ConfigurableDirectory='INSTALLDIR' Level='1'>
Use the following code
<Control Id="Browse" Type="PushButton" X="304" Y="210" Width="56" Height="17" Text="!(loc.CustomizeDlgBrowse)">
<Publish Event="SelectionBrowse" Value="BrowseDlg">1</Publish>
</Control>
Take the value of this in the variable you want and use it.

Resources