Skip to content

Commit

Permalink
Force typehint, add tests on category
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed May 21, 2024
1 parent 5b600fc commit f2d8f6f
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 41 deletions.
1 change: 1 addition & 0 deletions .composer-require-checker.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"GALETTE_PGSQL_MIN",
"GALETTE_DISPLAY_VERSION",
"GALETTE_PHP_MIN",
"GALETTE_TESTS",

"// Galette db constants (not detected as they are dynamically declared)",
"HOST_DB",
Expand Down
12 changes: 6 additions & 6 deletions lib/GaletteObjectsLend/Entity/LendCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LendCategory
'is_active' => 'boolean'
);
private int $category_id;
private string $name = '';
private ?string $name = null;
private bool $is_active = true;
private int $objects_nb = 0;
private float $objects_price_sum = 0.0;
Expand Down Expand Up @@ -121,9 +121,9 @@ 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;
Expand Down Expand Up @@ -300,11 +300,11 @@ public function isActive(): bool
/**
* Get picture
*
* @return CategoryPicture
* @return ?CategoryPicture
*/
public function getPicture(): CategoryPicture
public function getPicture(): ?CategoryPicture
{
return $this->picture;
return $this->picture ?? null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/GaletteObjectsLend/Entity/Picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down
116 changes: 116 additions & 0 deletions tests/GaletteObjectsLend/Entity/tests/units/LendCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?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;

/**
* Category tests
*
* @author Johan Cwiklinski <johan@x-tnd.be>
*/
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);
}
}
35 changes: 1 addition & 34 deletions tests/GaletteObjectsLend/Entity/tests/units/Preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,7 @@ class Preferences extends GaletteTestCase
protected int $seed = 20240519131740;

/**
* Cleanup after each test method
*
* @return void
*/
/*public function tearDown(): void
{
$delete = $this->zdb->delete(LEND_PREFIX . \GaletteObjectsLend\Entity\Preferences::TABLE);
$this->zdb->execute($delete);
parent::tearDown();
}*/

/**
* Test empty
* Test defaults
*
* @return void
*/
Expand Down Expand Up @@ -99,25 +87,4 @@ public function testCrud(): void

$this->assertTrue($prefs->store($orig_prefs));
}

/**
* Test load error
*
* @return void
*/
/*public function testLoadError(): void
{
$color = new \GaletteAuto\Color($this->zdb);
$this->assertFalse($color->load(999));
}*/

/**
* Test getClassName
*
* @return void
*/
/*public function testGetClassName(): void
{
$this->assertSame('\\' . \GaletteAuto\Color::class, \GaletteAuto\Color::getClassForPropName('color'));
}*/
}

0 comments on commit f2d8f6f

Please sign in to comment.