From 481541a42ab32cb44853dc2aef46281cf4593072 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Sun, 19 May 2024 13:18:49 +0200 Subject: [PATCH] Various fixes, add tests, drop dead code closes #1831 --- .composer-require-checker.config.json | 1 + .github/workflows/ci-linux.yml | 136 +++++++++ .gitignore | 1 + .../Controllers/Crud/ObjectsController.php | 3 +- .../Controllers/Crud/StatusController.php | 2 +- .../Controllers/MainController.php | 2 +- .../Entity/LendCategory.php | 19 +- lib/GaletteObjectsLend/Entity/LendObject.php | 146 ++-------- lib/GaletteObjectsLend/Entity/LendRent.php | 22 +- lib/GaletteObjectsLend/Entity/LendStatus.php | 2 +- lib/GaletteObjectsLend/Entity/Picture.php | 2 +- lib/GaletteObjectsLend/Entity/Preferences.php | 63 ++-- .../Filters/CategoriesList.php | 28 +- .../Filters/ObjectsList.php | 38 +-- lib/GaletteObjectsLend/Filters/StatusList.php | 28 +- .../Repository/Categories.php | 36 ++- lib/GaletteObjectsLend/Repository/Objects.php | 29 +- lib/GaletteObjectsLend/Repository/Status.php | 8 +- templates/default/categories_list.html.twig | 2 +- .../Entity/tests/units/LendCategory.php | 116 ++++++++ .../Entity/tests/units/LendObject.php | 272 ++++++++++++++++++ .../Entity/tests/units/LendRent.php | 250 ++++++++++++++++ .../Entity/tests/units/LendStatus.php | 118 ++++++++ .../Entity/tests/units/Preferences.php | 90 ++++++ .../Filters/tests/units/CategoriesList.php | 135 +++++++++ .../Filters/tests/units/ObjectsList.php | 143 +++++++++ .../Filters/tests/units/StatusList.php | 141 +++++++++ .../Repository/tests/units/Categories.php | 133 +++++++++ .../Repository/tests/units/Objects.php | 241 ++++++++++++++++ .../Repository/tests/units/Status.php | 159 ++++++++++ tests/TestsBootstrap.php | 32 +++ 31 files changed, 2113 insertions(+), 285 deletions(-) create mode 100644 tests/GaletteObjectsLend/Entity/tests/units/LendCategory.php create mode 100644 tests/GaletteObjectsLend/Entity/tests/units/LendObject.php create mode 100644 tests/GaletteObjectsLend/Entity/tests/units/LendRent.php create mode 100644 tests/GaletteObjectsLend/Entity/tests/units/LendStatus.php create mode 100644 tests/GaletteObjectsLend/Entity/tests/units/Preferences.php create mode 100644 tests/GaletteObjectsLend/Filters/tests/units/CategoriesList.php create mode 100644 tests/GaletteObjectsLend/Filters/tests/units/ObjectsList.php create mode 100644 tests/GaletteObjectsLend/Filters/tests/units/StatusList.php create mode 100644 tests/GaletteObjectsLend/Repository/tests/units/Categories.php create mode 100644 tests/GaletteObjectsLend/Repository/tests/units/Objects.php create mode 100644 tests/GaletteObjectsLend/Repository/tests/units/Status.php create mode 100644 tests/TestsBootstrap.php diff --git a/.composer-require-checker.config.json b/.composer-require-checker.config.json index c4190d9..8c79953 100644 --- a/.composer-require-checker.config.json +++ b/.composer-require-checker.config.json @@ -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", diff --git a/.github/workflows/ci-linux.yml b/.github/workflows/ci-linux.yml index edacaa8..d6e3b3f 100644 --- a/.github/workflows/ci-linux.yml +++ b/.github/workflows/ci-linux.yml @@ -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: @@ -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/ diff --git a/.gitignore b/.gitignore index 5cc068d..55721b9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ quick_gen_project_* dist/ tempcache/ +tests/coverage/ diff --git a/lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php b/lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php index 039a094..bf75732 100644 --- a/lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php +++ b/lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php @@ -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); @@ -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; diff --git a/lib/GaletteObjectsLend/Controllers/Crud/StatusController.php b/lib/GaletteObjectsLend/Controllers/Crud/StatusController.php index 4f5617b..9019c38 100644 --- a/lib/GaletteObjectsLend/Controllers/Crud/StatusController.php +++ b/lib/GaletteObjectsLend/Controllers/Crud/StatusController.php @@ -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"); } diff --git a/lib/GaletteObjectsLend/Controllers/MainController.php b/lib/GaletteObjectsLend/Controllers/MainController.php index 46870c0..b41abf7 100644 --- a/lib/GaletteObjectsLend/Controllers/MainController.php +++ b/lib/GaletteObjectsLend/Controllers/MainController.php @@ -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") diff --git a/lib/GaletteObjectsLend/Entity/LendCategory.php b/lib/GaletteObjectsLend/Entity/LendCategory.php index 7e09bf3..7bd6987 100644 --- a/lib/GaletteObjectsLend/Entity/LendCategory.php +++ b/lib/GaletteObjectsLend/Entity/LendCategory.php @@ -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; @@ -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); } @@ -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; } /** diff --git a/lib/GaletteObjectsLend/Entity/LendObject.php b/lib/GaletteObjectsLend/Entity/LendObject.php index f1291e2..0d0fb27 100644 --- a/lib/GaletteObjectsLend/Entity/LendObject.php +++ b/lib/GaletteObjectsLend/Entity/LendObject.php @@ -227,16 +227,16 @@ public function __construct(Db $zdb, Plugins $plugins, int|ArrayObject $args = n */ private function loadFromRS(ArrayObject $r): void { - $this->object_id = $r->object_id; + $this->object_id = (int)$r->object_id; $this->name = $r->name; $this->description = $r->description; $this->serial_number = $r->serial_number; - $this->price = is_numeric($r->price) ? floatval($r->price) : 0.0; - $this->rent_price = is_numeric($r->rent_price) ? floatval($r->rent_price) : 0.0; + $this->price = is_numeric($r->price) ? (float)$r->price : 0.0; + $this->rent_price = is_numeric($r->rent_price) ? (float)$r->rent_price : 0.0; $this->price_per_day = $r->price_per_day == '1'; $this->dimension = $r->dimension; - $this->weight = is_numeric($r->weight) ? floatval($r->weight) : 0.0; - $this->is_active = $r->is_active == '1' ? true : false; + $this->weight = is_numeric($r->weight) ? (float)$r->weight : 0.0; + $this->is_active = $r->is_active == '1'; if (property_exists($r, 'cat_active') && ($r->cat_active == 1 || $r->cat_active === null)) { $this->cat_active = true; } else { @@ -245,14 +245,18 @@ private function loadFromRS(ArrayObject $r): void if (property_exists($r, 'cat_name') && $r->cat_name) { $this->cat_name = $r->cat_name; } - $this->category_id = $r->category_id; - $this->nb_available = $r->nb_available; - $this->rent_id = $r->rent_id; + if ($r->category_id != null) { + $this->category_id = (int)$r->category_id; + } + $this->nb_available = (int)$r->nb_available; + if ($r->rent_id != null) { + $this->rent_id = (int)$r->rent_id; + } //load last rent infos (status, member, and so on - if ($this->rent_id) { + if (isset($this->rent_id)) { if (property_exists($r, 'status_id')) { - $this->status_id = $r->status_id; + $this->status_id = (int)$r->status_id; } if (property_exists($r, 'status_text')) { @@ -272,22 +276,16 @@ private function loadFromRS(ArrayObject $r): void } if (property_exists($r, Adherent::PK)) { - $this->id_adh = $r->{Adherent::PK}; + $this->id_adh = (int)$r->{Adherent::PK}; } if (property_exists($r, 'in_stock')) { - $this->in_stock = $r->in_stock; + $this->in_stock = (bool)$r->in_stock; } } - $this->category_id = $r->category_id; - if ($this->object_id && $this->deps['rents'] === true) { - $only_last = false; - if ($this->deps['rents'] === false && $this->deps['last_rent'] === true) { - $only_last = true; - } - $this->rents = LendRent::getRentsForObjectId($this->object_id, $only_last); + $this->rents = LendRent::getRentsForObjectId($this->object_id); } if ($this->deps['picture'] === true) { @@ -367,97 +365,6 @@ public function store(): bool } } - /** - * Get object rent status and rent user information. - * - * @param LendObject $object Object instance to be modified - * - * @return void - */ - public static function getStatusForObject(LendObject $object): void - { - global $zdb; - - // Statut - $select_rent = $zdb->select(LEND_PREFIX . LendRent::TABLE) - ->join( - PREFIX_DB . LEND_PREFIX . LendStatus::TABLE, - PREFIX_DB . LEND_PREFIX . LendRent::TABLE . '.status_id = ' . - PREFIX_DB . LEND_PREFIX . LendStatus::TABLE . '.status_id' - ) - ->join( - PREFIX_DB . Adherent::TABLE, - PREFIX_DB . Adherent::TABLE . '.id_adh = ' . PREFIX_DB . LEND_PREFIX . LendRent::TABLE . '.adherent_id', - '*', - 'left' - ) - ->where(array('object_id' => $object->object_id)) - ->limit(1) - ->offset(0) - ->order('date_begin desc'); - - $results = $zdb->execute($select_rent); - if ($results->count() == 1) { - $rent = $results->current(); - $object->date_begin = $rent->date_begin; - $object->date_forecast = $rent->date_forecast; - $object->date_end = $rent->date_end; - $object->status_text = $rent->status_text; - $object->comments = $rent->comments; - $object->in_stock = $rent->in_stock == '1' ? true : false; - $object->nom_adh = $rent->nom_adh; - $object->prenom_adh = $rent->prenom_adh; - $object->email_adh = $rent->email_adh; - $object->id_adh = $rent->id_adh; - } else { - $object->in_stock = true; - } - } - - /** - * Get requested objects - * - * @param array $ids Objects to retrieve IDs - * - * @return LendObject[] - */ - public static function getMoreObjectsByIds(array $ids): array - { - global $zdb, $plugins; - - $myids = array(); - foreach ($ids as $id) { - if (is_numeric($id)) { - $myids[] = $id; - } - } - - try { - $select = $zdb->select(LEND_PREFIX . self::TABLE) - ->where(array(self::PK => $myids)); - - $results = array(); - - $rows = $zdb->execute($select); - foreach ($rows as $r) { - $o = new self($zdb, $plugins, $r); - - self::getStatusForObject($o); - - $results[] = $o; - } - - return $results; - } catch (\Exception $e) { - Analog::log( - 'Something went wrong :\'( | ' . $e->getMessage() . "\n" . - $e->getTraceAsString(), - Analog::ERROR - ); - throw $e; - } - } - /** * Global getter method * @@ -476,8 +383,6 @@ public function __get(string $name): mixed return number_format($this->$name, 2, ',', ' '); case 'value_rent_price': return $this->rent_price; - case 'weight_bulk': - return $this->weight; case 'weight': return number_format($this->weight, 3, ',', ' '); default: @@ -501,6 +406,8 @@ public function __set(string $name, mixed $value): void case 'category_id': if ($value == '') { $value = null; + } else { + $value = (int)$value; } //no break for value to be set in default default: @@ -527,7 +434,7 @@ public function getCurrency(): string */ public function getCurrentRent(): ?LendRent { - if (is_array($this->rents) && count($this->rents) > 0) { + if (isset($this->rents) && is_array($this->rents) && count($this->rents) > 0) { return $this->rents[0]; } return null; @@ -585,10 +492,8 @@ private function getHighlighted(ObjectsList $filters, string $field): string return $this->$field; } - $untokenized = trim($filters->filter_str ?? '', '%'); - mb_internal_encoding('UTF-8'); return preg_replace( - '/(' . $untokenized . ')/iu', + '/(' . trim($filters->filter_str ?? '', '%') . ')/iu', '$1', $this->$field ); @@ -652,8 +557,13 @@ public function delete(): bool try { $this->zdb->connection->beginTransaction(); //remove rents + $update = $this->zdb->update(LEND_PREFIX . self::TABLE) + ->set([LendRent::PK => null]) + ->where(array(self::PK => $this->object_id)); + $this->zdb->execute($update); $delete = $this->zdb->delete(LEND_PREFIX . LendRent::TABLE) ->where(array(self::PK => $this->object_id)); + $this->zdb->execute($delete); $delete = $this->zdb->delete(LEND_PREFIX . self::TABLE) ->where(array(self::PK => $this->object_id)); $this->zdb->execute($delete); @@ -813,7 +723,7 @@ public function getIdAdh(): ?int */ public function getRentId(): ?int { - return $this->rent_id; + return $this->rent_id ?? null; } /** @@ -857,7 +767,7 @@ protected function getDateField(string $name): string /** * Generic isset function * - * @param $name Property name + * @param string $name Property name * * @return bool */ diff --git a/lib/GaletteObjectsLend/Entity/LendRent.php b/lib/GaletteObjectsLend/Entity/LendRent.php index 5256939..a4d84c9 100644 --- a/lib/GaletteObjectsLend/Entity/LendRent.php +++ b/lib/GaletteObjectsLend/Entity/LendRent.php @@ -67,7 +67,7 @@ class LendRent ); private int $rent_id; private int $object_id; - private string $date_begin; + private ?string $date_begin; private ?string $date_forecast; private ?string $date_end; private ?int $status_id; @@ -123,13 +123,15 @@ public function __construct(int|ArrayObject $args = null) */ private function loadFromRS(ArrayObject $r): void { - $this->rent_id = $r->rent_id; - $this->object_id = $r->object_id; + $this->rent_id = (int)$r->rent_id; + $this->object_id = (int)$r->object_id; $this->date_begin = $r->date_begin; $this->date_forecast = $r->date_forecast; $this->date_end = $r->date_end; - $this->status_id = $r->status_id; - $this->adherent_id = $r->adherent_id; + $this->status_id = (int)$r->status_id; + if ($r->adherent_id !== null) { + $this->adherent_id = (int)$r->adherent_id; + } $this->comments = $r->comments; } @@ -236,8 +238,8 @@ public static function getRentsForObjectId(int $object_id, bool $only_last = fal foreach ($rows as $r) { $rt = new LendRent($r); $rt->status_text = $r->status_text; - $rt->status_id = $r->status_id; - $rt->in_stock = $r->in_stock == '1' ? true : false; + $rt->status_id = (int)$r->status_id; + $rt->in_stock = $r->in_stock == '1'; $rt->prenom_adh = $r->prenom_adh; $rt->nom_adh = $r->nom_adh; $rt->pseudo_adh = $r->pseudo_adh; @@ -339,19 +341,19 @@ public function __get(string $name): mixed switch ($name) { case 'date_begin': case 'date_end': - if ($this->$name != '') { + if (($this->$name ?? '') != '') { $dt = new \DateTime($this->$name); return $dt->format(_T('Y-m-d H:i', 'objectslend')); } return ''; case 'date_forecast': - if ($this->$name != '') { + if (($this->$name ?? '') != '') { $dt = new \DateTime($this->$name); return $dt->format(_T('Y-m-d')); } return ''; default: - return $this->$name; + return $this->$name ?? null; } } diff --git a/lib/GaletteObjectsLend/Entity/LendStatus.php b/lib/GaletteObjectsLend/Entity/LendStatus.php index 4f85165..cc05a9d 100644 --- a/lib/GaletteObjectsLend/Entity/LendStatus.php +++ b/lib/GaletteObjectsLend/Entity/LendStatus.php @@ -99,7 +99,7 @@ public function __construct(Db $zdb, int|ArrayObject $args = null) */ private function loadFromRS(ArrayObject $r): void { - $this->status_id = $r->status_id; + $this->status_id = (int)$r->status_id; $this->status_text = $r->status_text; $this->in_stock = $r->in_stock == '1'; $this->is_active = $r->is_active == '1'; diff --git a/lib/GaletteObjectsLend/Entity/Picture.php b/lib/GaletteObjectsLend/Entity/Picture.php index 6c8302e..7dbd0f1 100644 --- a/lib/GaletteObjectsLend/Entity/Picture.php +++ b/lib/GaletteObjectsLend/Entity/Picture.php @@ -90,7 +90,7 @@ public function __construct(Plugins $plugins, mixed $objectid = null) */ protected function getDefaultPicture(): void { - $this->file_path = realpath( + $this->file_path = (string)realpath( $this->plugins->getTemplatesPathFromName('Galette Objects Lend') . '/../../webroot/images/1f5bc.png' ); diff --git a/lib/GaletteObjectsLend/Entity/Preferences.php b/lib/GaletteObjectsLend/Entity/Preferences.php index dc846e1..b577058 100644 --- a/lib/GaletteObjectsLend/Entity/Preferences.php +++ b/lib/GaletteObjectsLend/Entity/Preferences.php @@ -57,100 +57,100 @@ class Preferences private Db $zdb; /** @var array */ - private array $prefs; + private array $prefs = []; /** - * Paramètre : voir la liste des catégories en en-têtes de la liste des objets + * Show categories at the top of the objects list * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_CATEGORY = 'VIEW_CATEGORY'; /** - * Paramètre : voir la colonne "no de série" + * Show serial number column * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_SERIAL = 'VIEW_SERIAL'; /** - * Paramètre : voir la colonne "photo/minitature" + * Show thumbnail column * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_THUMBNAIL = 'VIEW_THUMBNAIL'; /** - * Paramètre : voir la colonne "description" + * Show description column * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_DESCRIPTION = 'VIEW_DESCRIPTION'; /** - * Paramètre : voir la colonne "prix" + * Show price column * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_PRICE = 'VIEW_PRICE'; /** - * Paramètre : voir la colonne "dimensions" + * Show dimensions column * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_DIMENSION = 'VIEW_DIMENSION'; /** - * Paramètre : voir la colonne "poids" + * Show weight column * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_WEIGHT = 'VIEW_WEIGHT'; /** - * Paramètre : voir la colonne "prix de location" + * Show rent price column * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_LEND_PRICE = 'VIEW_LEND_PRICE'; /** - * Parametre : voir la colonne "retour prevu le" + * Show previsional return date column * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_DATE_FORECAST = 'VIEW_DATE_FORECAST'; /** - * Parametre : voir la somme des prix sur la liste des objects + * Show the sum of prices on the list of objects * Valeur : 0 = false / 1 = true */ public const PARAM_VIEW_LIST_PRICE_SUM = 'VIEW_LIST_PRICE_SUM'; /** - * Paramètre : largeur max d'une miniature (appliquée aux objets/catégories) + * Maximum width of a thumbnail (applied to objects/categories) * Valeur : largeur en pixels */ public const PARAM_THUMB_MAX_WIDTH = 'THUMB_MAX_WIDTH'; /** - * Paramètre : hauteur max d'une miniature (appliquée aux objets/catégories) + * Maximum height of a thumbnail (applied to objects/categories) * Valeur : largeur en pixels */ public const PARAM_THUMB_MAX_HEIGHT = 'THUMB_MAX_HEIGHT'; /** - * Paramètre : Générer automatiquement une contribution lors de la location d'un objet + * Generate automatically a contribution when an object is rented * Valeur : 0 = false / 1 = true */ public const PARAM_AUTO_GENERATE_CONTRIBUTION = 'AUTO_GENERATE_CONTRIBUTION'; /** - * Paramètre : Id du type de contribution si auto-génération d'une contribution + * Contribution ID to generate when an object is rented * Valeur : ID du type de contribution */ public const PARAM_GENERATED_CONTRIBUTION_TYPE_ID = 'GENERATED_CONTRIBUTION_TYPE_ID'; /** - * Paramètre : Texte pour la contribution + * Text for the contribution * Valeur : texte d'info à mettre avec des placeholders à remplacer */ public const PARAM_GENERATED_CONTRIB_INFO_TEXT = 'GENERATED_CONTRIB_INFO_TEXT'; /** - * Paramètre : Autoriser les membres non staff ni admin à pouvoir louer un objet = accès à la page take_object.php + * Allow non staff members to rent objects * Valeur : 0 = false / 1 = true */ public const PARAM_ENABLE_MEMBER_RENT_OBJECT = 'ENABLE_MEMBER_RENT_OBJECT'; @@ -193,28 +193,26 @@ public function getPreferences(): array */ public function __get(string $name): mixed { - $forbidden = array(); - - if (!in_array($name, $forbidden) && isset($this->prefs[$name])) { + if (isset($this->prefs[$name])) { return $this->prefs[$name]; - } else { - Analog::log( - 'Preference `' . $name . '` is not set or is forbidden', - Analog::INFO - ); - return false; } + + $msg = __CLASS__ . '::' . $name . ' is not set'; + Analog::log( + $msg, + Analog::INFO + ); + throw new \RuntimeException($msg); } /** * Store preferences * - * @param array $data Posted data - * @param string[] $errors Errors + * @param array $data Posted data * * @return boolean */ - public function store(array $data, array &$errors): bool + public function store(array $data): bool { foreach ($data as $key => $value) { $this->prefs[$key] = $value; @@ -256,13 +254,6 @@ public function store(array $data, array &$errors): bool } catch (\Exception $e) { $this->zdb->connection->rollBack(); throw $e; - Analog::log( - 'Something went wrong :\'( | ' . $e->getMessage() . "\n" . - $e->getTraceAsString(), - Analog::ERROR - ); - $errors[] = _T("Unable to store preferences :(", "objectslend"); - return false; } } diff --git a/lib/GaletteObjectsLend/Filters/CategoriesList.php b/lib/GaletteObjectsLend/Filters/CategoriesList.php index 70e29ce..4177555 100644 --- a/lib/GaletteObjectsLend/Filters/CategoriesList.php +++ b/lib/GaletteObjectsLend/Filters/CategoriesList.php @@ -70,11 +70,11 @@ public function __construct() /** * Returns the field we want to default set order to * - * @return string field name + * @return int|string */ - protected function getDefaultOrder(): string + protected function getDefaultOrder(): int|string { - return 'name '; + return Categories::ORDERBY_NAME; } /** @@ -160,27 +160,17 @@ public function __set(string $name, mixed $value): void } break; default: - Analog::log( - '[CategoriesList] Unable to set proprety `' . $name . '`', - Analog::WARNING + throw new \RuntimeException( + sprintf( + 'Unable to set property "%s::%s"!', + __CLASS__, + $name + ) ); - break; } } } - /** - * Add SQL limit - * - * @param Select $select Original select - * - * @return void - */ - public function setLimit(Select $select): void - { - $this->setLimits($select); - } - /** * Set objects filter * diff --git a/lib/GaletteObjectsLend/Filters/ObjectsList.php b/lib/GaletteObjectsLend/Filters/ObjectsList.php index c39c9ca..961a7a4 100644 --- a/lib/GaletteObjectsLend/Filters/ObjectsList.php +++ b/lib/GaletteObjectsLend/Filters/ObjectsList.php @@ -76,11 +76,11 @@ public function __construct() /** * Returns the field we want to default set order to * - * @return string field name + * @return int|string */ - protected function getDefaultOrder(): string + protected function getDefaultOrder(): int|string { - return 'name'; + return Objects::ORDERBY_NAME; } /** @@ -172,11 +172,12 @@ public function __set(string $name, mixed $value): void } break; case 'filter_str': + case 'query': $this->$name = $value; break; case 'category_filter': if (is_numeric($value)) { - $this->$name = $value; + $this->$name = (int)$value; } elseif ($value !== null) { Analog::log( '[ObjectsList] Value for property `' . $name . @@ -192,7 +193,7 @@ public function __set(string $name, mixed $value): void case Objects::ALL_OBJECTS: case Objects::ACTIVE_OBJECTS: case Objects::INACTIVE_OBJECTS: - $this->active_filter = $value; + $this->active_filter = (int)$value; break; default: Analog::log( @@ -206,7 +207,7 @@ public function __set(string $name, mixed $value): void break; case 'field_filter': if (is_numeric($value)) { - $this->$name = $value; + $this->$name = (int)$value; } elseif ($value !== null) { Analog::log( '[ObjectsList] Value for property `' . $name . @@ -215,31 +216,18 @@ public function __set(string $name, mixed $value): void ); } break; - case 'query': - $this->$name = $value; - break; default: - Analog::log( - '[ObjectsList] Unable to set proprety `' . $name . '`', - Analog::WARNING + throw new \RuntimeException( + sprintf( + 'Unable to set property "%s::%s"!', + __CLASS__, + $name + ) ); - break; } } } - /** - * Add SQL limit - * - * @param Select $select Original select - * - * @return void - */ - public function setLimit(Select $select): void - { - $this->setLimits($select); - } - /** * Set commons filters for templates * diff --git a/lib/GaletteObjectsLend/Filters/StatusList.php b/lib/GaletteObjectsLend/Filters/StatusList.php index a332893..daae2e4 100644 --- a/lib/GaletteObjectsLend/Filters/StatusList.php +++ b/lib/GaletteObjectsLend/Filters/StatusList.php @@ -58,11 +58,11 @@ class StatusList extends Pagination /** * Returns the field we want to default set order to * - * @return string field name + * @return int|string */ - protected function getDefaultOrder(): string + protected function getDefaultOrder(): int|string { - return 'status_text'; + return Status::ORDERBY_NAME; } /** @@ -125,6 +125,7 @@ public function __set(string $name, mixed $value): void switch ($name) { case 'filter_str': + case 'query': $this->$name = $value; break; case 'active_filter': @@ -132,7 +133,7 @@ public function __set(string $name, mixed $value): void case Status::ALL: case Status::ACTIVE: case Status::INACTIVE: - $this->active_filter = $value; + $this->active_filter = (int)$value; break; default: Analog::log( @@ -149,28 +150,27 @@ public function __set(string $name, mixed $value): void case Status::DC_STOCK: case Status::IN_STOCK: case Status::OUT_STOCK: - $this->stock_filter = $value; + $this->stock_filter = (int)$value; break; default: Analog::log( '[StatusList] Value for stock filter should be either ' . - Status::IN_STOCK . ' or ' . - Status::OUT_STOCK . ' (' . $value . ' given)', + Status::IN_STOCK . ', ' . Status::OUT_STOCK . ' or ' . + Status::DC_STOCK . ' (' . $value . ' given)', Analog::WARNING ); break; } - break; - case 'query': - $this->$name = $value; break; default: - Analog::log( - '[StatusList] Unable to set proprety `' . $name . '`', - Analog::WARNING + throw new \RuntimeException( + sprintf( + 'Unable to set property "%s::%s"!', + __CLASS__, + $name + ) ); - break; } } } diff --git a/lib/GaletteObjectsLend/Repository/Categories.php b/lib/GaletteObjectsLend/Repository/Categories.php index 4946e81..b388a55 100644 --- a/lib/GaletteObjectsLend/Repository/Categories.php +++ b/lib/GaletteObjectsLend/Repository/Categories.php @@ -55,6 +55,7 @@ class Categories public const FILTER_NAME = 0; public const ORDERBY_NAME = 0; + public const ORDERBY_ACTIVITY = 1; private CategoriesList $filters; private ?int $count = null; @@ -111,7 +112,7 @@ public function getCategoriesList( //add limits to retrieve only relevant rows if ($limit === true) { - $this->filters->setLimit($select); + $this->filters->setLimits($select); } $rows = $this->zdb->execute($select); @@ -221,7 +222,6 @@ private function proceedCount(Select $select): void $countSelect = clone $select; $countSelect->reset($countSelect::COLUMNS); $countSelect->reset($countSelect::ORDER); - $countSelect->reset($countSelect::HAVING); $countSelect->reset($countSelect::JOINS); $countSelect->columns( array( @@ -229,13 +229,6 @@ private function proceedCount(Select $select): void ) ); - $have = $select->having; - if ($have->count() > 0) { - foreach ($have->getPredicates() as $h) { - $countSelect->where($h); - } - } - $joins = $select->getRawState($select::JOINS); foreach ($joins as $join) { $countSelect->join( @@ -247,11 +240,11 @@ private function proceedCount(Select $select): void } $results = $this->zdb->execute($countSelect); - - $this->count = $results->current()->count; - if (isset($this->filters) && $this->count > 0) { - $this->filters->setCounter($this->count); + $this->count = 0; + if ($results->count() > 0) { + $this->count = (int)$results->current()->count; } + $this->filters->setCounter($this->count); } catch (\Exception $e) { Analog::log( 'Cannot count categories | ' . $e->getMessage(), @@ -278,6 +271,11 @@ private function buildOrderClause(array $fields = null): array $order[] = 'name ' . $this->filters->getDirection(); } break; + case self::ORDERBY_ACTIVITY: + if ($this->canOrderBy('is_active', $fields)) { + $order[] = 'is_active ' . $this->filters->getDirection(); + } + break; } return $order; @@ -317,7 +315,7 @@ private function buildWhereClause(Select $select): void ); $select->where( - 'c.name LIKE ' . $token + 'LOWER(c.name) LIKE ' . $token ); } @@ -343,12 +341,12 @@ private function buildWhereClause(Select $select): void * Is field allowed to order? it should be present in * provided fields list (those that are SELECT'ed). * - * @param string $field_name Field name to order by - * @param array $fields SELECTE'ed fields + * @param string $field_name Field name to order by + * @param ?array $fields SELECTE'ed fields * * @return bool */ - private function canOrderBy(string $field_name, array $fields): bool + private function canOrderBy(string $field_name, ?array $fields): bool { if (!is_array($fields)) { return true; @@ -367,9 +365,9 @@ private function canOrderBy(string $field_name, array $fields): bool /** * Get count for current query * - * @return int + * @return ?int */ - public function getCount(): int + public function getCount(): ?int { return $this->count; } diff --git a/lib/GaletteObjectsLend/Repository/Objects.php b/lib/GaletteObjectsLend/Repository/Objects.php index e23fe32..700b2f2 100644 --- a/lib/GaletteObjectsLend/Repository/Objects.php +++ b/lib/GaletteObjectsLend/Repository/Objects.php @@ -123,7 +123,7 @@ public function getObjectsList( //add limits to retrieve only relevant rows if ($limit === true) { - $this->filters->setLimit($select); + $this->filters->setLimits($select); } $rows = $this->zdb->execute($select); @@ -209,25 +209,6 @@ public function removeObjects(array $ids): bool return false; } - /** - * Disable selected objects - * - * @param array $ids List of objects id to disable - * - * @return ResultSet - */ - public function disableObjects(array $ids): ResultSet - { - $update = $this->zdb->update(LEND_PREFIX . self::TABLE); - $update->set(['is_active' => false]); - $update->where->in( - self::PK, - $ids - ); - $results = $this->zdb->execute($update); - return $results; - } - /** * Get Objects list * @@ -359,7 +340,7 @@ private function proceedCount(Select $select): void $results = $zdb->execute($countSelect); - $this->count = $results->current()->count; + $this->count = (int)$results->current()->count; if (isset($this->filters) && $this->count > 0) { $this->filters->setCounter($this->count); } @@ -493,7 +474,7 @@ public function buildWhereClause(Select $select): void ); } else { $select->where( - 'o.name LIKE ' . $token + 'LOWER(o.name) LIKE ' . $token ); } break; @@ -551,9 +532,9 @@ private function canOrderBy(string $field_name, ?array $fields): bool /** * Get count for current query * - * @return int + * @return ?int */ - public function getCount(): int + public function getCount(): ?int { return $this->count; } diff --git a/lib/GaletteObjectsLend/Repository/Status.php b/lib/GaletteObjectsLend/Repository/Status.php index 7d5639c..e4928e5 100644 --- a/lib/GaletteObjectsLend/Repository/Status.php +++ b/lib/GaletteObjectsLend/Repository/Status.php @@ -223,7 +223,7 @@ private function proceedCount(Select $select): void $results = $this->zdb->execute($countSelect); - $this->count = $results->current()->count; + $this->count = (int)$results->current()->count; if (isset($this->filters) && $this->count > 0) { $this->filters->setCounter($this->count); } @@ -308,7 +308,7 @@ private function buildWhereClause(Select $select): void ); $select->where( - 'c.status_text LIKE ' . $token + 'LOWER(c.status_text) LIKE ' . $token ); } } catch (\Exception $e) { @@ -347,9 +347,9 @@ private function canOrderBy(string $field_name, ?array $fields): bool /** * Get count for current query * - * @return int + * @return ?int */ - public function getCount(): int + public function getCount(): ?int { return $this->count; } diff --git a/templates/default/categories_list.html.twig b/templates/default/categories_list.html.twig index 8dc0b47..4ca18b5 100644 --- a/templates/default/categories_list.html.twig +++ b/templates/default/categories_list.html.twig @@ -50,7 +50,7 @@ {% set columns = [ {'label': '#'}, {'label': _T("Name", "objectslend"), order: constant('GaletteObjectsLend\\Repository\\Categories::ORDERBY_NAME')}, - {'label': _T("Active", "objectslend")}, + {'label': _T("Active", "objectslend"), order: constant('GaletteObjectsLend\\Repository\\Categories::ORDERBY_ACTIVITY')}, ] %} {{ parent() }} {% endblock %} diff --git a/tests/GaletteObjectsLend/Entity/tests/units/LendCategory.php b/tests/GaletteObjectsLend/Entity/tests/units/LendCategory.php new file mode 100644 index 0000000..b456b77 --- /dev/null +++ b/tests/GaletteObjectsLend/Entity/tests/units/LendCategory.php @@ -0,0 +1,116 @@ +. + */ + +namespace GaletteObjectsLends\Entity\tests\units; + +use Galette\GaletteTestCase; + +/** + * Category tests + * + * @author Johan Cwiklinski + */ +class LendCategory extends GaletteTestCase +{ + protected int $seed = 20240521212536; + + protected \Galette\Core\Plugins $plugins; + + /** + * Set up tests + * + * @return void + */ + public function setUp(): void + { + parent::setUp(); + $this->plugins = $this->container->get('plugins'); + } + + /** + * Cleanup after each test method + * + * @return void + */ + public function tearDown(): void + { + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendCategory::TABLE); + $this->zdb->execute($delete); + parent::tearDown(); + } + + /** + * Test empty + * + * @return void + */ + public function testEmpty(): void + { + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + $this->assertSame('No category (0)', $category->getName()); + $this->assertSame('No category', $category->getName(false)); + $this->assertInstanceOf(\GaletteObjectsLend\Entity\CategoryPicture::class, $category->getPicture()); + $this->assertSame(0.0, $category->getSum()); + $this->assertSame(0, $category->getObjectsNb()); + $this->assertTrue($category->isActive()); + $this->assertNull($category->getId()); + $this->assertSame('0,00', $category->objects_price_sum); + $this->assertNull($category->non_existing); + + $category = new \GaletteObjectsLend\Entity\LendCategory( + $this->zdb, + $this->plugins, + null, + ['picture' => false] + ); + $this->assertNull($category->getPicture()); + } + + /** + * Test add and update + * + * @return void + */ + public function testCrud(): void + { + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + + $category->name = 'Test category'; + $category->is_active = false; + + $this->assertTrue($category->store()); + $cid = $category->getId(); + $this->assertGreaterThan(0, $cid); + + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins, $cid); + $this->assertSame('Test category (0)', $category->getName()); + $this->assertFalse($category->isActive()); + + $category->name = 'Test category (edited)'; + $this->assertTrue($category->store()); + + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins, $cid); + $this->assertSame('Test category (edited) (0)', $category->getName()); + + $this->assertTrue($category->delete()); + new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins, $cid); + } +} diff --git a/tests/GaletteObjectsLend/Entity/tests/units/LendObject.php b/tests/GaletteObjectsLend/Entity/tests/units/LendObject.php new file mode 100644 index 0000000..24a2563 --- /dev/null +++ b/tests/GaletteObjectsLend/Entity/tests/units/LendObject.php @@ -0,0 +1,272 @@ +. + */ + +namespace GaletteObjectsLends\Entity\tests\units; + +use Galette\GaletteTestCase; + +/** + * Status tests + * + * @author Johan Cwiklinski + */ +class LendObject extends GaletteTestCase +{ + protected int $seed = 20240522000325; + + protected \Galette\Core\Plugins $plugins; + + private int $active_category_id; + private int $inactive_category_id; + private int $active_instock_status; + private int $active_notinstock_status; + private int $inactive_instock_status; + + /** + * Set up tests + * + * @return void + */ + public function setUp(): void + { + parent::setUp(); + $this->plugins = $this->container->get('plugins'); + $this->createCategories(); + $this->createStatus(); + } + + /** + * Cleanup after each test method + * + * @return void + */ + public function tearDown(): void + { + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendRent::TABLE); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendObject::TABLE); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendCategory::TABLE); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendStatus::TABLE); + $this->zdb->execute($delete); + + parent::tearDown(); + } + + /** + * Test empty + * + * @return void + */ + public function testEmpty(): void + { + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins); + $this->assertSame('€', $object->getCurrency()); + $this->assertNull($object->getCurrentRent()); + $this->assertTrue($object->isActive()); + $this->assertNull($object->getId()); + $this->assertNull($object->getCategoryId()); + $this->assertSame('', $object->getName()); + $this->assertSame(0.0, $object->getPrice()); + $this->assertSame(0.0, $object->getRentPrice()); + $this->assertFalse($object->isPricePerDay()); + $this->assertSame(0.0, $object->getWeight()); + $this->assertSame('', $object->getStatusText()); + $this->assertTrue($object->inStock()); + $this->assertSame('', $object->getDateBegin()); + $this->assertSame('', $object->getDateForecast()); + $this->assertNull($object->getIdAdh()); + $this->assertNull($object->getRentId()); + $this->assertNull($object->getCategoryId()); + $this->assertSame('', $object->getSerialNumber()); + } + + /** + * Test add and update + * + * @return void + */ + public function testCrud(): void + { + $deps = [ + 'category' => true, + 'status' => true, + 'last_rent' => true + ]; + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins, null, $deps); + + $object->name = 'An object'; + $object->category_id = $this->active_category_id; + $object->is_active = true; + $object->price = 1500.00; + $object->rent_price = 10.00; + $object->price_per_day = true; + $object->weight = 186.00; + + $this->assertTrue($object->store()); + $oid = $object->getId(); + $this->assertGreaterThan(0, $oid); + + $rent = new \GaletteObjectsLend\Entity\LendRent(); + $bdate = new \DateTime('2024-05-22 19:46:21'); + $rent->date_begin = $bdate->format('Y-m-d H:i:s'); + $edate = clone $bdate; + $edate->add(new \DateInterval('P1Y')); + $rent->date_end = $edate->format('Y-m-d H:i:s'); + $rent->status_id = $this->active_instock_status; + $rent->object_id = $oid; + $this->assertTrue($rent->store()); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins, $oid, $deps); + $this->assertTrue($object->isActive()); + $this->assertSame(1500.00, $object->getPrice()); + $this->assertSame('1 500,00', $object->price); + $this->assertSame(10.00, $object->getRentPrice()); + $this->assertSame('10,00', $object->rent_price); + $this->assertSame(10.00, $object->value_rent_price); + $this->assertSame(186.00, $object->getWeight()); + $this->assertSame('186,000', $object->weight); + + $this->assertSame('2024-05-22', $object->getDateBegin()); + $this->assertSame('2024-05-22', $object->date_begin); + $object->name = 'An object (edited)'; + $object->description = 'An object description'; + $object->serial_number = 'SE-aBc-RI@L'; + $object->dimension = '10x50'; + $this->assertTrue($object->store()); + + $filter = new \GaletteObjectsLend\Filters\ObjectsList(); + $filter->field_filter = \GaletteObjectsLend\Repository\Objects::FILTER_NAME; + $filter->filter_str = 'object'; + $this->assertSame('An object (edited)', $object->displayName($filter)); + $this->assertSame('An object description', $object->displayDescription($filter)); + + $filter = new \GaletteObjectsLend\Filters\ObjectsList(); + $filter->field_filter = \GaletteObjectsLend\Repository\Objects::FILTER_SERIAL; + $filter->filter_str = 'abc'; + $this->assertSame('SE-aBc-RI@L', $object->displaySerial($filter)); + + $filter = new \GaletteObjectsLend\Filters\ObjectsList(); + $filter->field_filter = \GaletteObjectsLend\Repository\Objects::FILTER_DIM; + $filter->filter_str = '50'; + $this->assertSame('10x50', $object->displayDimension($filter)); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins, $oid, $deps); + $this->assertSame('An object (edited)', $object->getName()); + + //edit category to inactive one + $object->category_id = $this->inactive_category_id; + $this->assertNull($object->member); + $this->assertNull($object->rents); + $this->assertTrue($object->store()); + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins, $oid, $deps + ['member' => true, 'rents' => true]); + $this->assertFalse($object->isActive()); + $this->assertInstanceOf(\Galette\Entity\Adherent::class, $object->member); + + //removing category + $rm_category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + + $rm_category->name = 'Category to be removed'; + $rm_category->is_active = true; + $this->assertTrue($rm_category->store()); + $category_id = $rm_category->getId(); + + $object->category_id = $category_id; + $this->assertTrue($object->store()); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins, $oid, $deps); + $this->assertSame($category_id, $object->getCategoryId()); + + $this->assertTrue($rm_category->delete()); + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins, $oid, $deps); + $this->assertNull($object->getCategoryId()); + + //clone + $this->assertTrue($object->clone()); + $clone_id = $object->getId(); + $this->assertNotEquals($oid, $clone_id); + $this->assertSame('An object (edited)', $object->getName()); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins, $oid); + $this->assertTrue($object->delete()); + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins, $clone_id); + $this->assertTrue($object->delete()); + } + + /** + * Create few status + * + * @return void + */ + private function createStatus(): void + { + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One active status'; + $status->in_stock = true; + $status->is_active = true; + $this->assertTrue($status->store()); + $this->active_instock_status = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'Another active status'; + $status->in_stock = false; + $status->is_active = true; + $this->assertTrue($status->store()); + $this->active_notinstock_status = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One inactive status'; + $status->in_stock = true; + $status->is_active = false; + $this->assertTrue($status->store()); + $this->inactive_instock_status = $status->status_id; + } + + /** + * Create few categories + * + * @return void + */ + private function createCategories(): void + { + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + + $category->name = 'Active test category'; + $category->is_active = true; + + $this->assertTrue($category->store()); + $this->active_category_id = $category->getId(); + $this->assertGreaterThan(0, $this->active_category_id); + + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + + $category->name = 'Inactive test category'; + $category->is_active = false; + + $this->assertTrue($category->store()); + $this->inactive_category_id = $category->getId(); + $this->assertGreaterThan(0, $this->inactive_category_id); + } +} \ No newline at end of file diff --git a/tests/GaletteObjectsLend/Entity/tests/units/LendRent.php b/tests/GaletteObjectsLend/Entity/tests/units/LendRent.php new file mode 100644 index 0000000..ecdb993 --- /dev/null +++ b/tests/GaletteObjectsLend/Entity/tests/units/LendRent.php @@ -0,0 +1,250 @@ +. + */ + +namespace GaletteObjectsLends\Entity\tests\units; + +use Galette\GaletteTestCase; + +/** + * Status tests + * + * @author Johan Cwiklinski + */ +class LendRent extends GaletteTestCase +{ + protected int $seed = 20240524220704; + + protected \Galette\Core\Plugins $plugins; + private int $active_instock_status; + private int $active_notinstock_status; + private int $inactive_instock_status; + + /** + * Set up tests + * + * @return void + */ + public function setUp(): void + { + parent::setUp(); + $this->plugins = $this->container->get('plugins'); + //$this->createCategories(); + $this->createStatus(); + } + + /** + * Cleanup after each test method + * + * @return void + */ + public function tearDown(): void + { + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendRent::TABLE); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendCategory::TABLE); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendStatus::TABLE); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE); + $delete->where(['fingerprint' => 'FAKER' . $this->seed]); + $this->zdb->execute($delete); + + parent::tearDown(); + } + + /** + * Test empty + * + * @return void + */ + public function testEmpty(): void + { + $rent = new \GaletteObjectsLend\Entity\LendRent(); + $this->assertNull($rent->rent_id); + $this->assertNull($rent->object_id); + $this->assertNull($rent->status_id); + $this->assertNull($rent->adherent_id); + $this->assertMatchesRegularExpression('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}/', $rent->date_begin); + $this->assertSame('', $rent->date_forecast); + $this->assertSame('', $rent->date_end); + $this->assertSame('', $rent->comments); + } + + /** + * Test add and update + * + * @return void + */ + public function testCrud(): void + { + $rent = new \GaletteObjectsLend\Entity\LendRent(); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins); + $object->name = 'Test object'; + $this->assertTrue($object->store()); + $oid = $object->object_id; + + $bdate = new \DateTime('2024-05-22 19:46:21'); + $rent->date_begin = $bdate->format('Y-m-d H:i:s'); + $rent->object_id = $oid; + $rent->status_id = $this->active_instock_status; + $rent->comments = 'Test comment'; + $this->assertTrue($rent->store()); + $rent_id = $rent->rent_id; + + $rent = new \GaletteObjectsLend\Entity\LendRent($rent_id); + $this->assertSame($this->active_instock_status, $rent->status_id); + + //update rent + $member = $this->getMemberOne(); + $rent = new \GaletteObjectsLend\Entity\LendRent($rent_id); + $rent->status_id = $this->active_notinstock_status; + $rent->adherent_id = $member->id; + $this->assertTrue($rent->store()); + + $rent = new \GaletteObjectsLend\Entity\LendRent($rent_id); + $this->assertSame($this->active_notinstock_status, $rent->status_id); + $this->assertSame($member->id, $rent->adherent_id); + + //active members + $this->assertCount(1, $rent::getAllActivesAdherents()); + + //another (older) rent + $rent = new \GaletteObjectsLend\Entity\LendRent(); + $bdate = new \DateTime('2024-05-22 19:46:21'); + $bdate->sub(new \DateInterval('P2Y')); + $rent->date_begin = $bdate->format('Y-m-d H:i:s'); + $rent2_edate = clone $bdate; + $rent2_edate->add(new \DateInterval('P1Y')); + $rent->date_end = $rent2_edate->format('Y-m-d H:i:s'); + $rent->date_forecast = $rent2_edate->format('Y-m-d'); + $rent->object_id = $oid; + $rent->status_id = $this->active_instock_status; + $rent->comments = 'Test 2 comment'; + $this->assertTrue($rent->store()); + $rent2_id = $rent->rent_id; + $this->assertNotEquals($rent_id, $rent2_id); + $this->assertSame($rent2_edate->format('Y-m-d'), $rent->date_forecast); + + $object_rents = $rent::getRentsForObjectId($oid); + $this->assertCount(2, $object_rents); + + //only last + $object_rents = $rent::getRentsForObjectId($oid, true); + $this->assertCount(1, $object_rents); + + //close all rents + $this->assertTrue($rent::closeAllRentsForObject($oid, 'Now closed.')); + + $rent = new \GaletteObjectsLend\Entity\LendRent($rent_id); + $this->assertSame('Now closed.', $rent->comments); + + //had an end_date, should not be modified + $rent = new \GaletteObjectsLend\Entity\LendRent($rent2_id); + $this->assertEquals($rent2_edate->format('Y-m-d H:i'), $rent->date_end); + $this->assertSame('Test 2 comment', $rent->comments); + + //cleanup to avoid constraint errors + $this->assertTrue($object->delete()); + + /*$status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One active status'; + $status->in_stock = true; + $status->is_active = true; + $this->assertTrue($status->store()); + $status_one = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'Another active status'; + $status->in_stock = false; + $status->is_active = true; + $this->assertTrue($status->store()); + $status_two = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One inactive status'; + $status->in_stock = true; + $status->is_active = false; + $this->assertTrue($status->store()); + $status_three = $status->status_id; + + $list = $status::getActiveTakeAwayStatuses($this->zdb); + $this->assertCount(1, $list); + + $active_one = $list[0]; + $this->assertSame($status_two, $active_one->status_id); + $this->assertSame('Another active status', $active_one->status_text); + + $list = $status::getActiveStockStatuses($this->zdb); + $this->assertCount(1, $list); + + $active_one = $list[0]; + $this->assertSame($status_one, $active_one->status_id); + $this->assertSame('One active status', $active_one->status_text); + + $list = $status::getActiveStatuses($this->zdb); + $this->assertCount(2, $list); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb, $status_one); + $status->status_text = 'One active status (edited)'; + $this->assertTrue($status->store()); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb, $status_one); + $this->assertSame('One active status (edited)', $status->status_text); + + $this->assertTrue($status->delete()); + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb, $status_one); + $this->assertNull($status->status_id);*/ + } + + /** + * Create few status + * + * @return void + */ + private function createStatus(): void + { + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One active status'; + $status->in_stock = true; + $status->is_active = true; + $this->assertTrue($status->store()); + $this->active_instock_status = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'Another active status'; + $status->in_stock = false; + $status->is_active = true; + $this->assertTrue($status->store()); + $this->active_notinstock_status = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One inactive status'; + $status->in_stock = true; + $status->is_active = false; + $this->assertTrue($status->store()); + $this->inactive_instock_status = $status->status_id; + } + +} diff --git a/tests/GaletteObjectsLend/Entity/tests/units/LendStatus.php b/tests/GaletteObjectsLend/Entity/tests/units/LendStatus.php new file mode 100644 index 0000000..a1b34c2 --- /dev/null +++ b/tests/GaletteObjectsLend/Entity/tests/units/LendStatus.php @@ -0,0 +1,118 @@ +. + */ + +namespace GaletteObjectsLends\Entity\tests\units; + +use Galette\GaletteTestCase; + +/** + * Status tests + * + * @author Johan Cwiklinski + */ +class LendStatus extends GaletteTestCase +{ + protected int $seed = 20240521230915; + + /** + * Cleanup after each test method + * + * @return void + */ + public function tearDown(): void + { + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendStatus::TABLE); + $this->zdb->execute($delete); + parent::tearDown(); + } + + /** + * Test empty + * + * @return void + */ + public function testEmpty(): void + { + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $this->assertNull($status->status_id); + $this->assertSame('', $status->status_text); + $this->assertFalse($status->in_stock); + $this->assertTrue($status->is_active); + $this->assertNull($status->rent_day_number); + } + + /** + * Test add and update + * + * @return void + */ + public function testCrud(): void + { + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One active status'; + $status->in_stock = true; + $status->is_active = true; + $this->assertTrue($status->store()); + $status_one = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'Another active status'; + $status->in_stock = false; + $status->is_active = true; + $this->assertTrue($status->store()); + $status_two = $status->status_id; + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One inactive status'; + $status->in_stock = true; + $status->is_active = false; + $this->assertTrue($status->store()); + $status_three = $status->status_id; + + $list = $status::getActiveTakeAwayStatuses($this->zdb); + $this->assertCount(1, $list); + + $active_one = $list[0]; + $this->assertSame($status_two, $active_one->status_id); + $this->assertSame('Another active status', $active_one->status_text); + + $list = $status::getActiveStockStatuses($this->zdb); + $this->assertCount(1, $list); + + $active_one = $list[0]; + $this->assertSame($status_one, $active_one->status_id); + $this->assertSame('One active status', $active_one->status_text); + + $list = $status::getActiveStatuses($this->zdb); + $this->assertCount(2, $list); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb, $status_one); + $status->status_text = 'One active status (edited)'; + $this->assertTrue($status->store()); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb, $status_one); + $this->assertSame('One active status (edited)', $status->status_text); + + $this->assertTrue($status->delete()); + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb, $status_one); + $this->assertNull($status->status_id); + } +} diff --git a/tests/GaletteObjectsLend/Entity/tests/units/Preferences.php b/tests/GaletteObjectsLend/Entity/tests/units/Preferences.php new file mode 100644 index 0000000..694acf2 --- /dev/null +++ b/tests/GaletteObjectsLend/Entity/tests/units/Preferences.php @@ -0,0 +1,90 @@ +. + */ + +namespace GaletteObjectsLends\Entity\tests\units; + +use Galette\GaletteTestCase; + +/** + * Preferences tests + * + * @author Johan Cwiklinski + */ +class Preferences extends GaletteTestCase +{ + protected int $seed = 20240519131740; + + /** + * Test defaults + * + * @return void + */ + public function testDefaults(): void + { + $prefs = new \GaletteObjectsLend\Entity\Preferences($this->zdb); + $this->assertSame(128, $prefs->getThumbWidth()); + $this->assertSame(128, $prefs->getThumbHeight()); + $this->assertTrue($prefs->imagesInLists()); + $this->assertTrue($prefs->showFullsize()); + + $all_prefs = $prefs->getPreferences(); + $this->assertCount(17, $all_prefs); + foreach ($all_prefs as $code => $value) { + $this->assertSame($value, $prefs->$code); + } + + $prefs = new \GaletteObjectsLend\Entity\Preferences($this->zdb, false); + $this->assertTrue($prefs->load()); + + $prefs = new \GaletteObjectsLend\Entity\Preferences($this->zdb, false); + $this->assertCount(17, $prefs->getPreferences()); + + $this->expectException(\RuntimeException::class); + $this->assertSame(null, $prefs->NON_EXISTING); + } + + /** + * Test add and update + * + * @return void + */ + public function testCrud(): void + { + $prefs = new \GaletteObjectsLend\Entity\Preferences($this->zdb); + $orig_prefs = $prefs->getPreferences(); + $this->assertCount(17, $orig_prefs); + + $all_prefs = $orig_prefs; + + $this->assertSame(128, (int)$all_prefs[\GaletteObjectsLend\Entity\Preferences::PARAM_THUMB_MAX_WIDTH]); + $this->assertSame(128, (int)$all_prefs[\GaletteObjectsLend\Entity\Preferences::PARAM_THUMB_MAX_HEIGHT]); + $all_prefs[\GaletteObjectsLend\Entity\Preferences::PARAM_THUMB_MAX_WIDTH] = 256; + $all_prefs[\GaletteObjectsLend\Entity\Preferences::PARAM_THUMB_MAX_HEIGHT] = 256; + + $this->assertTrue($prefs->store($all_prefs)); + $all_prefs = $prefs->getPreferences(); + $this->assertCount(17, $all_prefs); + $this->assertSame(256, (int)$all_prefs[\GaletteObjectsLend\Entity\Preferences::PARAM_THUMB_MAX_WIDTH]); + $this->assertSame(256, (int)$all_prefs[\GaletteObjectsLend\Entity\Preferences::PARAM_THUMB_MAX_HEIGHT]); + + $this->assertTrue($prefs->store($orig_prefs)); + } +} diff --git a/tests/GaletteObjectsLend/Filters/tests/units/CategoriesList.php b/tests/GaletteObjectsLend/Filters/tests/units/CategoriesList.php new file mode 100644 index 0000000..571b341 --- /dev/null +++ b/tests/GaletteObjectsLend/Filters/tests/units/CategoriesList.php @@ -0,0 +1,135 @@ +. + */ + +declare(strict_types=1); + +namespace GaletteObjectslend\Filters\test\units; + +use Galette\GaletteTestCase; + +/** + * Categories filters tests class + * + * @author Johan Cwiklinski + */ +class CategoriesList extends GaletteTestCase +{ + /** + * Test filter defaults values + * + * @param \GaletteObjectsLend\Filters\CategoriesList $filters Filters instance + * + * @return void + */ + protected function testDefaults(\GaletteObjectsLend\Filters\CategoriesList $filters): void + { + $this->assertSame(\GaletteObjectsLend\Repository\Categories::ORDERBY_NAME, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\CategoriesList::ORDER_ASC, $filters->ordered); + $this->assertNull($filters->filter_str); + $this->assertNull($filters->active_filter); + $this->assertNull($filters->not_empty); + $this->assertNull($filters->objects_filters); + } + + /** + * Test creation + * + * @return void + */ + public function testCreate(): void + { + $filters = new \GaletteObjectsLend\Filters\CategoriesList(); + + $this->testDefaults($filters); + + //change order field + $filters->orderby = \GaletteObjectsLend\Repository\Categories::ORDERBY_ACTIVITY; + $this->assertSame(\GaletteObjectsLend\Repository\Categories::ORDERBY_ACTIVITY, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\CategoriesList::ORDER_ASC, $filters->ordered); + + //same order field again: direction inverted + $filters->orderby = \GaletteObjectsLend\Repository\Categories::ORDERBY_ACTIVITY; + $this->assertSame(\GaletteObjectsLend\Repository\Categories::ORDERBY_ACTIVITY, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\CategoriesList::ORDER_DESC, $filters->ordered); + + //not existing order, same kept + $filters->ordered = 42; + $this->assertSame(\GaletteObjectsLend\Repository\Categories::ORDERBY_ACTIVITY, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\CategoriesList::ORDER_DESC, $filters->ordered); + + //change direction only + $filters->ordered = \GaletteObjectsLend\Filters\CategoriesList::ORDER_ASC; + $this->assertSame(\GaletteObjectsLend\Repository\Categories::ORDERBY_ACTIVITY, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\CategoriesList::ORDER_ASC, $filters->ordered); + + //set string filter + $filters->filter_str = 'a string'; + $this->assertSame('a string', $filters->filter_str); + + //Set activity filter + $filters->active_filter = \GaletteObjectsLend\Repository\Categories::INACTIVE_CATEGORIES; + $this->assertSame(\GaletteObjectsLend\Repository\Categories::INACTIVE_CATEGORIES, $filters->active_filter); + + //cast is forced + $filters->active_filter = (string)\GaletteObjectsLend\Repository\Categories::INACTIVE_CATEGORIES; + $this->assertSame(\GaletteObjectsLend\Repository\Categories::INACTIVE_CATEGORIES, $filters->active_filter); + + //out of known values, no change + $filters->active_filter = 42; + $this->assertSame(\GaletteObjectsLend\Repository\Categories::INACTIVE_CATEGORIES, $filters->active_filter); + + $ofilters = new \GaletteObjectsLend\Filters\ObjectsList(); + $this->assertInstanceOf($filters::class, $filters->setObjectsFilter($ofilters)); + $this->assertSame($ofilters, $filters->objects_filters); + + //reinit and test defaults are back + $filters->reinit(); + $this->testDefaults($filters); + } + + /** + * Test setting non existing filter + * + * @return void + */ + public function testSetNotExisting() + { + $filters = new \GaletteObjectsLend\Filters\CategoriesList(); + $this->testDefaults($filters); + + $this->expectException(\RuntimeException::class); + $filters->non_existing = 42; + } + + /** + * Test getting non existing filter + * + * @return void + */ + public function testGetNotExisting() + { + $filters = new \GaletteObjectsLend\Filters\CategoriesList(); + $this->testDefaults($filters); + + $this->expectException(\RuntimeException::class); + $value = $filters->non_existing; + } +} diff --git a/tests/GaletteObjectsLend/Filters/tests/units/ObjectsList.php b/tests/GaletteObjectsLend/Filters/tests/units/ObjectsList.php new file mode 100644 index 0000000..48091f8 --- /dev/null +++ b/tests/GaletteObjectsLend/Filters/tests/units/ObjectsList.php @@ -0,0 +1,143 @@ +. + */ + +declare(strict_types=1); + +namespace GaletteObjectslend\Filters\test\units; + +use Galette\GaletteTestCase; + +/** + * Objects filters tests class + * + * @author Johan Cwiklinski + */ +class ObjectsList extends GaletteTestCase +{ + /** + * Test filter defaults values + * + * @param \GaletteObjectsLend\Filters\ObjectsList $filters Filters instance + * + * @return void + */ + protected function testDefaults(\GaletteObjectsLend\Filters\ObjectsList $filters): void + { + $this->assertSame(\GaletteObjectsLend\Repository\Objects::ORDERBY_NAME, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\ObjectsList::ORDER_ASC, $filters->ordered); + $this->assertNull($filters->filter_str); + $this->assertNull($filters->category_filter); + $this->assertNull($filters->active_filter); + $this->assertNull($filters->field_filter); + $this->assertSame([], $filters->selected); + } + + /** + * Test creation + * + * @return void + */ + public function testCreate(): void + { + $filters = new \GaletteObjectsLend\Filters\ObjectsList(); + + $this->testDefaults($filters); + + //change order field + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_STATUS; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::ORDERBY_STATUS, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\ObjectsList::ORDER_ASC, $filters->ordered); + + //same order field again: direction inverted + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_STATUS; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::ORDERBY_STATUS, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\ObjectsList::ORDER_DESC, $filters->ordered); + + //not existing order, same kept + $filters->ordered = 42; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::ORDERBY_STATUS, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\ObjectsList::ORDER_DESC, $filters->ordered); + + //change direction only + $filters->ordered = \GaletteObjectsLend\Filters\ObjectsList::ORDER_ASC; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::ORDERBY_STATUS, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\ObjectsList::ORDER_ASC, $filters->ordered); + + //set string filter + $filters->filter_str = 'a string'; + $this->assertSame('a string', $filters->filter_str); + + //Set activity filter + $filters->active_filter = \GaletteObjectsLend\Repository\Objects::INACTIVE_OBJECTS; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::INACTIVE_OBJECTS, $filters->active_filter); + + //cast is forced + $filters->active_filter = (string)\GaletteObjectsLend\Repository\Objects::INACTIVE_OBJECTS; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::INACTIVE_OBJECTS, $filters->active_filter); + + //out of known values, no change + $filters->active_filter = 42; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::INACTIVE_OBJECTS, $filters->active_filter); + + $filters->field_filter = \GaletteObjectsLend\Repository\Objects::FILTER_SERIAL; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::FILTER_SERIAL, $filters->field_filter); + + //cast is forced + $filters->field_filter = (string)\GaletteObjectsLend\Repository\Objects::FILTER_SERIAL; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::FILTER_SERIAL, $filters->field_filter); + + //non numeric value, no change + $filters->field_filter = 'abc'; + $this->assertSame(\GaletteObjectsLend\Repository\Objects::FILTER_SERIAL, $filters->field_filter); + + //reinit and test defaults are back + $filters->reinit(); + $this->testDefaults($filters); + } + + /** + * Test setting non existing filter + * + * @return void + */ + public function testSetNotExisting() + { + $filters = new \GaletteObjectsLend\Filters\ObjectsList(); + $this->testDefaults($filters); + + $this->expectException(\RuntimeException::class); + $filters->non_existing = 42; + } + + /** + * Test getting non existing filter + * + * @return void + */ + public function testGetNotExisting() + { + $filters = new \GaletteObjectsLend\Filters\ObjectsList(); + $this->testDefaults($filters); + + $this->expectException(\RuntimeException::class); + $value = $filters->non_existing; + } +} diff --git a/tests/GaletteObjectsLend/Filters/tests/units/StatusList.php b/tests/GaletteObjectsLend/Filters/tests/units/StatusList.php new file mode 100644 index 0000000..7f91ffb --- /dev/null +++ b/tests/GaletteObjectsLend/Filters/tests/units/StatusList.php @@ -0,0 +1,141 @@ +. + */ + +declare(strict_types=1); + +namespace GaletteObjectslend\Filters\test\units; + +use Galette\GaletteTestCase; + +/** + * Status filters tests class + * + * @author Johan Cwiklinski + */ +class StatusList extends GaletteTestCase +{ + /** + * Test filter defaults values + * + * @param \GaletteObjectsLend\Filters\StatusList $filters Filters instance + * + * @return void + */ + protected function testDefaults(\GaletteObjectsLend\Filters\StatusList $filters): void + { + $this->assertSame(\GaletteObjectsLend\Repository\Status::ORDERBY_NAME, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\StatusList::ORDER_ASC, $filters->ordered); + $this->assertNull($filters->filter_str); + $this->assertNull($filters->active_filter); + $this->assertNull($filters->stock_filter); + } + + /** + * Test creation + * + * @return void + */ + public function testCreate(): void + { + $filters = new \GaletteObjectsLend\Filters\StatusList(); + + $this->testDefaults($filters); + + //change order field + $filters->orderby = \GaletteObjectsLend\Repository\Status::ORDERBY_STOCK; + $this->assertSame(\GaletteObjectsLend\Repository\Status::ORDERBY_STOCK, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\StatusList::ORDER_ASC, $filters->ordered); + + //same order field again: direction inverted + $filters->orderby = \GaletteObjectsLend\Repository\Status::ORDERBY_STOCK; + $this->assertSame(\GaletteObjectsLend\Repository\Status::ORDERBY_STOCK, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\StatusList::ORDER_DESC, $filters->ordered); + + //not existing order, same kept + $filters->ordered = 42; + $this->assertSame(\GaletteObjectsLend\Repository\Status::ORDERBY_STOCK, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\StatusList::ORDER_DESC, $filters->ordered); + + //change direction only + $filters->ordered = \GaletteObjectsLend\Filters\StatusList::ORDER_ASC; + $this->assertSame(\GaletteObjectsLend\Repository\Status::ORDERBY_STOCK, $filters->orderby); + $this->assertSame(\GaletteObjectsLend\Filters\StatusList::ORDER_ASC, $filters->ordered); + + //set string filter + $filters->filter_str = 'a string'; + $this->assertSame('a string', $filters->filter_str); + + //Set activity filter + $filters->active_filter = \GaletteObjectsLend\Repository\Status::INACTIVE; + $this->assertSame(\GaletteObjectsLend\Repository\Status::INACTIVE, $filters->active_filter); + + //cast is forced + $filters->active_filter = (string)\GaletteObjectsLend\Repository\Status::INACTIVE; + $this->assertSame(\GaletteObjectsLend\Repository\Status::INACTIVE, $filters->active_filter); + + //out of known values, no change + $filters->active_filter = 42; + $this->assertSame(\GaletteObjectsLend\Repository\Status::INACTIVE, $filters->active_filter); + + //set stock filter + $filters->stock_filter = \GaletteObjectsLend\Repository\Status::IN_STOCK; + $this->assertSame(\GaletteObjectsLend\Repository\Status::IN_STOCK, $filters->stock_filter); + + $filters->stock_filter = \GaletteObjectsLend\Repository\Status::OUT_STOCK; + $this->assertSame(\GaletteObjectsLend\Repository\Status::OUT_STOCK, $filters->stock_filter); + + //out of known values, no change + $filters->stock_filter = 42; + $this->assertSame(\GaletteObjectsLend\Repository\Status::OUT_STOCK, $filters->stock_filter); + + //reinit and test defaults are back + $filters->reinit(); + $this->testDefaults($filters); + } + + /** + * Test setting non existing filter + * + * @return void + */ + public function testSetNotExisting() + { + $filters = new \GaletteObjectsLend\Filters\StatusList(); + $this->testDefaults($filters); + + $this->expectException(\RuntimeException::class); + $filters->non_existing = 42; + } + + /** + * Test getting non existing filter + * + * @return void + */ + public function testGetNotExisting() + { + $filters = new \GaletteObjectsLend\Filters\StatusList(); + $this->testDefaults($filters); + + $this->expectException(\RuntimeException::class); + $value = $filters->non_existing; + } +} diff --git a/tests/GaletteObjectsLend/Repository/tests/units/Categories.php b/tests/GaletteObjectsLend/Repository/tests/units/Categories.php new file mode 100644 index 0000000..f38386e --- /dev/null +++ b/tests/GaletteObjectsLend/Repository/tests/units/Categories.php @@ -0,0 +1,133 @@ +. + */ + +namespace GaletteObjectsLends\Repository\tests\units; + +use Galette\GaletteTestCase; + +/** + * Categories tests + * + * @author Johan Cwiklinski + */ +class Categories extends GaletteTestCase +{ + protected int $seed = 20240525091538; + + protected \Galette\Core\Plugins $plugins; + + /** + * Set up tests + * + * @return void + */ + public function setUp(): void + { + parent::setUp(); + $this->plugins = $this->container->get('plugins'); + } + + /** + * Cleanup after each test method + * + * @return void + */ + public function tearDown(): void + { + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendObject::TABLE); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendCategory::TABLE); + $this->zdb->execute($delete); + parent::tearDown(); + } + + /** + * Test getList + * + * @return void + */ + public function testGetList(): void + { + $categories = new \GaletteObjectsLend\Repository\Categories($this->zdb, $this->login, $this->plugins); + + $rs_list = $categories->getList(); + $this->assertInstanceOf(\Laminas\Db\ResultSet\ResultSet::class, $rs_list); + $this->assertSame(0, $rs_list->count()); + $this->assertSame([], $categories->getList(true)); + $this->assertNull($categories->getCount()); + + $this->assertSame([], $categories->getCategoriesList(true)); + $this->assertSame(0, $categories->getCount()); + + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + $category->name = 'One category'; + $category->is_active = true; + $this->assertTrue($category->store()); + $cat_one_id = $category->category_id; + + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + $category->name = 'Another category'; + $category->is_active = true; + $this->assertTrue($category->store()); + + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + $category->name = 'Yet another category'; + $category->is_active = false; + $this->assertTrue($category->store()); + + $filters = new \GaletteObjectsLend\Filters\CategoriesList(); + $categories = new \GaletteObjectsLend\Repository\Categories($this->zdb, $this->login, $this->plugins, $filters); + + $this->assertCount(3, $categories->getCategoriesList(true)); + $this->assertSame(3, $categories->getCount()); + + $filters->filter_str = 'category'; + $this->assertCount(3, $categories->getCategoriesList(true)); + + $filters->filter_str = 'Yet'; + $this->assertCount(1, $categories->getCategoriesList(true)); + + $filters->filter_str = 'noone'; + $this->assertCount(0, $categories->getCategoriesList(true)); + + $filters->reinit(); + $filters->not_empty = true; + $this->assertCount(0, $categories->getCategoriesList(true)); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins); + $object->name = 'One object'; + $object->category_id = $cat_one_id; + $this->assertTrue($object->store()); + + $this->assertCount(1, $categories->getCategoriesList(true)); + + $filters->reinit(); + $filters->active_filter = \GaletteObjectsLend\Repository\Categories::ALL_CATEGORIES; + $this->assertCount(3, $categories->getCategoriesList(true)); + + $filters->active_filter = \GaletteObjectsLend\Repository\Categories::ACTIVE_CATEGORIES; + $this->assertCount(2, $categories->getCategoriesList(true)); + + $filters->active_filter = \GaletteObjectsLend\Repository\Categories::INACTIVE_CATEGORIES; + $this->assertCount(1, $categories->getCategoriesList(true)); + } +} diff --git a/tests/GaletteObjectsLend/Repository/tests/units/Objects.php b/tests/GaletteObjectsLend/Repository/tests/units/Objects.php new file mode 100644 index 0000000..ec20ee7 --- /dev/null +++ b/tests/GaletteObjectsLend/Repository/tests/units/Objects.php @@ -0,0 +1,241 @@ +. + */ + +namespace GaletteObjectsLends\Repository\tests\units; + +use Galette\GaletteTestCase; + +/** + * Categories tests + * + * @author Johan Cwiklinski + */ +class Objects extends GaletteTestCase +{ + protected int $seed = 20240526224135; + + protected \Galette\Core\Plugins $plugins; + protected \GaletteObjectsLend\Entity\Preferences $lend_prefs; + + /** + * Set up tests + * + * @return void + */ + public function setUp(): void + { + parent::setUp(); + $this->plugins = $this->container->get('plugins'); + $this->lend_prefs = new \GaletteObjectsLend\Entity\Preferences($this->zdb); + } + + /** + * Cleanup after each test method + * + * @return void + */ + public function tearDown(): void + { + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendObject::TABLE); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendCategory::TABLE); + $this->zdb->execute($delete); + + parent::tearDown(); + } + + /** + * Test getList + * + * @return void + */ + public function testGetList(): void + { + $objects = new \GaletteObjectsLend\Repository\Objects($this->zdb, $this->plugins, $this->lend_prefs); + + $rs_list = $objects->getList(); + $this->assertInstanceOf(\Laminas\Db\ResultSet\ResultSet::class, $rs_list); + $this->assertSame(0, $rs_list->count()); + $this->assertSame([], $objects->getList(true)); + $this->assertNull($objects->getCount()); + + $this->assertSame([], $objects->getObjectsList(true)); + $this->assertSame(0, $objects->getCount()); + + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + $category->name = 'First category'; + $category->is_active = true; + $this->assertTrue($category->store()); + $first_category_id = $category->getId(); + $this->assertGreaterThan(0, $first_category_id); + + $category = new \GaletteObjectsLend\Entity\LendCategory($this->zdb, $this->plugins); + $category->name = 'Second category'; + $category->is_active = true; + $this->assertTrue($category->store()); + $second_category_id = $category->getId(); + $this->assertGreaterThan(0, $second_category_id); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins); + $object->name = 'First object'; + $object->category_id = $first_category_id; + $object->is_active = true; + $this->assertTrue($object->store()); + $first_object_id = $object->getId(); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins); + $object->name = 'Second object'; + $object->description = 'First description'; + $object->category_id = $first_category_id; + $object->is_active = true; + $this->assertTrue($object->store()); + $second_object_id = $object->getId(); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins); + $object->name = 'Third object'; + $object->category_id = $second_category_id; + $object->is_active = true; + $this->assertTrue($object->store()); + $third_object_id = $object->getId(); + + $object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins); + $object->name = 'Fourth object'; + $object->serial_number = 'GGABCDEXX'; + $object->dimension = '210x297'; + $object->is_active = false; + $this->assertTrue($object->store()); + $fourth_object_id = $object->getId(); + + $filters = new \GaletteObjectsLend\Filters\ObjectsList(); + $objects = new \GaletteObjectsLend\Repository\Objects($this->zdb, $this->plugins, $this->lend_prefs, $filters); + + $this->assertCount(4, $objects->getObjectsList(true)); + $this->assertSame(4, $objects->getCount()); + + $selected = [ + $first_object_id, + $third_object_id + ]; + $filters->selected = $selected; + $this->assertCount(2, $objects->getObjectsList(true)); + $this->assertSame(2, $objects->getCount()); + + $filters->reinit(); + $filters->active_filter = \GaletteObjectsLend\Repository\Objects::ACTIVE_OBJECTS; + $this->assertCount(3, $objects->getObjectsList(true)); + $this->assertSame(3, $objects->getCount()); + + $filters->active_filter = \GaletteObjectsLend\Repository\Objects::INACTIVE_OBJECTS; + $this->assertCount(1, $objects->getObjectsList(true)); + $this->assertSame(1, $objects->getCount()); + + $filters->reinit(); + $filters->category_filter = -1; + $this->assertCount(1, $objects->getObjectsList(true)); + $this->assertSame(1, $objects->getCount()); + + $filters->category_filter = $first_category_id; + $this->assertCount(2, $objects->getObjectsList(true)); + $this->assertSame(2, $objects->getCount()); + + $filters->category_filter = $second_category_id; + $this->assertCount(1, $objects->getObjectsList(true)); + $this->assertSame(1, $objects->getCount()); + + $filters->reinit(); + $filters->filter_str = 'OBJECT'; + $this->assertCount(4, $objects->getObjectsList(true)); + $this->assertSame(4, $objects->getCount()); + + $filters->filter_str = 'first'; + $this->assertCount(2, $objects->getObjectsList(true)); + $this->assertSame(2, $objects->getCount()); + + //disable view description + $orig_prefs = $this->lend_prefs->getPreferences(); + $all_prefs = $orig_prefs; + $all_prefs[\GaletteObjectsLend\Entity\Preferences::PARAM_VIEW_DESCRIPTION] = 0; + $this->assertTrue($this->lend_prefs->store($all_prefs)); + + //only one result (first in name only) + $this->assertCount(1, $objects->getObjectsList(true)); + $this->assertSame(1, $objects->getCount()); + + //reset prefs + $this->assertTrue($this->lend_prefs->store($orig_prefs)); + + $filters->field_filter = \GaletteObjectsLend\Repository\Objects::FILTER_ID; + $filters->filter_str = (string)$third_object_id; + $this->assertCount(1, $objects->getObjectsList(true)); + $this->assertSame(1, $objects->getCount()); + + $filters->filter_str = '42'; + $this->assertCount(0, $objects->getObjectsList(true)); + $this->assertSame(0, $objects->getCount()); + + $filters->field_filter = \GaletteObjectsLend\Repository\Objects::FILTER_SERIAL; + $filters->filter_str = 'ABCDE'; + $this->assertCount(1, $objects->getObjectsList(true)); + $this->assertSame(1, $objects->getCount()); + + $filters->field_filter = \GaletteObjectsLend\Repository\Objects::FILTER_DIM; + $filters->filter_str = '297'; + $this->assertCount(1, $objects->getObjectsList(true)); + $this->assertSame(1, $objects->getCount()); + + //quick test ordering + $filters->reinit(); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_NAME; + $this->assertCount(4, $objects->getObjectsList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_SERIAL; + $this->assertCount(4, $objects->getObjectsList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_PRICE; + $this->assertCount(4, $objects->getObjectsList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_RENTPRICE; + $this->assertCount(4, $objects->getObjectsList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_WEIGHT; + $this->assertCount(4, $objects->getObjectsList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_STATUS; + $this->assertCount(4, $objects->getObjectsList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_BDATE; + $this->assertCount(4, $objects->getObjectsList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_FDATE; + $this->assertCount(4, $objects->getObjectsList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_MEMBER; + $this->assertCount(4, $objects->getObjectsList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Objects::ORDERBY_CATEGORY; + $this->assertCount(4, $objects->getObjectsList(true)); + + $this->assertTrue($objects->removeObjects([$first_object_id, $second_object_id, $third_object_id])); + $this->assertCount(1, $objects->getObjectsList(true)); + } +} diff --git a/tests/GaletteObjectsLend/Repository/tests/units/Status.php b/tests/GaletteObjectsLend/Repository/tests/units/Status.php new file mode 100644 index 0000000..bf106fb --- /dev/null +++ b/tests/GaletteObjectsLend/Repository/tests/units/Status.php @@ -0,0 +1,159 @@ +. + */ + +namespace GaletteObjectsLends\Repository\tests\units; + +use Galette\GaletteTestCase; + +/** + * Categories tests + * + * @author Johan Cwiklinski + */ +class Status extends GaletteTestCase +{ + protected int $seed = 20240526084251; + + protected \Galette\Core\Plugins $plugins; + + /** + * Set up tests + * + * @return void + */ + public function setUp(): void + { + parent::setUp(); + $this->plugins = $this->container->get('plugins'); + } + + /** + * Cleanup after each test method + * + * @return void + */ + public function tearDown(): void + { + $delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\LendStatus::TABLE); + $this->zdb->execute($delete); + + parent::tearDown(); + } + + /** + * Test getList + * + * @return void + */ + public function testGetList(): void + { + $status = new \GaletteObjectsLend\Repository\Status($this->zdb, $this->login); + + $rs_list = $status->getList(); + $this->assertInstanceOf(\Laminas\Db\ResultSet\ResultSet::class, $rs_list); + $this->assertSame(0, $rs_list->count()); + $this->assertSame([], $status->getList(true)); + $this->assertNull($status->getCount()); + + $this->assertSame([], $status->getStatusList(true)); + $this->assertSame(0, $status->getCount()); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One active in stock status'; + $status->in_stock = true; + $status->is_active = true; + $this->assertTrue($status->store()); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One active in stock status'; + $status->in_stock = true; + $status->is_active = true; + $this->assertTrue($status->store()); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One active not in stock status'; + $status->in_stock = false; + $status->is_active = true; + $this->assertTrue($status->store()); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One inactive in stock status'; + $status->in_stock = true; + $status->is_active = false; + $this->assertTrue($status->store()); + + $status = new \GaletteObjectsLend\Entity\LendStatus($this->zdb); + $status->status_text = 'One inactive not in stock status'; + $status->in_stock = false; + $status->is_active = false; + $this->assertTrue($status->store()); + + $filters = new \GaletteObjectsLend\Filters\StatusList(); + $status = new \GaletteObjectsLend\Repository\Status($this->zdb, $this->login, $filters); + + $this->assertCount(5, $status->getStatusList(true)); + $this->assertSame(5, $status->getCount()); + + $filters->filter_str = 'one'; + $this->assertCount(5, $status->getStatusList(true)); + + $filters->filter_str = 'not'; + $this->assertCount(2, $status->getStatusList(true)); + + $filters->filter_str = 'noone'; + $this->assertCount(0, $status->getStatusList(true)); + + $filters->reinit(); + $filters->active_filter = \GaletteObjectsLend\Repository\Status::ACTIVE; + $this->assertCount(3, $status->getStatusList(true)); + + $filters->active_filter = \GaletteObjectsLend\Repository\Status::INACTIVE; + $this->assertCount(2, $status->getStatusList(true)); + + $filters->reinit(); + $filters->stock_filter = \GaletteObjectsLend\Repository\Status::DC_STOCK; + $this->assertCount(5, $status->getStatusList(true)); + + $filters->stock_filter = \GaletteObjectsLend\Repository\Status::IN_STOCK; + $this->assertCount(3, $status->getStatusList(true)); + + $filters->stock_filter = \GaletteObjectsLend\Repository\Status::OUT_STOCK; + $this->assertCount(2, $status->getStatusList(true)); + + //quick test ordering + $filters->reinit(); + + $filters->orderby = \GaletteObjectsLend\Repository\Status::ORDERBY_ID; + $this->assertCount(5, $status->getStatusList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Status::ORDERBY_NAME; + $this->assertCount(5, $status->getStatusList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Status::ORDERBY_ACTIVE; + $this->assertCount(5, $status->getStatusList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Status::ORDERBY_STOCK; + $this->assertCount(5, $status->getStatusList(true)); + + $filters->orderby = \GaletteObjectsLend\Repository\Status::ORDERBY_RENTDAYS; + $this->assertCount(5, $status->getStatusList(true)); + } +} diff --git a/tests/TestsBootstrap.php b/tests/TestsBootstrap.php new file mode 100644 index 0000000..aaef719 --- /dev/null +++ b/tests/TestsBootstrap.php @@ -0,0 +1,32 @@ +. + */ + +/** + * Bootstrap tests file for Galette ObjectsLend plugin + * + * @author Johan Cwiklinski + */ + +define('GALETTE_PLUGINS_PATH', __DIR__ . '/../../'); +$basepath = '../../../galette/'; + +include_once '../../../tests/TestsBootstrap.php'; +require_once __DIR__ . '/../_config.inc.php';