How to configure the test environment in PyroCMS? (Vagrant/Homestead)
Created 7 years ago by vargvinterHello,
I need help with the test environment configuration. I am using Vagrant/Homestead and I have done so far:
1). I have created 'mysql_testing'
entry in config/database.php
pointing to pyrocms_testing
database,
2). I have created pyrocms_testing
DB on Homestead,
3). I have created entry in phpunit.xml
which connection to use: <env name="DB_CONNECTION" value="mysql_testing"/>
When I run php artisan migrate --database=mysql_testing
it says: "Migration table created successfully. Nothing to migrate."
When I run ./bin/phpunit
I git bunch of errors like this:
53) TemplateTest::testCanRenderStringTemplate
ReflectionException: Class Anomaly\TestModule\TestModule does not exist
I have also tried to run tests for the custom module:
./bin/phpunit --filter VideosModuleTest
<?php
namespace Acme\VideosModule\Test\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
class VideosModuleTest extends \TestCase
{
use RefreshDatabase;
public function testFeature()
{
$this->assertTrue(true);
}
}
I got errors like this:
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pyrocms_testing.default_addons_modules' doesn't exist
It's understandable because I haven't got migrated DB but I don't know how.
Please help.
According to phpunit.xml
fragment:
<php>
<server name="HTTP_HOST" value="http://localhost"/>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="mysql_testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
APP_ENV should be set to testing while running phpunit.
@fryiee please help. 😄
Hi @vargvinter , this is due to the application not being installed on that database. You will either need to run php artisan install
and specify the database to install to, or set it back to your main application database. You do not need to have a separate testing database - all tests are run transactionally, meaning anything persisting to the database will (or should) be wiped when the test case concludes. Any tests that hit the DB and dont transactionally use methods, please make an issue and tag myself and @ryanthompson .
Hi @vargvinter , this is due to the application not being installed on that database. You will either need to run php artisan install
and specify the database to install to, or set it back to your main application database. You do not need to have a separate testing database - all tests are run transactionally, meaning anything persisting to the database will (or should) be wiped when the test case concludes. Any tests that hit the DB and dont transactionally use methods, please make an issue and tag myself and @ryanthompson .
@fryiee - thank for your fast and helpful reply.
I have done following things:
1). Set APP_ENV
in phpunit.xml
to local
,
2). My first test looks like this
<?php namespace Lizard\PortfolioModule\Test\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
class PortfolioModuleTest extends \TestCase
{
use RefreshDatabase;
public function testFeature()
{
$this->assertTrue(true);
}
}
After running
./bin/phpunit --filter PortfolioModuleTest
test was green but it also wiped out my local db after completion. Did I set above things correctly?
@vargvinter The trait RefreshDatabase
will refresh migrations after test. The trait you are looking for is Illuminate\Foundation\Testing\DatabaseTransactions
.
e.g. use DatabaseTransactions
. That will transactionally call any db queries but not wipe your DB afterwards.
@vargvinter so with APP_ENV=testing you get the reflectionexception? What's your whole phpunit.xml look like?
@fryiee - check this out:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="bootstrap/autoload.php"
convertWarningsToExceptions="true"
convertNoticesToExceptions="true"
convertErrorsToExceptions="true"
backupStaticAttributes="false"
processIsolation="false"
stopOnFailure="false"
backupGlobals="false"
syntaxCheck="false"
colors="true">
<testsuites>
<testsuite name="Streams">
<directory suffix="Test.php">./vendor/anomaly/streams-platform/</directory>
</testsuite>
<testsuite name="Application">
<directory suffix="Test.php">./addons/*/*/*/tests</directory>
</testsuite>
<testsuite name="Core">
<directory suffix="Test.php">./core/*/*/tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./vendor/anomaly/streams-platform/src/</directory>
<exclude>
<directory suffix=".php">./app/</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./coverage" charset="UTF-8"
yui="true" highlight="true"/>
</logging>
<php>
<server name="HTTP_HOST" value="http://localhost"/>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
</phpunit>
I also tried XAMPP. Unfortunately this same problem:
ReflectionException: Class Anomaly\TestModule\TestModule does not exists
🙍
Might need to open an issue and link to this thread. Sounds like we have ourselves a bug.
https://github.com/pyrocms/pyrocms/issues/4697