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

Enhance expect exception tests #86

Merged
merged 3 commits into from Oct 29, 2019
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
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -22,7 +22,7 @@
"require-dev": {
"phpmd/phpmd": "@stable",
"phpstan/phpstan": "^0.11",
"phpunit/phpunit": "^6.2",
"phpunit/phpunit": "^7.2",
"squizlabs/php_codesniffer": "^3.0"
},
"bin": [
Expand Down
11 changes: 4 additions & 7 deletions src/Test/Unit/Config/Dist/GeneratorTest.php
Expand Up @@ -49,7 +49,7 @@ class GeneratorTest extends TestCase
/**
* @inheritdoc
*/
protected function setUp()
protected function setUp(): void
{
$this->directoryListMock = $this->createMock(DirectoryList::class);
$this->filesystemMock = $this->createMock(Filesystem::class);
Expand Down Expand Up @@ -138,14 +138,11 @@ private function getConfigForUpdate(): string
TEXT;
}

/**
* @expectedExceptionMessage file system error
* @expectedException \Magento\CloudDocker\App\ConfigurationMismatchException
*
* @throws ConfigurationMismatchException
*/
public function testGenerateFileSystemException()
{
$this->expectException(ConfigurationMismatchException::class);
$this->expectExceptionMessage('file system error');

$this->filesystemMock->expects($this->once())
->method('put')
->willThrowException(new ConfigurationMismatchException('file system error'));
Expand Down
2 changes: 1 addition & 1 deletion src/Test/Unit/Config/Environment/ConverterTest.php
Expand Up @@ -23,7 +23,7 @@ class ConverterTest extends TestCase
/**
* @inheritDoc
*/
protected function setUp()
protected function setUp(): void
{
$this->converter = new Converter();
}
Expand Down
10 changes: 4 additions & 6 deletions src/Test/Unit/Config/Environment/ReaderTest.php
Expand Up @@ -37,7 +37,7 @@ class ReaderTest extends TestCase
/**
* @inheritDoc
*/
protected function setUp()
protected function setUp(): void
{
$this->directoryListMock = $this->createMock(DirectoryList::class);
$this->filesystemMock = $this->createMock(Filesystem::class);
Expand Down Expand Up @@ -101,13 +101,11 @@ public function testExecuteUsingDist()
$this->reader->read();
}

/**
* @expectedException \Magento\CloudDocker\Filesystem\FilesystemException
* @expectedExceptionMessage Source file docker_root/config.php.dist does not exists
* @throws FilesystemException
*/
public function testExecuteNoSource()
{
$this->expectException(FilesystemException::class);
$this->expectExceptionMessage('Source file docker_root/config.php.dist does not exists');

$this->directoryListMock->method('getDockerRoot')
->willReturn('docker_root');
$this->filesystemMock->expects($this->exactly(2))
Expand Down
47 changes: 16 additions & 31 deletions src/Test/Unit/Config/ReaderTest.php
Expand Up @@ -38,7 +38,7 @@ class ReaderTest extends TestCase
/**
* @inheritDoc
*/
protected function setUp()
protected function setUp(): void
{
$this->fileListMock = $this->createMock(FileList::class);
$this->filesystemMock = $this->createMock(Filesystem::class);
Expand All @@ -54,29 +54,23 @@ protected function setUp()
);
}

/**
* @expectedException \Magento\CloudDocker\Filesystem\FilesystemException
* @expectedExceptionMessage PHP version could not be parsed.
*
* @throws FilesystemException
*/
public function testReadEmpty()
{
$this->expectException(FilesystemException::class);
$this->expectExceptionMessage('PHP version could not be parsed.');

$this->filesystemMock->expects($this->exactly(2))
->method('get')
->willReturn(Yaml::dump([]));

$this->reader->read();
}

/**
* @expectedException \Magento\CloudDocker\Filesystem\FilesystemException
* @expectedExceptionMessage Relationships could not be parsed.
*
* @throws FileSystemException
*/
public function testReadWithPhp()
{
$this->expectException(FilesystemException::class);
$this->expectExceptionMessage('Relationships could not be parsed.');

$this->filesystemMock->expects($this->exactly(2))
->method('get')
->willReturnMap([
Expand All @@ -87,14 +81,11 @@ public function testReadWithPhp()
$this->reader->read();
}

/**
* @expectedExceptionMessage Only one instance of service "elasticsearch" supported
* @expectedException \Magento\CloudDocker\Filesystem\FilesystemException
*
* @throws FileSystemException
*/
public function testReadWithMultipleSameServices()
{
$this->expectException(FilesystemException::class);
$this->expectExceptionMessage('Only one instance of service "elasticsearch" supported');

$this->filesystemMock->expects($this->exactly(2))
->method('get')
->willReturnMap([
Expand Down Expand Up @@ -154,14 +145,11 @@ public function testReadWithMultipleSameServices()
], $this->reader->read());
}

/**
* @expectedExceptionMessage Service with name "myrabbitmq" could not be parsed
* @expectedException \Magento\CloudDocker\Filesystem\FilesystemException
*
* @throws FileSystemException
*/
public function testReadWithMissedService()
{
$this->expectException(FilesystemException::class);
$this->expectExceptionMessage('Service with name "myrabbitmq" could not be parsed');

$this->filesystemMock->expects($this->exactly(2))
->method('get')
->willReturnMap([
Expand Down Expand Up @@ -196,14 +184,11 @@ public function testReadWithMissedService()
$this->reader->read();
}

/**
* @expectedException \Magento\CloudDocker\Filesystem\FilesystemException
* @expectedExceptionMessage Some error
*
* @throws FileSystemException
*/
public function testReadBroken()
{
$this->expectException(FilesystemException::class);
$this->expectExceptionMessage('Some error');

$this->fileListMock->expects($this->once())
->method('getAppConfig')
->willThrowException(new \Exception('Some error'));
Expand Down
9 changes: 4 additions & 5 deletions src/Test/Unit/Config/RelationshipTest.php
Expand Up @@ -31,7 +31,7 @@ class RelationshipTest extends TestCase
/**
* @inheritdoc
*/
protected function setUp()
protected function setUp(): void
{
$this->configMock = $this->createMock(Config::class);

Expand Down Expand Up @@ -59,12 +59,11 @@ public function testGet()
$this->assertArrayHasKey('rabbitmq', $relationships);
}

/**
* @expectedExceptionMessage Configuration error
* @expectedException \Magento\CloudDocker\App\ConfigurationMismatchException
*/
public function testGetWithException()
{
$this->expectException(ConfigurationMismatchException::class);
$this->expectExceptionMessage('Configuration error');

$this->configMock->expects($this->any())
->method('getServiceVersion')
->willThrowException(new ConfigurationMismatchException('Configuration error'));
Expand Down
41 changes: 16 additions & 25 deletions src/Test/Unit/Service/ConfigTest.php
Expand Up @@ -34,7 +34,7 @@ class ConfigTest extends TestCase
/**
* @inheritdoc
*/
protected function setUp()
protected function setUp(): void
{
$this->readerMock = $this->createMock(Reader::class);

Expand Down Expand Up @@ -89,24 +89,21 @@ public function testGetServiceVersionFromConfig(array $config, string $serviceNa
$this->assertEquals($result, $this->version->getServiceVersion($serviceName));
}

/**
* @expectedException \Magento\CloudDocker\App\ConfigurationMismatchException
* @expectedExceptionMessage Type "notphp" is not supported
*/
public function testGetServiceVersionFromConfigException()
{
$this->expectException(ConfigurationMismatchException::class);
$this->expectExceptionMessage('Type "notphp" is not supported');

$this->readerMock->expects($this->once())
->method('read')
->willReturn(['type' => 'notphp:1']);
$this->version->getServiceVersion(ServiceInterface::NAME_PHP);
}

/**
* @throws ConfigurationMismatchException
* @expectedException \Magento\CloudDocker\App\ConfigurationMismatchException
*/
public function testGetServiceVersionException()
{
$this->expectException(ConfigurationMismatchException::class);

$exception = new FilesystemException('reader exception');
$this->readerMock->expects($this->once())
->method('read')
Expand All @@ -129,37 +126,33 @@ public function testGetPhpVersion(array $config, string $result)
$this->assertEquals($result, $this->version->getPhpVersion());
}

/**
* @expectedException \Magento\CloudDocker\App\ConfigurationMismatchException
* @expectedExceptionMessage Some exception
*/
public function testGetPhpVersionReaderException()
{
$this->expectException(ConfigurationMismatchException::class);
$this->expectExceptionMessage('Some exception');

$exception = new ConfigurationMismatchException('Some exception');
$this->readerMock->expects($this->once())
->method('read')
->willThrowException($exception);
$this->version->getPhpVersion();
}

/**
* @expectedException \Magento\CloudDocker\App\ConfigurationMismatchException
* @expectedExceptionMessage Type "notphp" is not supported
*/
public function testGetPhpVersionWrongType()
{
$this->expectException(ConfigurationMismatchException::class);
$this->expectExceptionMessage('Type "notphp" is not supported');

$this->readerMock->expects($this->once())
->method('read')
->willReturn(['type' => 'notphp:7.1']);
$this->version->getPhpVersion();
}

/**
* @throws ConfigurationMismatchException
* @expectedException \Magento\CloudDocker\App\ConfigurationMismatchException
*/
public function testGetPhpVersionException()
{
$this->expectException(ConfigurationMismatchException::class);

$exception = new FileSystemException('reader exception');
$this->readerMock->expects($this->once())
->method('read')
Expand All @@ -182,12 +175,10 @@ public function testGetCron($config, $result)
$this->assertEquals($result, $this->version->getCron());
}

/**
* @throws ConfigurationMismatchException
* @expectedException \Magento\CloudDocker\App\ConfigurationMismatchException
*/
public function testGetCronException()
{
$this->expectException(ConfigurationMismatchException::class);

$exception = new FileSystemException('reader exception');
$this->readerMock->expects($this->once())
->method('read')
Expand Down
11 changes: 4 additions & 7 deletions src/Test/Unit/Service/ServiceFactoryTest.php
Expand Up @@ -31,7 +31,7 @@ class ServiceFactoryTest extends TestCase
/**
* @inheritdoc
*/
protected function setUp()
protected function setUp(): void
{
$this->fileListMock = $this->createMock(FileList::class);

Expand All @@ -51,14 +51,11 @@ public function testCreate()
$this->factory->create(ServiceFactory::SERVICE_CLI, '7.0');
}

/**
* @expectedExceptionMessage Service "test" is not supported
* @expectedException \Magento\CloudDocker\App\ConfigurationMismatchException
*
* @throws ConfigurationMismatchException
*/
public function testCreateServiceNotSupported()
{
$this->expectException(ConfigurationMismatchException::class);
$this->expectExceptionMessage('Service "test" is not supported');

$this->factory->create('test', '5.6');
}
}