Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

New Exceptions System #60

Merged
merged 65 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
2b1f52b
Add new PHP attribute for 'Throws'
toddy15 Sep 6, 2021
04b9ad8
Add new VotingHasStartedException
toddy15 Sep 6, 2021
1853e38
Implement new VotingHasStartedException
toddy15 Sep 6, 2021
71eb76d
Add test for VotingHasStartedException
toddy15 Sep 6, 2021
99c77a8
Rename and adapt exception test
toddy15 Sep 8, 2021
a2b958c
Merge pull request #59 from toddy15/rebuild-exception-system
julien-boudry Sep 9, 2021
d7d496b
Add CandidateExistsException
toddy15 Sep 9, 2021
e476f72
Add CandidateDoesNotExistException
toddy15 Sep 9, 2021
2748aed
Add CandidateInvalidNameException
toddy15 Sep 9, 2021
7829796
Add VoteInvalidFormatException
toddy15 Sep 9, 2021
42d4dbd
Add ResultRequestedWithoutVotesException
toddy15 Sep 10, 2021
b780bcf
Remove unused exception code
toddy15 Sep 10, 2021
eae78e3
Add AlgorithmException
toddy15 Sep 10, 2021
b0d2b4c
Rebuild Exceptions: New base class CondorcetPublicApiException
julien-boudry Sep 10, 2021
22f7a79
Fix use Throwable (Candidate Process)
julien-boudry Sep 10, 2021
c75819d
CondorcetPublicApiException ==> abstract class
julien-boudry Sep 10, 2021
f357935
Rebuild Exceptions: Message System
julien-boudry Sep 10, 2021
9ad0f60
Remove unnecessary 'use' statements.
toddy15 Sep 11, 2021
44f583a
Rename test file to match the class name
toddy15 Sep 11, 2021
b4c93d6
Add PHPStorm's directory .idea to .gitignore
toddy15 Sep 11, 2021
3f94400
Remove generic testcases for new exceptions
toddy15 Sep 12, 2021
1f5d7e7
Fix PHPDoc comment
toddy15 Sep 12, 2021
f069ece
Add expectation for exception messages to tests
toddy15 Sep 12, 2021
a73bdf7
Minor refactoring to be able to return the name of the invalid voting…
toddy15 Sep 12, 2021
aa20f3f
Add VoteMaxNumberReachedException
toddy15 Sep 12, 2021
f18c6b5
Add JsonFormatException
julien-boudry Sep 13, 2021
6624aa5
Add VoteConstraintException
toddy15 Sep 13, 2021
26c1a87
Use existing CandidateExistsException for previous exception code 19
toddy15 Sep 13, 2021
86471fc
Use CandidateDoesNotExistException for previous exception code 32
toddy15 Sep 13, 2021
2e3ba88
Use VoteInvalidFormatException for previous exception code 17
toddy15 Sep 13, 2021
f1ab98c
Use VoteInvalidFormatException for previous exception code 13 and 14
toddy15 Sep 13, 2021
cc4f105
Use VoteMaxNumberReachedException for previous exception code 16
toddy15 Sep 13, 2021
8d5cb58
Add VoteNotLinkedException
toddy15 Sep 13, 2021
21e238d
Use VoteInvalidFormatException for previous exception code 21
toddy15 Sep 13, 2021
21ffee4
Use VoteInvalidFormatException for previous exception code 18
toddy15 Sep 13, 2021
659a810
Add ElectionObjectVersionMismatchException
toddy15 Sep 13, 2021
94a096b
Add serialized test data for an election object
toddy15 Sep 13, 2021
9cb77fb
Fix version comparison error and add a new test
toddy15 Sep 13, 2021
681837f
Fix testElectionUnserializing for next releases
julien-boudry Sep 14, 2021
e5eea9b
Add ElectionObjectVersionMismatchException
julien-boudry Sep 14, 2021
4e73cce
Improve CondorcetPublicException
julien-boudry Sep 14, 2021
a78583d
Add StvQuotaNotImplementedException
julien-boudry Sep 14, 2021
950e314
Add NoCandidatesException
toddy15 Sep 14, 2021
beb11b9
Use AlgorithmException for previous exception code 25
toddy15 Sep 14, 2021
13e768a
Add DataHandlerException
toddy15 Sep 14, 2021
6f69fda
Use VoteInvalidFormatException for previous exception code 26
toddy15 Sep 14, 2021
4101a49
Add VoteException
toddy15 Sep 14, 2021
61e352b
Add NoSeatsException
toddy15 Sep 14, 2021
f9e8e51
Funny: the exception about a non-existing entity does not exist in th…
toddy15 Sep 14, 2021
d8c62c7
Add CandidatesMaxNumberReachedException
toddy15 Sep 14, 2021
1ff8bbb
Add ResultException
toddy15 Sep 14, 2021
e8537db
Merge branch 'dev-3.2' into WORK/newExceptions
julien-boudry Sep 14, 2021
cd1b5e1
Merge branch 'WORK/newExceptions' of github.com:julien-boudry/Condorc…
julien-boudry Sep 14, 2021
3891328
Add TimerException
toddy15 Sep 14, 2021
8cd4452
Add VoteManagerException
toddy15 Sep 14, 2021
5f5786a
Switch Dockerfile to PHP 8.1 RC
toddy15 Sep 14, 2021
d6d9dba
Fix ElectionTest::testMaxVoteNumber() last assertion
julien-boudry Sep 14, 2021
e152e37
Use DataHandlerException
toddy15 Sep 14, 2021
4c029e3
Remove old CondorcetException
toddy15 Sep 14, 2021
f7e87d3
Remove unused use statements of CondorcetException
toddy15 Sep 14, 2021
92912e9
Remove more unused CondorcetException
toddy15 Sep 14, 2021
3633f72
Remove last bits of old CondorcetException
toddy15 Sep 14, 2021
d2bdbfc
Better check for ElectionTest::testAmbiguousCandidatesOnVoteSide
julien-boudry Sep 15, 2021
f91d2b4
Readme: Remove note about rebuilding Exception
julien-boudry Sep 15, 2021
b5e9ebd
Generate Documentation
julien-boudry Sep 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ lib/Algo/Methods/KemenyYoung-Data/10.data
build/
*.phar
Dev/large.votes

