Skip to content

Commit

Permalink
#35 add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
digedag committed Oct 4, 2024
1 parent 29c73f3 commit 7ecfbb9
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 66 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/php.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,62 @@ jobs:
- "8.0"
- "8.1"
- "8.2"
php-unit:
name: "PHP Unit"

runs-on: "ubuntu-latest"

needs: ["php-lint", "php-cs-fixer"]

strategy:
fail-fast: false
matrix:
include:
-
typo3-version: "typo3/minimal:^9.5"
php-version: "7.2"
-
typo3-version: "typo3/minimal:^9.5"
php-version: "7.3"
-
typo3-version: "typo3/minimal:^9.5"
php-version: "7.4"
-
typo3-version: "typo3/minimal:^10.4"
php-version: "7.2"
-
typo3-version: "typo3/minimal:^10.4"
php-version: "7.3"
-
typo3-version: "typo3/minimal:^10.4"
php-version: "7.4"

steps:
-
name: "Checkout"
uses: "actions/checkout@v2"
-
name: "Setup PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"
extensions: "${{ env.PHP_EXTENSIONS }}"
-
name: "Determine composer cache directory"
id: "determine-composer-cache-directory"
run: "echo \"::set-output name=directory::$(composer config cache-dir)\""
-
name: "Cache dependencies installed with composer"
uses: "actions/[email protected]"
with:
path: "${{ steps.determine-composer-cache-directory.outputs.directory }}"
key: "php-${{ matrix.php-version }}-composer-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-"
-
name: "Install composer dependencies"
run: |
composer remove --no-update typo3/cms typo3/cms-core
composer require "${{ matrix.typo3-version }}"
-
name: "Run PHP unit tests"
run: "composer test:phpunit"
2 changes: 1 addition & 1 deletion Classes/Utility/StatsDataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function setType($type, $value)

public function getTypeValue($type)
{
return (int) $this->data[$type];
return (int) ($this->data[$type] ?? 0);
}

public function getTypeValues()
Expand Down
90 changes: 90 additions & 0 deletions Tests/Unit/PHP/Series/Rule/LostRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace System25\T3sports\Tests\Series\Rule;

use PHPUnit\Framework\Assert;
use Sys25\RnBase\Testing\BaseTestCase;
use System25\T3sports\Model\Fixture;
use System25\T3sports\Series\Rule\LostRule;
use System25\T3sports\Series\SeriesRuleInterface;
use System25\T3sports\Tests\StatsFixtureUtil;

/***************************************************************
* Copyright notice
*
* (c) 2008-2024 Rene Nitzsche ([email protected])
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project 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 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

class LostTest extends BaseTestCase
{
/** @var SeriesRuleInterface */
private $rule;
/** @var Fixture[] */
private $winsHome;
/** @var Fixture[] */
private $winsAway;
/** @var Fixture[] */
private $draws;

protected function setUp(): void
{
$this->rule = new LostRule();
$fixtures = StatsFixtureUtil::getMatches();
$this->winsHome = array_filter($fixtures, function (Fixture $f) { return $f->getGoalsHome() > $f->getGoalsGuest(); });
$this->winsAway = array_filter($fixtures, function (Fixture $f) { return $f->getGoalsHome() < $f->getGoalsGuest(); });
$this->draws = array_filter($fixtures, function (Fixture $f) { return $f->getGoalsHome() == $f->getGoalsGuest(); });
}

/**
* @group unit
*/
public function testIsSatisfiedWinHome()
{
$this->testIsSatisfied($this->winsHome, true, false);
$this->testIsSatisfied($this->winsHome, false, true);
}

/**
* @group unit
*/
public function testIsSatisfiedWinAway()
{
$this->testIsSatisfied($this->winsAway, true, true);
$this->testIsSatisfied($this->winsAway, false, false);
}

/**
* @group unit
*/
public function testIsSatisfiedDraw()
{
$this->testIsSatisfied($this->draws, true, false);
$this->testIsSatisfied($this->draws, false, false);
}

private function testIsSatisfied(array $fixtures, $isHome, bool $expected)
{
$config = '';
foreach ($fixtures as $fixture) {
$result = $this->rule->isSatified($fixture, $isHome, $config);
Assert::assertEquals($expected, $result);
}
}
}
90 changes: 90 additions & 0 deletions Tests/Unit/PHP/Series/Rule/NotLostRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace System25\T3sports\Tests\Series\Rule;

