How to configure the test environment in PyroCMS? (Vagrant/Homestead)
Created 6 years ago by vargvinter

Hello,

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.

ryanthompson  —  6 years ago

@fryiee will know more about this as he is the testing guru. But I do know the testing suite addons won't load without setting APP_ENV=testing in your .env file.

vargvinter  —  6 years ago

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. 😄

fryiee  —  6 years ago

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  —  6 years ago

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 .

vargvinter  —  6 years ago

@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?

fryiee  —  6 years ago

@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  —  6 years ago

@fryiee - yeah, now it is working 😄

Last question, I promise 😄

I have set APP_ENV=local in phpunit.xml. I think it should be APP_ENV=testing (because I am testing) but after running test I got:

ReflectionException: Class Anomaly\TestModule\TestModule does not exists

Why?

fryiee  —  6 years ago

@vargvinter so with APP_ENV=testing you get the reflectionexception? What's your whole phpunit.xml look like?

vargvinter  —  6 years ago

@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>
vargvinter  —  6 years ago

@fryiee any chance?

vargvinter  —  6 years ago

I also tried XAMPP. Unfortunately this same problem:

ReflectionException: Class Anomaly\TestModule\TestModule does not exists

🙍

ryanthompson  —  6 years ago

Might need to open an issue and link to this thread. Sounds like we have ourselves a bug.