# phpstorm
.idea
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
Condorcet PHP - Election manager and results calculator.
Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.

By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt)
https://github.com/julien-boudry/Condorcet
*/
declare(strict_types=1);

namespace CondorcetPHP\Condorcet\Dev\CondorcetDocumentationGenerator\CondorcetDocAttributes;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
class Throws
{
public array $exceptionList;

public function __construct(string ...$exceptionList)
{
$this->exceptionList = $exceptionList;
}
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:8.0-cli-bullseye
FROM php:8.1-rc-cli-bullseye

COPY . /usr/src/condorcetapp
WORKDIR /usr/src/condorcetapp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A valid class path. Class must extend VoteConstraint class.

### Return value:

*(bool)* True on success. Throw Throwable\CondorcetException code 27/28/29 on error.
*(bool)* True on success.


---------------------------------------
Expand Down
370 changes: 339 additions & 31 deletions Documentation/README.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,6 @@ _Benchmark on a modern machine (linux - x64 - php 8.0 - cli)._

## Roadmap for further releases

- Rebuild Exception System
- **Research reference librarians !!**


Expand Down
8 changes: 5 additions & 3 deletions Tests/lib/Algo/Methods/CondorcetBasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace CondorcetPHP\Condorcet\Tests\Algo\Methods;

use CondorcetPHP\Condorcet\{Candidate, Condorcet, CondorcetUtil, Election, Result, Vote, VoteConstraint};
use CondorcetPHP\Condorcet\Algo\Methods\CondorcetBasic;
use CondorcetPHP\Condorcet\Throwable\AlgorithmWithoutRankingFeatureException;
use PHPUnit\Framework\TestCase;

class CondorcetBasicTest extends TestCase
Expand Down Expand Up @@ -126,8 +128,8 @@ public function testResult_6 (): void

public function testNoResultObject (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(102);
$this->expectException(AlgorithmWithoutRankingFeatureException::class);
$this->expectExceptionMessage("This algortihm can't provide a full ranking (but only Winner and Loser): ".CondorcetBasic::METHOD_NAME[0]);

$this->election->addCandidate('A');
$this->election->addCandidate('B');
Expand All @@ -140,7 +142,7 @@ public function testNoResultObject (): never
A > C > L
');

$this->election->getResult(\CondorcetPHP\Condorcet\Algo\Methods\CondorcetBasic::class);
$this->election->getResult(CondorcetBasic::class);
}

}
6 changes: 3 additions & 3 deletions Tests/lib/Algo/Methods/KemenyYoung/KemenyYoungTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use CondorcetPHP\Condorcet\{Candidate, Condorcet, CondorcetUtil, Election, Result, Vote, VoteConstraint};
use CondorcetPHP\Condorcet\Algo\Methods\KemenyYoung\KemenyYoung;
use CondorcetPHP\Condorcet\Throwable\CandidatesMaxNumberReachedException;
use PHPUnit\Framework\TestCase;

