Skip to content

Commit

Permalink
Extract exceptions assembling from the code into custom Exceptions to…
Browse files Browse the repository at this point in the history
… clean it a bit
  • Loading branch information
OndraM committed Nov 30, 2020
1 parent 0dae025 commit 2881a2b
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 117 deletions.
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\RuntimeException;
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\RuntimeException
*/
class GenerateTimelineCommandTest extends TestCase
{
Expand All @@ -33,7 +35,7 @@ protected function setUp(): void

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

$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\RuntimeException;
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\RuntimeException
*/
class ResultsCommandTest extends TestCase
{
Expand All @@ -35,7 +37,7 @@ protected function setUp(): void

public function testShouldShowErrorIfResultsFileCannotBeFound(): void
{
$this->expectException(\RuntimeException::class);
$this->expectException(RuntimeException::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\RuntimeException;
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\RuntimeException
* @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(RuntimeException::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
19 changes: 10 additions & 9 deletions src/Component/Legacy.php
Expand Up @@ -53,6 +53,7 @@
namespace Lmc\Steward\Component;

use Lmc\Steward\ConfigProvider;
use Lmc\Steward\Exception\LegacyComponentException;
use Lmc\Steward\Test\AbstractTestCase;
use Lmc\Steward\Utils\Strings;

Expand Down Expand Up @@ -100,14 +101,14 @@ public function setFileDir(string $dir): void
*
* @param string $type LEGACY_TYPE_CASE (shared by all tests in test case)
* or LEGACY_TYPE_TEST (shared only by the same test function)
* @throws LegacyException
* @throws LegacyComponentException
*/
protected function getLegacyName(string $type): string
{
$name = $this->testClassName;

if (!preg_match('/Phase\d/', $name)) {
throw new LegacyException(
throw new LegacyComponentException(
"Cannot generate Legacy name from class without 'Phase' followed by number in name " . $name
);
}
Expand Down Expand Up @@ -136,7 +137,7 @@ protected function getLegacyFullPath(string $filename): string
* @param string $legacyName filename to store the data if null getLegacyFilename is called to generate filename
* from the test class name
* @param mixed $data
* @throws LegacyException
* @throws LegacyComponentException
*/
public function saveWithName($data, string $legacyName): void
{
Expand All @@ -145,7 +146,7 @@ public function saveWithName($data, string $legacyName): void
$this->debug('Legacy data: %s', $this->getPrintableValue($data));

if (@file_put_contents($filename, serialize($data)) === false) {
throw new LegacyException('Cannot save Legacy to file ' . $filename);
throw new LegacyComponentException('Cannot save Legacy to file ' . $filename);
}
}

Expand All @@ -155,7 +156,7 @@ public function saveWithName($data, string $legacyName): void
* @param string $type LEGACY_TYPE_CASE (shared by all tests in test case)
* or LEGACY_TYPE_TEST (shared only by the same test function)
* @param mixed $data
* @throws LegacyException
* @throws LegacyComponentException
*/
public function save($data, string $type = self::LEGACY_TYPE_CASE): void
{
Expand All @@ -168,7 +169,7 @@ public function save($data, string $type = self::LEGACY_TYPE_CASE): void
*
* @param string $type LEGACY_TYPE_CASE (shared by all tests in test case)
* or LEGACY_TYPE_TEST (shared only by the same test function)
* @throws LegacyException
* @throws LegacyComponentException
*/
public function load(string $type = self::LEGACY_TYPE_CASE)
{
Expand All @@ -180,7 +181,7 @@ public function load(string $type = self::LEGACY_TYPE_CASE)
* Raises exception if it is not found.
*
* @param string $legacyName filename to store the data from the test class name
* @throws LegacyException
* @throws LegacyComponentException
*/
public function loadWithName(string $legacyName)
{
Expand All @@ -190,12 +191,12 @@ public function loadWithName(string $legacyName)

$data = @file_get_contents($filename);
if ($data === false) {
throw new LegacyException('Cannot read Legacy file ' . $filename);
throw new LegacyComponentException('Cannot read Legacy file ' . $filename);
}

$legacy = unserialize($data);
if ($legacy === false) {
throw new LegacyException('Cannot parse Legacy from file ' . $filename);
throw new LegacyComponentException('Cannot parse Legacy from file ' . $filename);
}

$this->debug('Legacy data: %s', $this->getPrintableValue($legacy));
Expand Down
10 changes: 0 additions & 10 deletions src/Component/LegacyException.php

This file was deleted.

0 comments on commit 2881a2b

Please sign in to comment.