Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests #23

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .composer-require-checker.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"GALETTE_PGSQL_MIN",
"GALETTE_DISPLAY_VERSION",
"GALETTE_PHP_MIN",
"GALETTE_TESTS",

"// Galette db constants (not detected as they are dynamically declared)",
"HOST_DB",
Expand Down
136 changes: 136 additions & 0 deletions .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ on:
- 'feature/*'
- 'hotfix/*'
- 'release/*'
tags:
- '*'
pull_request:
# Enable manual run
workflow_dispatch:

concurrency:
group: "${{ github.workflow }}-${{ github.ref }}"
cancel-in-progress: true

jobs:
lint:
Expand Down Expand Up @@ -85,3 +93,131 @@ jobs:
run: |
cd galette-core/galette/plugins/plugin-objectslend
../../vendor/bin/docheader --docheader=../../../.docheader check lib ./*.php

unit-tests:
runs-on: ubuntu-latest

strategy:
matrix:
include:
#always tests higher stable php version with lower db version
#enable coverage on higher stable php version with higher postrgesql version
#lower php version
- { php-version: "8.1", db-image: "mysql:5.7", coverage: none, always: false }
- { php-version: "8.1", db-image: "mysql:8.1", coverage: none, always: false }
- { php-version: "8.1", db-image: "mariadb:10.4", coverage: none, always: false }
- { php-version: "8.1", db-image: "mariadb:11", coverage: none, always: false }
- { php-version: "8.1", db-image: "postgres:11", coverage: none, always: false }
- { php-version: "8.1", db-image: "postgres:16", coverage: none, always: false }
#higher stable php version
- { php-version: "8.3", db-image: "mysql:5.7", coverage: none, always: true }
- { php-version: "8.3", db-image: "mysql:8.1", coverage: none, always: false }
- { php-version: "8.3", db-image: "mariadb:10.4", coverage: none, always: true }
- { php-version: "8.3", db-image: "mariadb:11", coverage: none, always: false }
- { php-version: "8.3", db-image: "postgres:11", coverage: none, always: true }
- { php-version: "8.3", db-image: "postgres:16", coverage: none, always: true }
fail-fast: false

env:
skip: ${{ matrix.always == false && (github.event_name == 'pull_request' || github.repository != 'galette/galette-objectslend' || !(github.event_name == 'workflow_dispatch' || github.ref == 'refs/heads/master' || github.ref == 'refs/heads/develop' || startsWith(github.ref, 'refs/tags'))) }}
DB: ${{ matrix.db-image }}

services:
# Label used to access the service container
db:
# Docker Hub image
image: ${{ matrix.db-image }}
# Provide env variables for both mysql and pgsql
env:
POSTGRES_USER: galette_tests
POSTGRES_PASSWORD: g@l3tte
POSTGRES_DB: galette_tests
MYSQL_USER: galette_tests
MYSQL_PASSWORD: g@l3tte
MYSQL_ROOT_PASSWORD: g@l3tte
MYSQL_DATABASE: galette_tests
# Open network ports for both mysql and pgsql
ports:
- 3306:3306
- 5432:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd="bash -c 'if [[ -n $(command -v pg_isready) ]]; then pg_isready; else if [[ -n $(command -v mysqladmin) ]]; then mysqladmin ping; else mariadb-admin ping; fi fi'"
--health-interval=10s
--health-timeout=5s
--health-retries=10

name: PHP ${{ matrix.php-version }} ${{ matrix.db-image }} ${{ (matrix.always == false && (github.event_name == 'pull_request' || github.repository != 'galette/galette' || !(github.event_name == 'workflow_dispatch' || github.ref == 'refs/heads/master' || github.ref == 'refs/heads/develop' || startsWith(github.ref, 'refs/tags')))) && ' (skipped)' || matrix.coverage == 'xdebug' && ' (with coverage)' || ''}}

steps:
- name: PHP
if: env.skip != 'true'
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: composer, pecl
coverage: ${{ matrix.coverage }}
extensions: apcu
ini-values: apc.enable_cli=1

- name: "Show versions"
if: env.skip != 'true'
run: |
php --version
composer --version
echo "node $(node --version)"
echo "npm $(npm --version)"
docker exec ${{ job.services.db.id }} bash -c "if [[ -n \$(command -v psql) ]]; then psql --version; else if [[ -n \$(command -v mysql) ]]; then mysql --version; else mariadb --version; fi fi"

- name: Checkout Galette core
if: env.skip != 'true'
uses: actions/checkout@v4
with:
repository: galette/galette
path: galette-core
fetch-depth: 1
ref: develop

- name: Checkout plugin
uses: actions/checkout@v4
with:
path: galette-core/galette/plugins/plugin-objectslend

- name: "Restore dependencies cache"
if: env.skip != 'true'
uses: actions/cache@v4
with:
path: |
~/.composer/cache/
~/.npm/_cacache/
key: "${{ runner.os }}-galette-${{ matrix.php-version }}-${{ hashFiles('galette/composer.lock', 'package-lock.json') }}"
restore-keys: |
${{ runner.os }}-galette-${{ matrix.php-version }}-

- name: Install dependencies
if: env.skip != 'true'
run: |
cd galette-core
bin/install_deps

- name: Init for PostgreSQL
env:
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
run: |
PGPASSWORD=g@l3tte psql -d galette_tests -a -f galette-core/galette/install/scripts/pgsql.sql -U galette_tests -h localhost
PGPASSWORD=g@l3tte psql -d galette_tests -a -f galette-core/galette/plugins/plugin-objectslend/scripts/pgsql.sql -U galette_tests -h localhost
if: env.skip != 'true' && startsWith(matrix.db-image, 'postgres')

- name: Init for MariaDB
run: |
mysql -e 'create database IF NOT EXISTS galette_tests;' -u galette_tests --password=g@l3tte -h 127.0.0.1 -P 3306
mysql -e 'use galette_tests; source galette-core/galette/install/scripts/mysql.sql;' -u galette_tests --password=g@l3tte -h 127.0.0.1 -P 3306
mysql -e 'use galette_tests; source galette-core/galette/plugins/plugin-objectslend/scripts/mysql.sql;' -u galette_tests --password=g@l3tte -h 127.0.0.1 -P 3306
if: env.skip != 'true' && (startsWith(matrix.db-image, 'mysql') || startsWith(matrix.db-image, 'mariadb'))

- name: Unit tests
if: env.skip != 'true'
run: |
cd galette-core/galette/plugins/plugin-objectslend
../../vendor/bin/phpunit --test-suffix=.php --bootstrap tests/TestsBootstrap.php --no-coverage --process-isolation tests/GaletteObjectsLend/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
quick_gen_project_*
dist/
tempcache/
tests/coverage/
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function list(Request $request, Response $response, string $option = null
$cat_filters = new CategoriesList();
$cat_filters->active_filter = Categories::ACTIVE_CATEGORIES; //retrieve only active categories
$cat_filters->not_empty = true; //retrieve only categories with objects
$cat_filters->setObjectsFilter($filters); //search for categories corresponding to filtered objects
$categories = new Categories($this->zdb, $this->login, $this->plugins, $cat_filters);
$categories_list = $categories->getCategoriesList(true, null, false);

Expand Down Expand Up @@ -403,7 +404,7 @@ public function doEdit(Request $request, Response $response, int $id = null, str
$object->dimension = $post['dimension'];
if ($post['weight'] != '') {
//FIXME: better format handler
$object->weight = (int)str_replace(' ', '', str_replace(',', '.', $post['weight']));
$object->weight = (float)str_replace(' ', '', str_replace(',', '.', $post['weight']));
}
$object->is_active = ($post['is_active'] ?? false) == true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function doEdit(Request $request, Response $response, int $id = null, str
$status->in_stock = isset($post['in_stock']);
$status->is_active = isset($post['is_active']);
$days = trim($post['rent_day_number']);
$status->rent_day_number = strlen($days) > 0 ? intval($days) : null;
$status->rent_day_number = strlen($days) > 0 ? (int)$days : null;
if (!$status->store()) {
$error_detected[] = _T("An error occurred while storing the status.", "objectslend");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/GaletteObjectsLend/Controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function storePreferences(Request $request, Response $response): Response

$error_detected = [];
$success_detected = [];
if ($lendsprefs->store($post, $error_detected)) {
if ($lendsprefs->store($post)) {
$this->flash->addMessage(
'success_detected',
_T("Preferences have been successfully stored!", "objectslend")
Expand Down
19 changes: 9 additions & 10 deletions lib/GaletteObjectsLend/Entity/LendCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LendCategory
'is_active' => 'boolean'
);
private int $category_id;
private string $name = '';
private ?string $name = null;
private bool $is_active = true;
private int $objects_nb = 0;
private float $objects_price_sum = 0.0;
Expand Down Expand Up @@ -121,19 +121,18 @@ public function __construct(Db $zdb, Plugins $plugins, int|ArrayObject $args = n
*/
private function loadFromRS(ArrayObject $r): void
{
$this->category_id = $r->category_id;
$this->category_id = (int)$r->category_id;
$this->name = $r->name;
$this->is_active = $r->is_active == '1' ? true : false;
$this->is_active = $r->is_active == '1';

if (property_exists($r, 'objects_count')) {
$this->objects_nb = $r->objects_count;
$this->objects_nb = (int)$r->objects_count;
}

if (property_exists($r, 'objects_price_sum')) {
$this->objects_price_sum = $r->objects_price_sum ?? 0.0;
if (property_exists($r, 'objects_price_sum') && $r->objects_price_sum !== null) {
$this->objects_price_sum = (float)$r->objects_price_sum;
}


if ($this->deps['picture'] === true) {
$this->picture = new CategoryPicture($this->plugins, (int)$this->category_id);
}
Expand Down Expand Up @@ -300,11 +299,11 @@ public function isActive(): bool
/**
* Get picture
*
* @return CategoryPicture
* @return ?CategoryPicture
*/
public function getPicture(): CategoryPicture
public function getPicture(): ?CategoryPicture
{
return $this->picture;
return $this->picture ?? null;
}

/**
Expand Down
Loading