use PHPUnit\Framework\Assert;
use Sys25\RnBase\Testing\BaseTestCase;
use System25\T3sports\Model\Fixture;
use System25\T3sports\Series\Rule\NotLostRule;
use System25\T3sports\Series\SeriesRuleInterface;
use System25\T3sports\Tests\StatsFixtureUtil;

/***************************************************************
* Copyright notice
*
* (c) 2008-2024 Rene Nitzsche ([email protected])
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project 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 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

class NotLostTest extends BaseTestCase
{
/** @var SeriesRuleInterface */
private $rule;
/** @var Fixture[] */
private $winsHome;
/** @var Fixture[] */
private $winsAway;
/** @var Fixture[] */
private $draws;

protected function setUp(): void
{
$this->rule = new NotLostRule();
$fixtures = StatsFixtureUtil::getMatches();
$this->winsHome = array_filter($fixtures, function (Fixture $f) { return $f->getGoalsHome() > $f->getGoalsGuest(); });
$this->winsAway = array_filter($fixtures, function (Fixture $f) { return $f->getGoalsHome() < $f->getGoalsGuest(); });
$this->draws = array_filter($fixtures, function (Fixture $f) { return $f->getGoalsHome() == $f->getGoalsGuest(); });
}

/**
* @group unit
*/
public function testIsSatisfiedWinHome()
{
$this->testIsSatisfied($this->winsHome, true, true);
$this->testIsSatisfied($this->winsHome, false, false);
}

/**
* @group unit
*/
public function testIsSatisfiedWinAway()
{
$this->testIsSatisfied($this->winsAway, true, false);
$this->testIsSatisfied($this->winsAway, false, true);
}

/**
* @group unit
*/
public function testIsSatisfiedDraw()
{
$this->testIsSatisfied($this->draws, true, true);
$this->testIsSatisfied($this->draws, false, true);
}

private function testIsSatisfied(array $fixtures, $isHome, bool $expected)
{
$config = '';
foreach ($fixtures as $fixture) {
$result = $this->rule->isSatified($fixture, $isHome, $config);
Assert::assertEquals($expected, $result);
}
}
}
90 changes: 90 additions & 0 deletions Tests/Unit/PHP/Series/Rule/NotWonRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace System25\T3sports\Tests\Series\Rule;

use PHPUnit\Framework\Assert;
use Sys25\RnBase\Testing\BaseTestCase;
use System25\T3sports\Model\Fixture;
use System25\T3sports\Series\Rule\NotWonRule;
use System25\T3sports\Series\SeriesRuleInterface;
use System25\T3sports\Tests\StatsFixtureUtil;

/***************************************************************
* Copyright notice
*
* (c) 2008-2024 Rene Nitzsche ([email protected])
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project 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 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

class NotWonTest extends BaseTestCase
{
/** @var SeriesRuleInterface */
private $rule;
/** @var Fixture[] */
private $winsHome;
/** @var Fixture[] */
private $winsAway;
/** @var Fixture[] */
private $draws;

protected function setUp(): void
{
$this->rule = new NotWonRule();
$fixtures = StatsFixtureUtil::getMatches();
$this->winsHome = array_filter($fixtures, function (Fixture $f) { return $f->getGoalsHome() > $f->getGoalsGuest(); });
$this->winsAway = array_filter($fixtures, function (Fixture $f) { return $f->getGoalsHome() < $f->getGoalsGuest(); });
$this->draws = array_filter($fixtures, function (Fixture $f) { return $f->getGoalsHome() == $f->getGoalsGuest(); });
}

/**
* @group unit
*/
public function testIsSatisfiedWinHome()
{
$this->testIsSatisfied($this->winsHome, true, false);
$this->testIsSatisfied($this->winsHome, false, true);
}

/**
* @group unit
*/
public function testIsSatisfiedWinAway()
{
$this->testIsSatisfied($this->winsAway, true, true);
$this->testIsSatisfied($this->winsAway, false, false);
}

/**
* @group unit
*/
public function testIsSatisfiedDraw()
{
$this->testIsSatisfied($this->draws, true, true);
$this->testIsSatisfied($this->draws, false, true);
}

private function testIsSatisfied(array $fixtures, $isHome, bool $expected)
{
$config = '';
foreach ($fixtures as $fixture) {
$result = $this->rule->isSatified($fixture, $isHome, $config);
Assert::assertEquals($expected, $result);
}
}
}
Loading

0 comments on commit 7ecfbb9

Please sign in to comment.