class KemenyYoungTest extends TestCase
Expand Down Expand Up @@ -77,9 +78,8 @@ public function testResult2 (): void

public function testMaxCandidates (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(101);
$this->expectExceptionMessage('Kemeny–Young is configured to accept only 8 candidates');
$this->expectException(CandidatesMaxNumberReachedException::class);
$this->expectExceptionMessage("Maximum number of candidates reached: The method 'Kemeny–Young' is configured to accept only 8 candidates");

for ($i=0; $i < 10; $i++) :
$this->election->addCandidate();
Expand Down
6 changes: 3 additions & 3 deletions Tests/lib/Algo/Methods/RankedPairs/RankedPairsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace CondorcetPHP\Condorcet\Tests\Algo\Methods\RankedPairs;

use CondorcetPHP\Condorcet\{Candidate, Condorcet, CondorcetUtil, Election, Result, Vote, VoteConstraint};
use CondorcetPHP\Condorcet\Throwable\CandidatesMaxNumberReachedException;
use PHPUnit\Framework\TestCase;

class RankedPairsTest extends TestCase
Expand Down Expand Up @@ -388,9 +389,8 @@ public function testResult_11 (): void

public function testMaxCandidates (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(101);
$this->expectExceptionMessage('Ranked Pairs Winning is configured to accept only 40 candidates');
$this->expectException(CandidatesMaxNumberReachedException::class);
$this->expectExceptionMessage("Maximum number of candidates reached: The method 'Ranked Pairs Winning' is configured to accept only 40 candidates");

for ($i=0; $i < 41; $i++) :
$this->election->addCandidate();
Expand Down
9 changes: 4 additions & 5 deletions Tests/lib/Algo/Methods/STV/SingleTransferableVoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace CondorcetPHP\Condorcet\Tests\Algo\STV;

use CondorcetPHP\Condorcet\{Candidate, Condorcet, CondorcetUtil, Election, Result, Vote, VoteConstraint};
use CondorcetPHP\Condorcet\Election;
use CondorcetPHP\Condorcet\Throwable\StvQuotaNotImplementedException;
use CondorcetPHP\Condorcet\Algo\Methods\STV\SingleTransferableVote;
use CondorcetPHP\Condorcet\Algo\Tools\StvQuotas;
use CondorcetPHP\Condorcet\Throwable\CondorcetException;
use PHPUnit\Framework\TestCase;

class SingleTransferableVoteTest extends TestCase
Expand All @@ -29,9 +29,8 @@ public function testQuotaOption (): never
$this->election->setMethodOption('STV', 'Quota', StvQuotas::make('Hagenbach-Bischoff'))
);

$this->expectException(CondorcetException::class);
$this->expectExceptionCode(103);
$this->expectExceptionMessage('This quota is not implemented.');
$this->expectException(StvQuotaNotImplementedException::class);
$this->expectExceptionMessage('This STV quota is not implemented: "another quota"');

$this->election->setMethodOption('STV', 'Quota', StvQuotas::make('another quota'));
$this->election->addCandidate('A');
Expand Down
35 changes: 18 additions & 17 deletions Tests/lib/CandidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace CondorcetPHP\Condorcet\Tests;

use CondorcetPHP\Condorcet\{Candidate, Condorcet, CondorcetUtil, Election, Result, Vote, VoteConstraint};
use CondorcetPHP\Condorcet\Throwable\{CandidateInvalidNameException, CandidateExistsException};
use PHPUnit\Framework\TestCase;

class CandidateTest extends TestCase
Expand Down Expand Up @@ -36,20 +37,20 @@ public function testTrimName (): void
self::assertSame('candidateName',(string) $candidate);
}

public function testToLongName (): never
public function testTooLongName (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(1);
$name = bin2hex(random_bytes(Election::MAX_LENGTH_CANDIDATE_ID + 42));

new Candidate (
bin2hex(random_bytes(Election::MAX_LENGTH_CANDIDATE_ID + 42))
);
$this->expectException(CandidateInvalidNameException::class);
$this->expectExceptionMessage("This name is not valid: $name");

new Candidate($name);
}

