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

new interface for escaping strings and synapse implementation #21

Merged
merged 1 commit into from
May 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Escaping/QuoteInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Keboola\TableBackendUtils\Escaping;

interface QuoteInterface
{
public static function quote(string $value): string;

public static function quoteSingleIdentifier(string $str): string;
}
18 changes: 18 additions & 0 deletions src/Escaping/SynapseQuote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Keboola\TableBackendUtils\Escaping;

class SynapseQuote implements QuoteInterface
{
public static function quote(string $value): string
{
return "'" . str_replace("'", "''", $value) . "'";
}

public static function quoteSingleIdentifier(string $str): string
{
return '[' . str_replace(']', '][', $str) . ']';
tomasfejfar marked this conversation as resolved.
Show resolved Hide resolved
}
}
66 changes: 66 additions & 0 deletions tests/Unit/Escaping/SynapseQuoteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Tests\Keboola\TableBackendUtils\Unit\Escaping;

use Keboola\TableBackendUtils\Escaping\SynapseQuote;
use PHPUnit\Framework\TestCase;

class SynapseQuoteTest extends TestCase
{
/**
* @return \Generator<string, array<int, string>>
*/
public function valueQuoteProvider(): \Generator
{
yield 'simple' => [
'valueToQuote',
'\'valueToQuote\'',
];
yield 'with escaping char' => [
'value\'To\'Quote',
'\'value\'\'To\'\'Quote\'',
];
yield 'complex' => [
'va[l.u]e\']To\'[Q][uo.t[]e',
'\'va[l.u]e\'\']To\'\'[Q][uo.t[]e\'',
];
}

/**
* @dataProvider valueQuoteProvider
*/
public function testQuote(string $value, string $expectedOutput): void
{
self::assertSame($expectedOutput, SynapseQuote::quote($value));
}

/**
* @return \Generator<string, array<int, string>>
*/
public function valueQuoteIdentifierProvider(): \Generator
{
yield 'simple' => [
'valueToQuote',
'[valueToQuote]',
];
yield 'with escaping char' => [
'value\'To\'Quote',
'[value\'To\'Quote]',
];
yield 'complex' => [
'va[l.u]e\']To\'[Q][uo.t[]e',
'[va[l.u][e\'][To\'[Q][[uo.t[][e]',
];
}


/**
* @dataProvider valueQuoteIdentifierProvider
*/
public function testQuoteSingleIdentifier(string $value, string $expectedOutput): void
{
self::assertSame($expectedOutput, SynapseQuote::quoteSingleIdentifier($value));
}
}