|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This code is licensed under the BSD 3-Clause License. |
| 4 | + * |
| 5 | + * Copyright (c) 2017, Maks Rafalko |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * * Redistributions of source code must retain the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * * Neither the name of the copyright holder nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived from |
| 20 | + * this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 26 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 30 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + */ |
| 33 | + |
| 34 | +declare(strict_types=1); |
| 35 | + |
| 36 | +namespace Infection\Tests\TestFramework\PhpSpec\Adapter; |
| 37 | + |
| 38 | +use Infection\TestFramework\PhpSpec\VersionParser; |
| 39 | +use InvalidArgumentException; |
| 40 | +use PHPUnit\Framework\TestCase; |
| 41 | + |
| 42 | +final class VersionParserTest extends TestCase |
| 43 | +{ |
| 44 | + private VersionParser $versionParser; |
| 45 | + |
| 46 | + protected function setUp(): void |
| 47 | + { |
| 48 | + $this->versionParser = new VersionParser(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dataProvider versionProvider |
| 53 | + */ |
| 54 | + public function test_it_parses_version_from_string(string $content, string $expectedVersion): void |
| 55 | + { |
| 56 | + $result = $this->versionParser->parse($content); |
| 57 | + |
| 58 | + $this->assertSame($expectedVersion, $result); |
| 59 | + } |
| 60 | + |
| 61 | + public function test_it_throws_exception_when_content_has_no_version_substring(): void |
| 62 | + { |
| 63 | + try { |
| 64 | + $this->versionParser->parse('abc'); |
| 65 | + |
| 66 | + $this->fail(); |
| 67 | + } catch (InvalidArgumentException $exception) { |
| 68 | + $this->assertSame( |
| 69 | + 'Parameter does not contain a valid SemVer (sub)string.', |
| 70 | + $exception->getMessage() |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return iterable<string, array<array-key, string>> |
| 77 | + */ |
| 78 | + public function versionProvider(): iterable |
| 79 | + { |
| 80 | + yield 'nominal stable' => ['7.0.2', '7.0.2']; |
| 81 | + |
| 82 | + yield 'nominal development' => ['0.2.8', '0.2.8']; |
| 83 | + |
| 84 | + yield 'stable variant' => ['v7.0.2', '7.0.2']; |
| 85 | + |
| 86 | + yield 'development variant' => ['v0.2.8', '0.2.8']; |
| 87 | + |
| 88 | + yield 'patch' => ['7.0.2-patch', '7.0.2-patch']; |
| 89 | + |
| 90 | + yield 'versioned patch' => ['7.0.2-patch.0', '7.0.2-patch.0']; |
| 91 | + |
| 92 | + yield 'RC' => ['7.0.2-rc', '7.0.2-rc']; |
| 93 | + |
| 94 | + yield 'uppercase RC' => ['7.0.2-RC', '7.0.2-RC']; |
| 95 | + |
| 96 | + yield 'versioned RC' => ['7.0.2-rc.0', '7.0.2-rc.0']; |
| 97 | + |
| 98 | + yield 'with spaces' => [' 7.0.2 ', '7.0.2']; |
| 99 | + |
| 100 | + yield 'nonsense suffix 0' => ['7.0.2foo', '7.0.2']; |
| 101 | + |
| 102 | + yield 'nonsense suffix 1' => ['7.0.2-foo', '7.0.2-foo']; |
| 103 | + |
| 104 | + yield 'Hoa' => ['3.17.05.02', '3.17.05']; |
| 105 | + |
| 106 | + yield 'phpspec stable' => ['phpspec 7.1.0', '7.1.0']; |
| 107 | + |
| 108 | + yield 'phpspec RC' => ['phpspec version 5.0.0-rc1', '5.0.0-rc1']; |
| 109 | + } |
| 110 | +} |
0 commit comments