Skip to content

Commit

Permalink
Add PHPUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Feb 2, 2023
1 parent b2b419b commit 8f3c060
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:

- name: PHP syntax checker 5.6
uses: prestashop/github-action-php-lint/5.6@master
with:
folder-to-exclude: "! -path \"./tests/*\""

- name: PHP syntax checker 7.2
uses: prestashop/github-action-php-lint/7.2@master
Expand Down Expand Up @@ -86,3 +88,31 @@ jobs:
env:
_PS_ROOT_DIR_: /tmp/prestashop
run: vendor/bin/phpstan

phpunit:
name: PHPUnit
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Cache vendor folder
uses: actions/cache@v3
with:
path: vendor
key: php-${{ hashFiles('composer.lock') }}

- name: Cache composer folder
uses: actions/cache@v3
with:
path: ~/.composer/cache
key: php-composer-cache

- name: Install dependencies
run: composer install

- name: PHPUnit tests 1.7
run: DOCKER_INTERNAL="1.7" make phpunit

- name: PHPUnit tests with PS 8
run: DOCKER_INTERNAL=8 make phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
.php-cs-fixer.cache
.phpunit.result.cache
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
DOCKER_INTERNAL ?= 8
DOCKER = $(shell docker ps 2> /dev/null)

check-docker:
ifndef DOCKER
$(error "DOCKER is unavailable on your system")
endif

phpunit: check-docker
docker pull prestashop/docker-internal-images:${DOCKER_INTERNAL}
@docker run --rm \
--name phpunit \
-e PS_DOMAIN=localhost \
-e PS_ENABLE_SSL=0 \
-e PS_DEV_MODE=1 \
-e XDEBUG_MODE=coverage \
-e XDEBUG_ENABLED=1 \
-v ${PWD}:/var/www/html/test-lib \
-w /var/www/html/test-lib \
prestashop/docker-internal-images:${DOCKER_INTERNAL} \
sh -c " \
service mariadb start && \
service apache2 start && \
_PS_ROOT_DIR_=/var/www/html/ vendor/bin/phpunit \
"
@echo phpunit passed
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ This example would be called from your module.
$mboStatus = (new Prestashop\ModuleLibMboInstaller\Presenter)->present();

var_dump($mboStatus);
/*
Example output:
array(4) {
["isPresentOnDisk"]=>
bool(false)
["isInstalled"]=>
bool(false)
["isEnabled"]=>
bool(false)
["version"]=>
NULL
}
/*
```

### Trigger download and installation of MBO
Expand Down
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/bootstrap.php"
colors="true"
stopOnFailure="false"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<testsuites>
<testsuite name="Integration">
<directory suffix="Test.php">tests/Integration</directory>
</testsuite>
</testsuites>
<logging/>
</phpunit>
37 changes: 37 additions & 0 deletions tests/Integration/InstallationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use PHPUnit\Framework\TestCase;
use Prestashop\ModuleLibMboInstaller\Installer;
use Prestashop\ModuleLibMboInstaller\Presenter;

class InstallationTest extends TestCase
{
protected function setUp(): void
{
if (version_compare(_PS_VERSION_, '8.0.0', '<')) {
global $kernel;
$moduleManager = $kernel->getContainer()->get('prestashop.module.manager');
$moduleManager->uninstall('ps_mbo', true);
}
}

public function testModuleIsInstalled()
{
$mboStatusBeforeInstall = (new Presenter())->present();

// Expect module to be unknown
$this->assertFalse($mboStatusBeforeInstall['isInstalled']);
$this->assertFalse($mboStatusBeforeInstall['isEnabled']);

// Install it...
$this->assertTrue((new Installer(_PS_VERSION_))->installModule());

// Expect module to be installed now
$mboStatusAfterInstall = (new Presenter())->present();
$this->assertTrue($mboStatusAfterInstall['isPresentOnDisk']);
$this->assertTrue($mboStatusAfterInstall['isInstalled']);
// CheckMe: MBO appears as disabled on PS 1.7
// $this->assertTrue($mboStatusAfterInstall['isEnabled']);
$this->assertIsString($mboStatusAfterInstall['version']);
}
}
24 changes: 24 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

if (!getenv('_PS_ROOT_DIR_')) {
echo "[ERROR] Define _PS_ROOT_DIR_ with the path to PrestaShop folder\n";
exit(1);
}

if (!defined('_PS_ADMIN_DIR_')) {
define('_PS_ADMIN_DIR_', '/admin');
}

if (!defined('_PS_MODE_DEV_')) {
define('_PS_MODE_DEV_', true);
}

$rootDirectory = getenv('_PS_ROOT_DIR_');
require_once $rootDirectory . '/config/config.inc.php';

global $kernel;
if (!$kernel) {
require_once _PS_ROOT_DIR_ . '/app/AppKernel.php';
$kernel = new \AppKernel('dev', true);
$kernel->boot();
}

0 comments on commit 8f3c060

Please sign in to comment.