PrestaShop / prestashop-flashlight

A docker based testing utility for PrestaShop
MIT License
24 stars 10 forks source link

Using Prestashop Flashlight to launch tests #136

Open Maus3rSR opened 2 days ago

Maus3rSR commented 2 days ago

Hello

Thank you for first awesome docker image that let us able to run an instance of Prestashop in seconds and install our modules for development purposes.

I would also like to use this image to launch my Integration test. However how can I achieve this goal as the Prestashop code source zip used in the image is a production build and does not contain any testing?

I like to practice TDD, DI, IoC etc. And I really enjoy developing 90% of my module without having to test each time by launching the browser, going to admin, login, goto thing, etc.

I show you the simple thing I try to achieve:

My docker compose

name: $MODULE
services:
  prestashop:
    image: prestashop/prestashop-flashlight:latest
    container_name: $MODULE-ps
    depends_on:
      mysql:
        condition: service_healthy
    environment:
      MODULE_NAME: $MODULE
      PS_DOMAIN: localhost:8000
      INIT_SCRIPTS_DIR: /tmp/init-scripts
    ports:
      - 8000:80
    volumes:
      - ./scripts:/tmp/init-scripts:ro
      - type: bind
        source: ../modules/$MODULE
        target: /var/www/html/modules/$MODULE

  mysql:
    image: mariadb:lts
    container_name: $MODULE-db
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect"]
      interval: 10s
      timeout: 20s
      retries: 5
    environment:
      - MYSQL_USER=prestashop
      - MYSQL_PASSWORD=prestashop
      - MYSQL_ROOT_PASSWORD=prestashop
      - MYSQL_PORT=3306
      - MYSQL_DATABASE=prestashop

in modules/gtrmanager/composer.json

"gtrmanager:test:unit": "docker exec gtrmanager-ps /usr/bin/phpunit -c modules/gtrmanager/tests/Unit/phpunit.xml",
"gtrmanager:test:integration": "docker exec gtrmanager-ps /usr/bin/phpunit -c modules/gtrmanager/tests/Integration/phpunit.xml"

In modules/gtrmanager/tests/Integration/phpunit.xml and bootstrap.php

<phpunit backupGlobals="false" colors="true" bootstrap="bootstrap.php" cacheDirectory=".phpunit.cache" processIsolation="true">
    <php>
        <env name="KERNEL_CLASS" value="AppKernel" />
        <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
    </php>
    <testsuites>
        <testsuite name="Integration">
            <directory>.</directory>
        </testsuite>
    </testsuites>
    <source>
        <include>
            <directory>../../src</directory>
        </include>
    </source>
</phpunit>
<?php

define('_PS_IN_TEST_', true);
define('_PS_ROOT_DIR_', dirname(__DIR__, 4));
define('_PS_MODULE_DIR_', _PS_ROOT_DIR_ . '/modules/');

require_once _PS_ROOT_DIR_ . '/admin-dev/bootstrap.php';
require_once _PS_MODULE_DIR_ . '/gtrmanager/vendor/autoload.php';

And my test file

<?php

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Response;

/**
 * @testdox Configuration page
 */
class ConfigurationTest extends WebTestCase
{
    protected KernelBrowser $client;
    protected Router $router;

    protected function setUp(): void
    {
        parent::setUp();
        $this->client = self::createClient();
        $this->router = self::$kernel->getContainer()->get('router');
    }

    /**
     * @test
     * @testdox should be displayed
     */
    public function shouldDisplayConfigurationPage(): void
    {
        $url = $this->router->generate('gtrmanager_configuration');
        $this->client->request('GET', $url);
        $response = $this->client->getResponse();

        $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
    }
}

Im getting hard times running this simple code because when running the test Im going into the test context and Prestashop manage to load test config, using test_databasename, etc. However all these things are missing in a production sourcecode.

How can I achieve that?

Maus3rSR commented 2 days ago

Mh, I think you will suggest me to use the Prestashop sourcecode like this example: https://github.com/PrestaShop/prestashop-flashlight/tree/main/examples/develop-prestashop

However I do not understand why flashlight install phpunit, but use Prestashop production codesource.

When I read the doc, if you want to test and develop with Prestashop, you might use docker image or PS flashlight. But how can you test Prestashop without the whole test env set by Prestashop if the image use production code source.

jokesterfr commented 6 hours ago

Flashlight install phpunit, so you can run this in your CI env easily. Here is an example: https://github.com/PrestaShopCorp/ps_eventbus/blob/main/.github/workflows/quality-check.yml

Any question?

Maus3rSR commented 3 hours ago

Im able to run phpunit, that's not my issue.

My issue is to run integration test of my module (you can see above). It will run: