|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Keboola\TableBackendUtils\Unit\Connection\Bigquery; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use Keboola\TableBackendUtils\Connection\Bigquery\QueryTagKey; |
| 9 | +use Keboola\TableBackendUtils\Connection\Bigquery\QueryTags; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class QueryTagsTest extends TestCase |
| 13 | +{ |
| 14 | + public function testEmptyQueryTags(): void |
| 15 | + { |
| 16 | + $queryTags = new QueryTags(); |
| 17 | + $this->assertTrue($queryTags->isEmpty()); |
| 18 | + $this->assertEmpty($queryTags->toArray()); |
| 19 | + } |
| 20 | + |
| 21 | + public function testValidQueryTagsInConstructor(): void |
| 22 | + { |
| 23 | + $queryTags = new QueryTags([ |
| 24 | + QueryTagKey::BRANCH_ID->value => 'test-branch', |
| 25 | + ]); |
| 26 | + |
| 27 | + $this->assertFalse($queryTags->isEmpty()); |
| 28 | + $this->assertEquals( |
| 29 | + ['branch_id' => 'test-branch'], |
| 30 | + $queryTags->toArray(), |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function testValidQueryTagsAddition(): void |
| 35 | + { |
| 36 | + $queryTags = new QueryTags(); |
| 37 | + $queryTags->addTag(QueryTagKey::BRANCH_ID->value, 'test-branch'); |
| 38 | + |
| 39 | + $this->assertFalse($queryTags->isEmpty()); |
| 40 | + $this->assertEquals( |
| 41 | + ['branch_id' => 'test-branch'], |
| 42 | + $queryTags->toArray(), |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + public function testInvalidQueryTagsInConstructor(): void |
| 47 | + { |
| 48 | + $this->expectException(InvalidArgumentException::class); |
| 49 | + $this->expectExceptionMessage('Invalid query tag key "invalid_key". Valid keys are: branch_id'); |
| 50 | + |
| 51 | + new QueryTags([ |
| 52 | + 'invalid_key' => 'some-value', |
| 53 | + ]); |
| 54 | + } |
| 55 | + |
| 56 | + public function testInvalidQueryTagsAddition(): void |
| 57 | + { |
| 58 | + $this->expectException(InvalidArgumentException::class); |
| 59 | + $this->expectExceptionMessage('Invalid query tag key "invalid_key". Valid keys are: branch_id'); |
| 60 | + |
| 61 | + $queryTags = new QueryTags(); |
| 62 | + $queryTags->addTag('invalid_key', 'some-value'); |
| 63 | + } |
| 64 | + |
| 65 | + public function testChainedQueryTagsAddition(): void |
| 66 | + { |
| 67 | + $queryTags = new QueryTags(); |
| 68 | + $queryTags |
| 69 | + ->addTag(QueryTagKey::BRANCH_ID->value, 'test-branch-1') |
| 70 | + ->addTag(QueryTagKey::BRANCH_ID->value, 'test-branch-2'); // Override previous value |
| 71 | + |
| 72 | + $this->assertEquals( |
| 73 | + ['branch_id' => 'test-branch-2'], |
| 74 | + $queryTags->toArray(), |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @dataProvider validQueryTagsValuesProvider |
| 80 | + */ |
| 81 | + public function testValidQueryTagsValues(string $value): void |
| 82 | + { |
| 83 | + $queryTags = new QueryTags(); |
| 84 | + $queryTags->addTag(QueryTagKey::BRANCH_ID->value, $value); |
| 85 | + $this->assertEquals($value, $queryTags->toArray()[QueryTagKey::BRANCH_ID->value]); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return array<string, array{string}> |
| 90 | + */ |
| 91 | + public function validQueryTagsValuesProvider(): array |
| 92 | + { |
| 93 | + return [ |
| 94 | + 'empty value' => [''], |
| 95 | + 'simple value' => ['test'], |
| 96 | + 'with numbers' => ['test123'], |
| 97 | + 'with underscore' => ['test_branch'], |
| 98 | + 'with dash' => ['test-branch'], |
| 99 | + 'complex value' => ['my-test-branch-123'], |
| 100 | + 'single letter' => ['a'], |
| 101 | + 'starts with number' => ['123-test'], |
| 102 | + 'starts with underscore' => ['_test'], |
| 103 | + 'starts with dash' => ['-test'], |
| 104 | + 'international characters' => ['čeština-test'], |
| 105 | + 'international single char' => ['ě'], |
| 106 | + 'mixed international' => ['test-über-123'], |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @dataProvider invalidQueryTagsValuesProvider |
| 112 | + */ |
| 113 | + public function testInvalidQueryTagsValues(string $value, string $expectedMessage): void |
| 114 | + { |
| 115 | + $this->expectException(InvalidArgumentException::class); |
| 116 | + $this->expectExceptionMessage($expectedMessage); |
| 117 | + |
| 118 | + $queryTags = new QueryTags(); |
| 119 | + $queryTags->addTag(QueryTagKey::BRANCH_ID->value, $value); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return array<string, array{string, string}> |
| 124 | + */ |
| 125 | + public function invalidQueryTagsValuesProvider(): array |
| 126 | + { |
| 127 | + return [ |
| 128 | + 'uppercase letters' => [ |
| 129 | + 'Test-Branch', |
| 130 | + 'Invalid query tag value "Test-Branch" for key "branch_id". Values can only contain lowercase letters' |
| 131 | + .' (including international characters), numbers, underscores and dashes.', |
| 132 | + ], |
| 133 | + 'special characters' => [ |
| 134 | + 'test@branch', |
| 135 | + 'Invalid query tag value "test@branch" for key "branch_id". Values can only contain lowercase letters' |
| 136 | + .' (including international characters), numbers, underscores and dashes.', |
| 137 | + ], |
| 138 | + 'too long' => [ |
| 139 | + str_repeat('a', 64), |
| 140 | + 'Query tag value "' . str_repeat('a', 64) |
| 141 | + . '" for key "branch_id" is too long. Maximum length is 63 characters.', |
| 142 | + ], |
| 143 | + 'uppercase international' => [ |
| 144 | + 'ÜBER-test', |
| 145 | + 'Invalid query tag value "ÜBER-test" for key "branch_id". Values can only contain lowercase letters' |
| 146 | + .' (including international characters), numbers, underscores and dashes.', |
| 147 | + ], |
| 148 | + 'special international' => [ |
| 149 | + 'test-€-symbol', |
| 150 | + 'Invalid query tag value "test-€-symbol" for key "branch_id". Values can only contain lowercase letters' |
| 151 | + .' (including international characters), numbers, underscores and dashes.', |
| 152 | + ], |
| 153 | + ]; |
| 154 | + } |
| 155 | +} |
0 commit comments