Skip to content

Commit

Permalink
Add tests on object
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed May 23, 2024
1 parent 62941c4 commit c86e6db
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public function doEdit(Request $request, Response $response, int $id = null, str
$object->dimension = $post['dimension'];
if ($post['weight'] != '') {
//FIXME: better format handler
$object->weight = (int)str_replace(' ', '', str_replace(',', '.', $post['weight']));
$object->weight = (float)str_replace(' ', '', str_replace(',', '.', $post['weight']));
}
$object->is_active = ($post['is_active'] ?? false) == true;

Expand Down
45 changes: 25 additions & 20 deletions lib/GaletteObjectsLend/Entity/LendObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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')) {
Expand All @@ -272,16 +276,14 @@ 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) {
Expand Down Expand Up @@ -527,7 +529,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;
Expand Down Expand Up @@ -585,10 +587,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',
'<span class="search">$1</span>',
$this->$field
);
Expand Down Expand Up @@ -652,8 +652,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);
Expand Down Expand Up @@ -813,7 +818,7 @@ public function getIdAdh(): ?int
*/
public function getRentId(): ?int
{
return $this->rent_id;
return $this->rent_id ?? null;
}

/**
Expand Down Expand Up @@ -857,7 +862,7 @@ protected function getDateField(string $name): string
/**
* Generic isset function
*
* @param $name Property name
* @param string $name Property name
*
* @return bool
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/GaletteObjectsLend/Entity/LendRent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
256 changes: 256 additions & 0 deletions tests/GaletteObjectsLend/Entity/tests/units/LendObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<?php

/**
* Copyright © 2003-2024 The Galette Team
*
* This file is part of Galette (https://galette.eu).
*
* Galette is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Galette is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Galette. If not, see <http://www.gnu.org/licenses/>.
*/

namespace GaletteObjectsLends\tests\units;

use Galette\GaletteTestCase;

/**
* Status tests
*
* @author Johan Cwiklinski <johan@x-tnd.be>
*/
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;

$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('2024-05-22', $object->getDateBegin());
$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 <span class="search">object</span> (edited)', $object->displayName($filter));
$this->assertSame('An <span class="search">object</span> description', $object->displayDescription($filter));

$filter = new \GaletteObjectsLend\Filters\ObjectsList();
$filter->field_filter = \GaletteObjectsLend\Repository\Objects::FILTER_SERIAL;
$filter->filter_str = 'abc';
$this->assertSame('SE-<span class="search">aBc</span>-RI@L', $object->displaySerial($filter));

$filter = new \GaletteObjectsLend\Filters\ObjectsList();
$filter->field_filter = \GaletteObjectsLend\Repository\Objects::FILTER_DIM;
$filter->filter_str = '50';
$this->assertSame('10x<span class="search">50</span>', $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->assertTrue($object->store());
$object = new \GaletteObjectsLend\Entity\LendObject($this->zdb, $this->plugins, $oid, $deps);
$this->assertFalse($object->isActive());

//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);
}
}

0 comments on commit c86e6db

Please sign in to comment.