Skip to content

Commit

Permalink
Only do file exists check for YML config (#542)
Browse files Browse the repository at this point in the history
* Only do file exists check for YML config
  • Loading branch information
cnizzardini authored Feb 25, 2024
1 parent 0668328 commit f840a6e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
10 changes: 4 additions & 6 deletions src/Lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,12 @@ public function setYml(string $yml)
}

$path = $this->root . $yml;
if ((!file_exists($path) && !is_writable($path)) || !touch($path)) {
if (!file_exists($path)) {
throw new InvalidArgumentException(
sprintf(
"Invalid yml: `%s`. yml must exist on the file system. An attempt was made to create %s, but
permission was denied or the file path is bad. Either fix the file system permissions, create the
file and/or both. $message",
$yml,
$path
'A YML file is required but none was found at "%s". See "%s" for a sample file.',
$path,
'$vendor/cnizzardini/cakephp-swagger-bake/assets/swagger.yml'
)
);
}
Expand Down
36 changes: 15 additions & 21 deletions tests/TestCase/Lib/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
declare(strict_types=1);

namespace SwaggerBake\Test\TestCase\Lib;

use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;
use LogicException;
use SwaggerBake\Lib\Configuration;
use SwaggerBake\Test\TestCase\Helper\ConfigurationHelperTrait;

Expand Down Expand Up @@ -36,8 +37,8 @@ public function test_get_set_docType(): void

public function test_property_does_not_exist_throws_logic_exception(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches("/Property nope does not exist/");
$this->expectException(LogicException::class);
$this->expectExceptionMessageMatches('/Property nope does not exist/');
$configs = array_merge(self::DEFAULT_CONFIGS, ['nope' => 'nope']);
new Configuration($configs, SWAGGER_BAKE_TEST_APP);
}
Expand All @@ -51,13 +52,6 @@ public function test_constructor_throws_exception_when_config_arg_missing_requir

/**
* @dataProvider dataProviderInvalidConfig
*
* @param string $property The config property
* @param mixed $value The value of the $property
* @param string $exceptionClass The expected exception
* @param string $exceptionMsg A string the exception message should contain
*
* @return void
*/
public function test_invalid_configs_should_throw_exceptions_with_invalid_data(
string $property,
Expand All @@ -66,7 +60,7 @@ public function test_invalid_configs_should_throw_exceptions_with_invalid_data(
string $exceptionMsg
): void {
if (!class_exists($exceptionClass)) {
throw new \InvalidArgumentException("$exceptionClass is not a class. Test cannot be run.");
throw new InvalidArgumentException("$exceptionClass is not a class. Test cannot be run.");
}
$this->expectException($exceptionClass);
$this->expectExceptionMessageMatches("/$exceptionMsg/");
Expand All @@ -85,15 +79,15 @@ public static function dataProviderInvalidConfig(): array
$invalidPath = '/' . '..' . DS . '..' . DS . '..' . DS . '..' . DS . '..' . DS . '..' . DS . '..' . DS . '..' . DS;

return [
['prefix', 'nope', \InvalidArgumentException::class, 'Invalid prefix'],
['yml', 'nope', \InvalidArgumentException::class, 'Value should start with'],
['yml', $invalidPath . 'nope', \InvalidArgumentException::class, 'yml must exist on the file'],
['json', 'nope', \InvalidArgumentException::class, 'Value should start with'],
['json', $invalidPath . 'nope', \InvalidArgumentException::class, 'Config value for `json` must exist'],
['webPath', 'nope', \InvalidArgumentException::class, 'Invalid webPath'],
['docType', 'nope', \InvalidArgumentException::class, 'Invalid docType'],
['editActionMethods', ['nope'], \InvalidArgumentException::class, 'Invalid editActionMethod'],
['connectionName', 'nope', \InvalidArgumentException::class, 'Invalid connectionName'],
['prefix', 'nope', InvalidArgumentException::class, 'Invalid prefix'],
['yml', 'nope', InvalidArgumentException::class, 'Value should start with'],
['yml', $invalidPath . 'nope', InvalidArgumentException::class, 'A YML file is required but none was found'],
['json', 'nope', InvalidArgumentException::class, 'Value should start with'],
['json', $invalidPath . 'nope', InvalidArgumentException::class, 'Config value for `json` must exist'],
['webPath', 'nope', InvalidArgumentException::class, 'Invalid webPath'],
['docType', 'nope', InvalidArgumentException::class, 'Invalid docType'],
['editActionMethods', ['nope'], InvalidArgumentException::class, 'Invalid editActionMethod'],
['connectionName', 'nope', InvalidArgumentException::class, 'Invalid connectionName'],
];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace SwaggerBake\Test\TestCase\Lib\Operation;

Expand All @@ -8,12 +9,10 @@
use SwaggerBake\Lib\Attribute\OpenApiResponse;
use SwaggerBake\Lib\Configuration;
use SwaggerBake\Lib\Exception\SwaggerBakeRunTimeException;
use SwaggerBake\Lib\SwaggerFactory;
use SwaggerBake\Lib\OpenApi\Schema;
use SwaggerBake\Lib\OpenApi\SchemaProperty;
use SwaggerBake\Lib\Operation\OperationResponseAssociation;
use SwaggerBake\Lib\Route\RouteDecorator;
use SwaggerBake\Lib\Route\RouteScanner;
use SwaggerBake\Lib\SwaggerFactory;

class OperationResponseAssociationTest extends TestCase
{
Expand All @@ -30,7 +29,7 @@ class OperationResponseAssociationTest extends TestCase
private Configuration $config;

/**
* @var RouteDecorator[]
* @var \SwaggerBake\Lib\Route\RouteDecorator[]
*/
private array $routes;

Expand All @@ -55,7 +54,7 @@ public function setUp(): void
'controllers' => ['\SwaggerBakeTest\App\\'],
'entities' => ['\SwaggerBakeTest\App\\'],
'tables' => ['\SwaggerBakeTest\App\\'],
]
],
], SWAGGER_BAKE_TEST_APP);

$cakeRoute = new RouteScanner(new Router(), $this->config);
Expand Down Expand Up @@ -182,7 +181,7 @@ public function test_associate_one(): void
associations: ['table' => 'EmployeeSalaries', 'whiteList' => ['Employees']]
));

/** @var SchemaProperty $schemaProperty */
/** @var \SwaggerBake\Lib\OpenApi\SchemaProperty $schemaProperty */
$schemaProperty = $schema->getProperties()['employee'];
$this->assertEquals('employee', $schemaProperty->getName());
}
Expand All @@ -200,4 +199,4 @@ public function test_associate_throws_exception_when_association_not_found(): vo
));
$this->expectExceptionMessageMatches('/OpenApiResponse association not found/');
}
}
}

0 comments on commit f840a6e

Please sign in to comment.