-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
phpunit tests: WriterFactory and Validator
- Loading branch information
1 parent
32a5831
commit 3652624
Showing
12 changed files
with
284 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keboola\DbWriter\Tests; | ||
|
||
use Generator; | ||
use Keboola\DbWriter\Exception\ApplicationException; | ||
use Keboola\DbWriter\Exception\InvalidDatabaseHostException; | ||
use Keboola\DbWriter\Validator; | ||
use Keboola\DbWriterConfig\Config; | ||
use Keboola\DbWriterConfig\Configuration\ActionConfigDefinition; | ||
use Keboola\Temp\Temp; | ||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\Test\TestLogger; | ||
|
||
class ValidatorTest extends TestCase | ||
{ | ||
public function testValidHostname(): void | ||
{ | ||
$config = new Config($this->getConfig(), new ActionConfigDefinition()); | ||
|
||
$validator = new Validator(new TestLogger()); | ||
$validator->validateDatabaseHost($config); | ||
|
||
Assert::assertTrue(true); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidHostnameProvider | ||
*/ | ||
public function testInvalidHostname(array $config, string $expectedMessage): void | ||
{ | ||
$config = new Config($config, new ActionConfigDefinition()); | ||
$validator = new Validator(new TestLogger()); | ||
|
||
$this->expectException(InvalidDatabaseHostException::class); | ||
$this->expectExceptionMessage($expectedMessage); | ||
$validator->validateDatabaseHost($config); | ||
} | ||
|
||
/** | ||
* @dataProvider validateTableItemsProvider | ||
*/ | ||
public function testValidateTableItems(array $items, array $expectedItems): void | ||
{ | ||
$manifest = [ | ||
'columns' => ['test', 'test2'], | ||
]; | ||
|
||
$temp = new Temp(); | ||
$file = $temp->createFile('table.csv.manifest'); | ||
file_put_contents($file->getPathname(), json_encode($manifest)); | ||
|
||
$validator = new Validator(new TestLogger()); | ||
$result = $validator->validateTableItems( | ||
substr($file->getPathname(), 0, -9), | ||
$items, | ||
); | ||
|
||
Assert::assertEquals($expectedItems, $result); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidTableItemsProvider | ||
*/ | ||
public function testInvalidTableItems( | ||
?array $manifestContent, | ||
bool $encodeJsonContent, | ||
string $expectedExceptionMessage, | ||
): void { | ||
if ($manifestContent) { | ||
$temp = new Temp(); | ||
$expectedExceptionMessage = sprintf( | ||
$expectedExceptionMessage, | ||
$temp->getTmpFolder(), | ||
); | ||
$file = $temp->createFile('table.csv.manifest'); | ||
file_put_contents( | ||
$file->getPathname(), | ||
$encodeJsonContent ? json_encode($manifestContent) : $manifestContent, | ||
); | ||
$tablePath = substr($file->getPathname(), 0, -9); | ||
} else { | ||
$tablePath = 'table.csv'; | ||
} | ||
|
||
$validator = new Validator(new TestLogger()); | ||
|
||
$this->expectException(ApplicationException::class); | ||
$this->expectExceptionMessage($expectedExceptionMessage); | ||
$validator->validateTableItems($tablePath, []); | ||
} | ||
|
||
private function getConfig(): array | ||
{ | ||
return [ | ||
'parameters' => [ | ||
'data_dir' => '/data/', | ||
'writer_class' => 'Common', | ||
'db' => [ | ||
'host' => 'localhost', | ||
'port' => '3306', | ||
'database' => 'test', | ||
'user' => 'root', | ||
'#password' => 'root', | ||
], | ||
], | ||
'image_parameters' => [ | ||
'approvedHostnames' => [ | ||
[ | ||
'host' => 'localhost', | ||
'port' => '3306', | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function invalidHostnameProvider(): Generator | ||
{ | ||
$config = $this->getConfig(); | ||
$config['image_parameters']['approvedHostnames'][0]['host'] = 'wrongHost'; | ||
|
||
yield 'wrongHost' => [ | ||
$config, | ||
'Hostname "localhost" with port "3306" is not approved.', | ||
]; | ||
|
||
$config['image_parameters']['approvedHostnames'][0]['port'] = 'wrongPort'; | ||
yield 'wrongPort' => [ | ||
$config, | ||
'Hostname "localhost" with port "3306" is not approved.', | ||
]; | ||
|
||
$config['image_parameters']['approvedHostnames'] = []; | ||
yield 'emptyImageParamsArray' => [ | ||
$config, | ||
'Hostname "localhost" with port "3306" is not approved.', | ||
]; | ||
} | ||
|
||
public function validateTableItemsProvider(): Generator | ||
{ | ||
$baseItems = [ | ||
[ | ||
'name' => 'test', | ||
'type' => 'varchar', | ||
'size' => '255', | ||
], | ||
[ | ||
'name' => 'test2', | ||
'type' => 'varchar', | ||
'size' => '255', | ||
], | ||
]; | ||
yield 'valid' => [ | ||
$baseItems, | ||
$baseItems, | ||
]; | ||
|
||
yield 'reorderItems' => [ | ||
array_reverse($baseItems), | ||
$baseItems, | ||
]; | ||
} | ||
|
||
public function invalidTableItemsProvider(): Generator | ||
{ | ||
yield 'manifestNotExists' => [ | ||
null, | ||
true, | ||
'Manifest "table.csv.manifest" not found.', | ||
]; | ||
|
||
yield 'manifestNotValidJson' => [ | ||
['test'], | ||
false, | ||
'Manifest "%s/table.csv.manifest" is not valid JSON.', | ||
]; | ||
|
||
yield 'manifestMissingColumns' => [ | ||
['test'], | ||
true, | ||
'Manifest "%s/table.csv.manifest" is missing "columns" key.', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keboola\DbWriter\Tests; | ||
|
||
use Keboola\DbWriter\Exception\UserException; | ||
use Keboola\DbWriter\WriterFactory; | ||
use Keboola\DbWriterConfig\Config; | ||
use Keboola\DbWriterConfig\Configuration\ActionConfigDefinition; | ||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\Test\TestLogger; | ||
|
||
class WriterFactoryTest extends TestCase | ||
{ | ||
public function testCreateWriterClass(): void | ||
{ | ||
$config = new Config( | ||
[ | ||
'action' => 'testConnection', | ||
'parameters' => [ | ||
'data_dir' => '/data', | ||
'writer_class' => 'Common', | ||
'db' => [ | ||
'host' => (string) getenv('COMMON_DB_HOST'), | ||
'port' => (int) getenv('COMMON_DB_PORT'), | ||
'database' => (string) getenv('COMMON_DB_DATABASE'), | ||
'user' => (string) getenv('COMMON_DB_USER'), | ||
'#password' => (string) getenv('COMMON_DB_PASSWORD'), | ||
], | ||
], | ||
], | ||
new ActionConfigDefinition(), | ||
); | ||
|
||
$writerFactory = new WriterFactory($config); | ||
$writer = $writerFactory->create(new TestLogger()); | ||
|
||
Assert::assertSame('Keboola\DbWriter\Writer\Common', get_class($writer)); | ||
} | ||
|
||
public function testUnknownWriterClass(): void | ||
{ | ||
$config = new Config( | ||
[ | ||
'action' => 'testConnection', | ||
'parameters' => [ | ||
'data_dir' => '/data', | ||
'writer_class' => 'Unknown', | ||
'db' => [ | ||
'host' => (string) getenv('COMMON_DB_HOST'), | ||
'port' => (int) getenv('COMMON_DB_PORT'), | ||
'database' => (string) getenv('COMMON_DB_DATABASE'), | ||
'user' => (string) getenv('COMMON_DB_USER'), | ||
'#password' => (string) getenv('COMMON_DB_PASSWORD'), | ||
], | ||
], | ||
], | ||
new ActionConfigDefinition(), | ||
); | ||
|
||
$writerFactory = new WriterFactory($config); | ||
$this->expectException(UserException::class); | ||
$this->expectExceptionMessage("Writer class 'Keboola\DbWriter\Writer\Unknown' doesn't exist"); | ||
$writerFactory->create(new TestLogger()); | ||
} | ||
} |
Oops, something went wrong.