Skip to content

Commit

Permalink
Merge pull request #137 from TheoBiron/setter_resource_null
Browse files Browse the repository at this point in the history
Add the possibility to give a null parameter if the column of type resource is nullable
  • Loading branch information
moufmouf committed Jun 22, 2019
2 parents 1b9acd0 + ae47a0d commit 4573dd7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Expand Up @@ -11,6 +11,9 @@ parameters:
- "#expects array<string>, array<int, int|string> given.#"
- "/Parameter #. \\$types of method Doctrine\\\\DBAL\\\\Connection::.*() expects array<int|string>, array<int, Doctrine\\\\DBAL\\\\Types\\\\Type> given./"
- "#Method TheCodingMachine\\\\TDBM\\\\Schema\\\\ForeignKey::.*() should return .* but returns array<string>|string.#"
-
message: '#Result of && is always false.#'
path: src/Test/Dao/Bean/Generated/ArticleBaseBean.php
reportUnmatchedIgnoredErrors: false
includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
Expand Down
9 changes: 6 additions & 3 deletions src/Utils/ScalarBeanPropertyDescriptor.php
Expand Up @@ -210,13 +210,16 @@ public function getGetterSetterCode(): array

$resourceTypeCheck = '';
if ($normalizedType === 'resource') {
$checkNullable = '';
if ($isNullable) {
$checkNullable = sprintf('$%s !== null && ', $this->column->getName());
}
$resourceTypeCheck .= <<<EOF
if (!\is_resource($%s)) {
if (%s!\is_resource($%s)) {
throw \TheCodingMachine\TDBM\TDBMInvalidArgumentException::badType('resource', $%s, __METHOD__);
}
EOF;
$resourceTypeCheck = sprintf($resourceTypeCheck, $this->column->getName(), $this->column->getName());
$resourceTypeCheck = sprintf($resourceTypeCheck, $checkNullable, $this->column->getName(), $this->column->getName());
}


Expand Down
3 changes: 2 additions & 1 deletion tests/TDBMAbstractServiceTest.php
Expand Up @@ -350,7 +350,8 @@ private static function initSchema(Connection $connection): void
$db->table('article')
->column('id')->string(36)->primaryKey()->comment('@UUID')
->column('content')->string(255)
->column('author_id')->references('users')->null();
->column('author_id')->references('users')->null()
->column('attachment')->blob()->null();

$db->table('article2')
->column('id')->string(36)->primaryKey()->comment('@UUID v4')
Expand Down
16 changes: 16 additions & 0 deletions tests/TDBMDaoGeneratorTest.php
Expand Up @@ -1988,6 +1988,9 @@ public function testAddInterfaceOnDaoAnnotation()
$this->assertTrue($refClass->implementsInterface(TestUserDaoInterface::class));
}

/**
* @depends testDaoGeneration
*/
public function testTrait()
{
if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
Expand All @@ -2004,4 +2007,17 @@ public function testTrait()
$refClass = new ReflectionClass(UserBaseDao::class);
$this->assertTrue($refClass->hasMethod('findNothing'));
}

/**
* @depends testDaoGeneration
*/
public function testCanNullifyBlob()
{
$article = new ArticleBean('content');
$fp = fopen(__FILE__, 'r');
$article->setAttachment($fp);
$article->setAttachment(null);
$this->assertNull($article->getAttachment(null));
fclose($fp);
}
}
4 changes: 2 additions & 2 deletions vendor-bin/couscous/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor-bin/require-checker/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4573dd7

Please sign in to comment.