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

Extract exceptions assembling from the code into custom Exceptions #263

Merged
merged 1 commit into from Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions src-tests/Component/LegacyTest.php
Expand Up @@ -4,12 +4,13 @@

use Lmc\Steward\Component\Fixtures\StringableObject;
use Lmc\Steward\ConfigHelper;
use Lmc\Steward\Exception\LegacyComponentException;
use Lmc\Steward\Test\AbstractTestCase;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Steward\Component\Legacy
* @covers \Lmc\Steward\Component\LegacyException
* @covers \Lmc\Steward\Exception\LegacyComponentException
*/
class LegacyTest extends TestCase
{
Expand All @@ -35,7 +36,7 @@ public function testShouldThrowExceptionIfLegacyFileNotFound(): void
$legacy = new Legacy($this->testCase);
$this->expectOutputRegex('/.*New Legacy instantiated.*/');

$this->expectException(LegacyException::class);
$this->expectException(LegacyComponentException::class);
$this->expectExceptionMessage('Cannot read Legacy file');

$legacy->loadWithName('not-existing');
Expand All @@ -50,7 +51,7 @@ public function testShouldFailIfNotUnserializableFileFound(): void
$legacy->setFileDir(sys_get_temp_dir());
$this->expectOutputRegex('/.*New Legacy instantiated.*/');

$this->expectException(LegacyException::class);
$this->expectException(LegacyComponentException::class);
$this->expectExceptionMessage('Cannot parse Legacy from file');

$legacy->loadWithName('wrong');
Expand Down Expand Up @@ -98,7 +99,7 @@ public function testShouldFailIfSavingToNotExistingDirectory(): void

$this->expectOutputRegex('/.*Saving data as Legacy "baz" to file "\/notexisting\/baz\.legacy".*/');

$this->expectException(LegacyException::class);
$this->expectException(LegacyComponentException::class);
$this->expectExceptionMessage('Cannot save Legacy to file /notexisting/baz.legacy');

$legacy->saveWithName([], 'baz');
Expand Down Expand Up @@ -146,7 +147,7 @@ public function testShouldFailIfTryingToAutomaticallySaveLegacyIfTestDoesntHaveP
$legacy = new Legacy($testCasePhase1);
$legacy->setFileDir(sys_get_temp_dir());

$this->expectException(LegacyException::class);
$this->expectException(LegacyComponentException::class);
$this->expectExceptionMessage(
'Cannot generate Legacy name from class without \'Phase\' followed by number in name'
);
Expand Down Expand Up @@ -223,11 +224,11 @@ public function testShouldAutomaticallySaveAndLoadLegacyForMethodsWithSameNameIn

try {
$legacy2Method2->load(Legacy::LEGACY_TYPE_TEST);
} catch (LegacyException $e) {
$this->assertContains('Cannot read Legacy file', $e->getMessage());
} catch (LegacyComponentException $e) {
$this->assertStringContainsString('Cannot read Legacy file', $e->getMessage());

return;
}
$this->fail('Expected exception LegacyException not thrown when loading Legacy that should not exists');
$this->fail('Expected exception LegacyComponentException not thrown when loading Legacy that should not exists');
}
}
4 changes: 3 additions & 1 deletion src-tests/Console/Command/GenerateTimelineCommandTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Lmc\Steward\Console\Command;

use Lmc\Steward\Console\Application;
use Lmc\Steward\Exception\CommandException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
Expand All @@ -11,6 +12,7 @@

