From 8f3c0602f26af04821c5ffec828269772b55336c Mon Sep 17 00:00:00 2001 From: Thomas Nabord Date: Thu, 2 Feb 2023 17:26:02 +0000 Subject: [PATCH] Add PHPUnit --- .github/workflows/php.yml | 30 +++++++++++++++++++++ .gitignore | 1 + Makefile | 26 ++++++++++++++++++ README.md | 13 +++++++++ phpunit.xml | 18 +++++++++++++ tests/Integration/InstallationTest.php | 37 ++++++++++++++++++++++++++ tests/bootstrap.php | 24 +++++++++++++++++ 7 files changed, 149 insertions(+) create mode 100644 Makefile create mode 100644 phpunit.xml create mode 100644 tests/Integration/InstallationTest.php create mode 100644 tests/bootstrap.php diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 0d4ea11..bc66824 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -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 @@ -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 diff --git a/.gitignore b/.gitignore index 69bd16e..d42db56 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ .php-cs-fixer.cache +.phpunit.result.cache diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c3c79b3 --- /dev/null +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index acc1b7a..8157caf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..f6c3b38 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + tests/Integration + + + + diff --git a/tests/Integration/InstallationTest.php b/tests/Integration/InstallationTest.php new file mode 100644 index 0000000..578beec --- /dev/null +++ b/tests/Integration/InstallationTest.php @@ -0,0 +1,37 @@ +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']); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..4f32082 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,24 @@ +boot(); +}