public function testBadName (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(1);
$this->expectException(CandidateInvalidNameException::class);
$this->expectExceptionMessage("This name is not valid");

new Candidate ('<$"');
}
Expand All @@ -63,8 +64,8 @@ public function testCandidateBadClass (): never

public function testAddSameCandidate1 (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(3);
$this->expectException(CandidateExistsException::class);
$this->expectExceptionMessage("This candidate already exists: Schizophrenic");

$election1 = new Election ();

Expand All @@ -76,8 +77,8 @@ public function testAddSameCandidate1 (): never

public function testAddSameCandidate2 (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(3);
$this->expectException(CandidateExistsException::class);
$this->expectExceptionMessage("This candidate already exists: candidate1");

$election1 = new Election ();

Expand All @@ -86,8 +87,8 @@ public function testAddSameCandidate2 (): never

public function testAddSameCandidate3 (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(3);
$this->expectException(CandidateExistsException::class);
$this->expectExceptionMessage("This candidate already exists: candidate1");

$election1 = new Election ();

Expand All @@ -110,8 +111,8 @@ public function testAddSameCandidate4 (): void

function testSameCandidateToMultipleElection ()
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(19);
$this->expectException(CandidateExistsException::class);
$this->expectExceptionMessage("This candidate already exists: the name 'Debussy' is taken by another candidate");

$election1 = new Election ();
$election2 = new Election ();
Expand All @@ -138,7 +139,7 @@ function testSameCandidateToMultipleElection ()
self::assertNotSame($this->candidate1, $election1->addCandidate('candidate1.n1'));

$election2->addCandidate('Debussy');
$this->candidate1->setName('Debussy'); // Throw an Exception. Code 19.
$this->candidate1->setName('Debussy');
}

public function testCloneCandidate(): void
Expand Down
42 changes: 0 additions & 42 deletions Tests/lib/CondorcetExceptionTest.php

This file was deleted.

17 changes: 9 additions & 8 deletions Tests/lib/CondorcetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use CondorcetPHP\Condorcet\{Candidate, Condorcet, CondorcetUtil, Election, Result, Vote, VoteConstraint};
use CondorcetPHP\Condorcet\Algo\{Method, MethodInterface};
use CondorcetPHP\Condorcet\Throwable\AlgorithmException;

use PHPUnit\Framework\TestCase;

Expand All @@ -27,8 +28,8 @@ public function testAddExistingMethod (): void

public function testBadClassMethod (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(9);
$this->expectException(AlgorithmException::class);
$this->expectExceptionMessage("The voting algorithm is not available: no class found for 'sjskkdlkkzksh'");

Condorcet::addMethod('sjskkdlkkzksh');
}
Expand All @@ -47,8 +48,8 @@ public function testAuthMethod (): void
*/
public function testAddMethod (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(25);
$this->expectException(AlgorithmException::class);
$this->expectExceptionMessage("The voting algorithm is not available: the given class is using an existing alias");

$algoClassPath = CondorcetTest_ValidAlgorithmName::class;

Expand All @@ -64,8 +65,8 @@ public function testAddMethod (): never

public function testAddUnvalidMethod (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(10);
$this->expectException(AlgorithmException::class);
$this->expectExceptionMessage("The voting algorithm is not available: the given class is not correct");

$algoClassPath = CondorcetTest_UnvalidAlgorithmName::class;

Expand All @@ -84,8 +85,8 @@ public function testUnvalidDefaultMethod (): void

public function testEmptyMethod (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(8);
$this->expectException(AlgorithmException::class);
$this->expectExceptionMessage("The voting algorithm is not available: no method name given");

Condorcet::isAuthMethod('');
}
Expand Down
5 changes: 3 additions & 2 deletions Tests/lib/Console/Commands/ElectionCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace CondorcetPHP\Condorcet\Tests\Console\Commands;

use CondorcetPHP\Condorcet\Throwable\ResultRequestedWithoutVotesException;
use PHPUnit\Framework\TestCase;
use CondorcetPHP\Condorcet\Console\CondorcetApplication;
use Symfony\Bundle\FrameworkBundle\Console\Application;
Expand Down Expand Up @@ -189,8 +190,8 @@ public function testInteractiveCommand (): void

public function testNonInteractionMode (): never
{
$this->expectException(\CondorcetPHP\Condorcet\Throwable\CondorcetException::class);
$this->expectExceptionCode(6);
$this->expectException(ResultRequestedWithoutVotesException::class);
$this->expectExceptionMessage("The result cannot be requested without votes");

$this->electionCommand->execute([],['interactive' => false]);

Expand Down
Loading