/**
* @covers \Lmc\Steward\Console\Command\GenerateTimelineCommand
* @covers \Lmc\Steward\Exception\CommandException
*/
class GenerateTimelineCommandTest extends TestCase
{
Expand All @@ -33,7 +35,7 @@ protected function setUp(): void

public function testShouldShowErrorIfResultsFileCannotBeFound(): void
{
$this->expectException(\RuntimeException::class);
$this->expectException(CommandException::class);
$this->expectExceptionMessage('Cannot read results file "/not/accessible.xml"');

$this->tester->execute(
Expand Down
4 changes: 2 additions & 2 deletions src-tests/Console/Command/InstallCommandTest.php
Expand Up @@ -6,11 +6,11 @@
use Lmc\Steward\Console\CommandEvents;
use Lmc\Steward\Console\Event\BasicConsoleEvent;
use Lmc\Steward\Console\Event\ExtendedConsoleEvent;
use Lmc\Steward\Exception\CommandException;
use Lmc\Steward\Selenium\Downloader;
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -149,7 +149,7 @@ public function testShouldThrowAnExceptionInNonInteractiveModeIfLastVersionCheck
$fileGetContentsMock->expects($this->any())
->willReturn(false);

$this->expectException(RuntimeException::class);
$this->expectException(CommandException::class);
$this->expectExceptionMessage('Auto-detection of latest Selenium version failed - version must be provided');

$this->tester->execute(
Expand Down
4 changes: 3 additions & 1 deletion src-tests/Console/Command/ResultsCommandTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Lmc\Steward\Console\Command;

use Lmc\Steward\Console\Application;
use Lmc\Steward\Exception\CommandException;
use Lmc\Steward\LineEndingsNormalizerTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -11,6 +12,7 @@

/**
* @covers \Lmc\Steward\Console\Command\ResultsCommand
* @covers \Lmc\Steward\Exception\CommandException
*/
class ResultsCommandTest extends TestCase
{
Expand All @@ -35,7 +37,7 @@ protected function setUp(): void

public function testShouldShowErrorIfResultsFileCannotBeFound(): void
{
$this->expectException(\RuntimeException::class);
$this->expectException(CommandException::class);
$this->expectExceptionMessage('Cannot read results file "/not/accessible.xml"');

$this->tester->execute(
Expand Down
1 change: 1 addition & 0 deletions src-tests/Console/Command/RunCommandIntegrationTest.php
Expand Up @@ -12,6 +12,7 @@
* Run command tests that require real Selenium server to execute the tests.
*
* @covers \Lmc\Steward\Console\Command\RunCommand
* @covers \Lmc\Steward\Exception\RuntimeException
* @covers \Lmc\Steward\Listener\WebDriverListener
* @covers \Lmc\Steward\Process\ExecutionLoop
* @group integration
Expand Down
4 changes: 3 additions & 1 deletion src-tests/Console/Command/RunCommandTest.php
Expand Up @@ -7,6 +7,7 @@
use Lmc\Steward\Console\CommandEvents;
use Lmc\Steward\Console\Event\BasicConsoleEvent;
use Lmc\Steward\Console\Event\ExtendedConsoleEvent;
use Lmc\Steward\Exception\CommandException;
use Lmc\Steward\Process\ProcessSet;
use Lmc\Steward\Process\ProcessSetCreator;
use Lmc\Steward\Process\ProcessWrapper;
Expand All @@ -20,6 +21,7 @@

/**
* @covers \Lmc\Steward\Console\Command\RunCommand
* @covers \Lmc\Steward\Exception\CommandException
* @covers \Lmc\Steward\Process\ExecutionLoop
*/
class RunCommandTest extends TestCase
Expand Down Expand Up @@ -145,7 +147,7 @@ public function testShouldThrowExceptionOnlyIfUnsupportedBrowserSelected(
$this->command->setSeleniumAdapter($seleniumAdapterMock);

if ($shouldThrowException) {
$this->expectException(\RuntimeException::class);
$this->expectException(CommandException::class);
$this->expectExceptionMessage('Browser "' . $browserName . '" is not supported');
}

Expand Down
12 changes: 8 additions & 4 deletions src-tests/Process/ProcessSetCreatorTest.php
Expand Up @@ -7,6 +7,8 @@
use Lmc\Steward\Console\Configuration\ConfigOptions;
use Lmc\Steward\Console\Configuration\ConfigResolver;
use Lmc\Steward\Console\Event\RunTestsProcessEvent;
use Lmc\Steward\Exception\LogicException;
use Lmc\Steward\Exception\RuntimeException;
use Lmc\Steward\Process\Fixtures\DelayedTests\DelayedByZeroTimeTest;
use Lmc\Steward\Process\Fixtures\DelayedTests\DelayedTest;
use Lmc\Steward\Process\Fixtures\DelayedTests\FirstTest;
Expand All @@ -24,6 +26,8 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @covers \Lmc\Steward\Exception\LogicException
* @covers \Lmc\Steward\Exception\RuntimeException
* @covers \Lmc\Steward\Process\ProcessSetCreator
*/
class ProcessSetCreatorTest extends TestCase
Expand Down Expand Up @@ -124,7 +128,7 @@ public function testShouldThrowExceptionIfAddingClassWithNameMismatchingTheFileN
{
$files = $this->findDummyTests('WrongClassTest.php', 'InvalidTests');

$this->expectException(\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageRegExp(
'/Error loading class "Lmc\\\\Steward\\\\Process\\\\Fixtures\\\\InvalidTests\\\\ReallyWrongClassTest"'
. ' from file ".*WrongClassTest.php"/'
Expand Down Expand Up @@ -213,10 +217,10 @@ public function testShouldThrowExceptionIfAddingTestWithDelayTimeButWithoutDelay
{
$files = $this->findDummyTests('InvalidDelayTest.php', 'InvalidTests');

$this->expectException(\InvalidArgumentException::class);
$this->expectException(LogicException::class);
$this->expectExceptionMessage(
'Testcase "Lmc\Steward\Process\Fixtures\InvalidTests\InvalidDelayTest" has defined delay 5 minutes, '
. 'but doesn\'t have defined the testcase to run after'
'Testcase "Lmc\Steward\Process\Fixtures\InvalidTests\InvalidDelayTest" has defined @delayMinutes 5 minutes, '
. 'but doesn\'t have defined the testcase to run after using @delayAfter'
);

$this->creator->createFromFiles($files, [], []);
Expand Down
10 changes: 6 additions & 4 deletions src-tests/Process/ProcessSetTest.php
Expand Up @@ -4,6 +4,8 @@

use Assert\InvalidArgumentException;
use Graphp\Algorithms\Tree\OutTree;
use Lmc\Steward\Exception\LogicException;
use Lmc\Steward\Exception\RuntimeException;
use Lmc\Steward\Process\Fixtures\MockOrderStrategy;
use Lmc\Steward\Publisher\XmlPublisher;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -33,7 +35,7 @@ public function testShouldFailWhenAddingTestWithNonUniqueName(): void
{
$this->set->add(new ProcessWrapper(new Process(''), 'Foo\Bar'));

$this->expectException(\InvalidArgumentException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Testcase with name "Foo\Bar" was already added');

$this->set->add(new ProcessWrapper(new Process(''), 'Foo\Bar'));
Expand Down Expand Up @@ -201,9 +203,9 @@ public function testShouldFailBuildingTreeIfTestHasDependencyOnNotExistingTest()

$this->set->add($process);

$this->expectException(\InvalidArgumentException::class);
$this->expectException(LogicException::class);
$this->expectExceptionMessage(
'Testcase "Foo" has @delayAfter dependency on "NotExisting", but this testcase was not defined.'
'Testcase "Foo" has @delayAfter dependency on "NotExisting", but this testcase is not defined.'
);
$this->set->buildTree();
}
Expand All @@ -224,7 +226,7 @@ public function testShouldFailBuildingTreeIfCycleDetected(): void
$this->set->add($processA);
$this->set->add($processB);

$this->expectException(\InvalidArgumentException::class);
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Cannot build tree graph from tests dependencies.');

$this->set->buildTree();
Expand Down
12 changes: 8 additions & 4 deletions src-tests/Process/ProcessWrapperTest.php
Expand Up @@ -72,15 +72,17 @@ public function provideInvalidDelay(): array
return [
'negative value' => [
-5,
'Delay defined in testcase "Foo" must be greater than or equal 0, but "-5" was given',
'Delay defined in testcase "Foo" using @delayMinutes must be greater than or equal 0, '
. 'but "-5" was given',
],
'string value' => [
'omg',
'Delay defined in testcase "Foo" must be greater than or equal 0, but "omg" was given',
'Delay defined in testcase "Foo" using @delayMinutes must be greater than or equal 0, '
. 'but "omg" was given',
],
'empty value' => [
'',
'Delay defined in testcase "Foo" must be greater than or equal 0, but "" was given',
'Delay defined in testcase "Foo" using @delayMinutes must be greater than or equal 0, but "" was given',
],
];
}
Expand All @@ -90,7 +92,9 @@ public function testShouldFailIfDependencyWasDefinedButWithoutDelay(): void
$wrapper = new ProcessWrapper(new Process(''), 'Foo');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Testcase "Foo" should run after "Bar", but no delay was defined');
$this->expectExceptionMessage(
'Testcase "Foo" should run after "Bar", but no delay was defined using @delayMinutes'
);

$wrapper->setDelay('Bar', null);
}
Expand Down
10 changes: 7 additions & 3 deletions src-tests/Utils/Annotations/ClassParserTest.php
Expand Up @@ -2,11 +2,13 @@

namespace Lmc\Utils\Annotations;

use Lmc\Steward\Exception\RuntimeException;
use Lmc\Steward\Utils\Annotations\ClassParser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\SplFileInfo;

/**
* @covers \Lmc\Steward\Exception\RuntimeException
* @covers \Lmc\Steward\Utils\Annotations\ClassParser
*/
class ClassParserTest extends TestCase
Expand All @@ -24,16 +26,18 @@ public function testShouldThrowExceptionForMultipleClassesInOneFile(): void
{
$file = $this->createFileInfo('MultipleClassesInFile.php');

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/^File ".+MultipleClassesInFile.php" contains definition of 2 classes\./');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageRegExp(
'/^File ".+MultipleClassesInFile.php" contains definition of 2 classes\./'
);
ClassParser::readClassNameFromFile($file);
}

public function testShouldThrowExceptionForNoClassInOneFile(): void
{
$file = $this->createFileInfo('NoClassInFile.php');

$this->expectException(\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageRegExp('/^No class found in file ".+NoClassInFile.php"/');
ClassParser::readClassNameFromFile($file);
}
Expand Down