Skip to content

Commit 045af48

Browse files
committed
Avoid using helpers
Helpers are tested with a separate job
1 parent c6fcdf4 commit 045af48

File tree

12 files changed

+109
-37
lines changed

12 files changed

+109
-37
lines changed

.github/workflows/test.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,38 @@ jobs:
4848
run: |
4949
composer global require php-coveralls/php-coveralls
5050
php-coveralls --coverage_clover=build/logs/clover.xml -v
51+
52+
test-helpers:
53+
runs-on: ubuntu-22.04
54+
steps:
55+
- name: Checkout
56+
uses: actions/checkout@v4
57+
- name: Install PHP
58+
uses: shivammathur/setup-php@v2
59+
with:
60+
coverage: xdebug
61+
php-version: "${{ matrix.php-version }}"
62+
ini-values: memory_limit=-1
63+
tools: composer:v2
64+
- name: Cache dependencies
65+
uses: actions/cache@v4
66+
with:
67+
path: |
68+
~/.composer/cache
69+
vendor
70+
key: "php-${{ matrix.php-version }}"
71+
restore-keys: "php-${{ matrix.php-version }}"
72+
73+
- name: Install dependencies
74+
run: composer install --no-interaction --no-progress
75+
76+
- name: Run PHPUnit
77+
run: make test-coveralls
78+
env:
79+
PHPUNIT_VERSION: "07-helpers"
80+
- name: Upload code coverage
81+
env:
82+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
83+
run: |
84+
composer global require php-coveralls/php-coveralls
85+
php-coveralls --coverage_clover=build/logs/clover.xml -v

lib/Inflections.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function __get(string $property)
178178
*/
179179
public function acronym(string $acronym): self
180180
{
181-
$this->acronyms[downcase($acronym)] = $acronym;
181+
$this->acronyms[StaticInflector::downcase($acronym)] = $acronym;
182182
$this->acronym_regex = '/' . implode('|', $this->acronyms) . '/';
183183

184184
return $this;
@@ -252,11 +252,11 @@ public function irregular(string $singular, string $plural): self
252252
unset($this->uncountables[$plural]);
253253

254254
$s0 = mb_substr($singular, 0, 1);
255-
$s0_upcase = upcase($s0);
255+
$s0_upcase = StaticInflector::upcase($s0);
256256
$srest = mb_substr($singular, 1);
257257

258258
$p0 = mb_substr($plural, 0, 1);
259-
$p0_upcase = upcase($p0);
259+
$p0_upcase = StaticInflector::upcase($p0);
260260
$prest = mb_substr($plural, 1);
261261

262262
if ($s0_upcase == $p0_upcase) {
@@ -266,8 +266,8 @@ public function irregular(string $singular, string $plural): self
266266
$this->singular("/({$s0}){$srest}$/i", '\1' . $srest);
267267
$this->singular("/({$p0}){$prest}$/i", '\1' . $srest);
268268
} else {
269-
$s0_downcase = downcase($s0);
270-
$p0_downcase = downcase($p0);
269+
$s0_downcase = StaticInflector::downcase($s0);
270+
$p0_downcase = StaticInflector::downcase($p0);
271271

272272
$this->plural("/{$s0_upcase}(?i){$srest}$/", $p0_upcase . $prest);
273273
$this->plural("/{$s0_downcase}(?i){$srest}$/", $p0_downcase . $prest);

lib/Inflector.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ public static function get(string $locale = self::DEFAULT_LOCALE): self
5858
?? self::$inflectors[$locale] = new self(Inflections::get($locale));
5959
}
6060

61+
private static function downcase(string $str): string
62+
{
63+
return StaticInflector::downcase($str);
64+
}
65+
66+
private static function upcase(string $str): string
67+
{
68+
return StaticInflector::upcase($str);
69+
}
70+
71+
private static function capitalize(string $str, bool $downcase_first_letter = false): string
72+
{
73+
return StaticInflector::capitalize($str, $downcase_first_letter);
74+
}
75+
6176
/**
6277
* Inflections used by the inflector.
6378
*
@@ -116,7 +131,7 @@ private function apply_inflections(string $word, array $rules): string
116131
return $rc;
117132
}
118133

119-
if (preg_match('/\b[[:word:]]+\Z/u', downcase($rc), $matches)) {
134+
if (preg_match('/\b[[:word:]]+\Z/u', self::downcase($rc), $matches)) {
120135
if (isset($this->inflections->uncountables[$matches[0]])) {
121136
return $rc;
122137
}
@@ -203,7 +218,7 @@ public function camelize(string $term, bool $downcase_first_letter = self::UPCAS
203218
. trim($this->inflections->acronym_regex, '/')
204219
. '(?=\b|[[:upper:]_])|\w)/u',
205220
function (array $matches): string {
206-
return downcase($matches[0]);
221+
return self::downcase($matches[0]);
207222
},
208223
$string,
209224
1
@@ -214,7 +229,7 @@ function (array $matches): string {
214229
function (array $matches) use ($acronyms): string {
215230
$m = $matches[0];
216231

217-
return !empty($acronyms[$m]) ? $acronyms[$m] : capitalize($m, true);
232+
return !empty($acronyms[$m]) ? $acronyms[$m] : self::capitalize($m, true);
218233
},
219234
$string,
220235
1
@@ -228,7 +243,7 @@ function (array $matches) use ($acronyms): string {
228243
function (array $matches) use ($acronyms): string {
229244
[ , $m1, $m2 ] = $matches;
230245

231-
return $m1 . ($acronyms[$m2] ?? capitalize($m2, true));
246+
return $m1 . ($acronyms[$m2] ?? self::capitalize($m2, true));
232247
},
233248
$string
234249
);
@@ -266,7 +281,7 @@ public function underscore(string $camel_cased_word): string
266281
function (array $matches): string {
267282
[ , $m1, $m2 ] = $matches;
268283

269-
return $m1 . ($m1 ? '_' : '') . downcase($m2);
284+
return $m1 . ($m1 ? '_' : '') . self::downcase($m2);
270285
},
271286
$word
272287
);
@@ -279,7 +294,7 @@ function (array $matches): string {
279294
$word = preg_replace('/\-+|\s+/', '_', $word);
280295

281296
// @phpstan-ignore-next-line
282-
return downcase($word);
297+
return self::downcase($word);
283298
}
284299

285300
/**
@@ -315,7 +330,7 @@ public function humanize(string $lower_case_and_underscored_word): string
315330
function (array $matches) use ($acronyms): string {
316331
[ $m ] = $matches;
317332

318-
return !empty($acronyms[$m]) ? $acronyms[$m] : downcase($m);
333+
return !empty($acronyms[$m]) ? $acronyms[$m] : self::downcase($m);
319334
},
320335
$result
321336
);
@@ -324,7 +339,7 @@ function (array $matches) use ($acronyms): string {
324339

325340
// @phpstan-ignore-next-line
326341
return preg_replace_callback('/^[[:lower:]]/u', function (array $matches): string {
327-
return upcase($matches[0]);
342+
return self::upcase($matches[0]);
328343
}, $result);
329344
}
330345

@@ -347,7 +362,7 @@ public function titleize(string $str): string
347362

348363
// @phpstan-ignore-next-line
349364
return preg_replace_callback('/\b(?<![\'’`])[[:lower:]]/u', function (array $matches): string {
350-
return upcase($matches[0]);
365+
return self::upcase($matches[0]);
351366
}, $str);
352367
}
353368

@@ -438,7 +453,7 @@ public function is_uncountable(string $word): bool
438453
$rc = $word;
439454

440455
return $rc
441-
&& preg_match('/\b[[:word:]]+\Z/u', downcase($rc), $matches)
456+
&& preg_match('/\b[[:word:]]+\Z/u', self::downcase($rc), $matches)
442457
&& isset($this->inflections->uncountables[$matches[0]]);
443458
}
444459
}

lib/StaticInflector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public static function capitalize(string $str, bool $preserve_str_end = false):
3434
$end = mb_substr($str, 1);
3535

3636
if (!$preserve_str_end) {
37-
$end = downcase($end);
37+
$end = self::downcase($end);
3838
}
3939

40-
return upcase(mb_substr($str, 0, 1)) . $end;
40+
return self::upcase(mb_substr($str, 0, 1)) . $end;
4141
}
4242

4343
/**

phpcs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@
2727
<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
2828
<exclude-pattern>*/lib/Inflections/*</exclude-pattern>
2929
</rule>
30+
31+
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
32+
<exclude-pattern>*/tests/HelpersTest.php</exclude-pattern>
33+
</rule>
3034
</ruleset>

phpunit07-helpers.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
executionOrder="depends,defects"
6+
beStrictAboutOutputDuringTests="true"
7+
verbose="true"
8+
colors="true"
9+
>
10+
<testsuites>
11+
<testsuite name="icanboogie/inflector">
12+
<file>tests/HelpersTest.php</file>
13+
</testsuite>
14+
</testsuites>
15+
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">./lib</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

phpunit07.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
4-
bootstrap="tests/bootstrap.php"
4+
bootstrap="vendor/autoload.php"
55
executionOrder="depends,defects"
66
beStrictAboutOutputDuringTests="true"
77
verbose="true"
88
colors="true"
99
>
1010
<testsuites>
1111
<testsuite name="icanboogie/inflector">
12-
<directory>./tests</directory>
12+
<directory>tests</directory>
13+
<exclude>tests/HelpersTest.php</exclude>
1314
</testsuite>
1415
</testsuites>
1516

phpunit11.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
44
beStrictAboutCoverageMetadata="true"
55
beStrictAboutOutputDuringTests="true"
6-
bootstrap="tests/bootstrap.php"
6+
bootstrap="vendor/autoload.php"
77
colors="true"
88
displayDetailsOnTestsThatTriggerDeprecations="true"
99
displayDetailsOnTestsThatTriggerNotices="true"
@@ -14,6 +14,7 @@
1414
<testsuites>
1515
<testsuite name="icanboogie/inflector">
1616
<directory>tests</directory>
17+
<exclude>tests/HelpersTest.php</exclude>
1718
</testsuite>
1819
</testsuites>
1920
<source>

tests/HelpersTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tests\ICanBoogie;
44

5+
require_once __DIR__ . '/../lib/helpers.php';
6+
57
use PHPUnit\Framework\Attributes\DataProvider;
68
use PHPUnit\Framework\TestCase;
79

tests/InflectionsTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313

1414
use ICanBoogie\Inflections;
1515
use ICanBoogie\InflectionsNotFound;
16+
use ICanBoogie\StaticInflector;
1617
use PHPUnit\Framework\Attributes\DataProvider;
1718
use PHPUnit\Framework\Attributes\Group;
1819
use PHPUnit\Framework\TestCase;
1920

20-
use function ICanBoogie\pluralize;
21-
use function ICanBoogie\singularize;
22-
2321
/**
2422
* @group integration
2523
*/
@@ -39,7 +37,7 @@ public function test_fail_on_undefined_inflections(): void
3937
#[DataProvider('provide_singular_and_plural')]
4038
public function test_singular_to_plural(string $locale, string $singular, string $plural): void
4139
{
42-
$this->assertEquals($plural, pluralize($singular, $locale));
40+
$this->assertEquals($plural, StaticInflector::pluralize($singular, $locale));
4341
}
4442

4543
/**
@@ -48,7 +46,7 @@ public function test_singular_to_plural(string $locale, string $singular, string
4846
#[DataProvider('provide_singular_and_plural')]
4947
public function test_plural_to_singular(string $locale, string $singular, string $plural): void
5048
{
51-
$this->assertEquals($singular, singularize($plural, $locale));
49+
$this->assertEquals($singular, StaticInflector::singularize($plural, $locale));
5250
}
5351

5452
// @phpstan-ignore-next-line

tests/InflectorTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
use ICanBoogie\Inflections;
1515
use ICanBoogie\Inflector;
16+
use ICanBoogie\StaticInflector;
1617
use PHPUnit\Framework\TestCase;
1718

18-
use function ICanBoogie\camelize;
19-
2019
final class InflectorTest extends TestCase
2120
{
2221
/**
@@ -73,39 +72,39 @@ public function test_camelize(): void
7372

7473
foreach ($ar as $camel => $underscore) {
7574
$this->assertEquals($camel, self::$inflector->camelize($underscore));
76-
$this->assertEquals($camel, camelize($underscore));
75+
$this->assertEquals($camel, StaticInflector::camelize($underscore));
7776
}
7877

7978
$ar = require __DIR__ . '/cases/camel_to_dash.php';
8079

8180
foreach ($ar as $camel => $dash) {
8281
$this->assertEquals($camel, self::$inflector->camelize($dash));
83-
$this->assertEquals($camel, camelize($dash));
82+
$this->assertEquals($camel, StaticInflector::camelize($dash));
8483
}
8584
}
8685

8786
public function test_camelize_with_lower_upcases_the_first_letter(): void
8887
{
8988
$this->assertEquals('Capital', self::$inflector->camelize('capital'));
90-
$this->assertEquals('Capital', camelize('capital'));
89+
$this->assertEquals('Capital', StaticInflector::camelize('capital'));
9190
}
9291

9392
public function test_camelize_preserve_words_ends(): void
9493
{
9594
$this->assertEquals('WordOne\\WordTwo', self::$inflector->camelize('wordOne/wordTwo'));
96-
$this->assertEquals('WordOne\\WordTwo', camelize('wordOne/wordTwo'));
95+
$this->assertEquals('WordOne\\WordTwo', StaticInflector::camelize('wordOne/wordTwo'));
9796
}
9897

9998
public function test_camelize_with_lower_downcases_the_first_letter(): void
10099
{
101100
$this->assertEquals('capital', self::$inflector->camelize('Capital', true));
102-
$this->assertEquals('capital', camelize('Capital', true));
101+
$this->assertEquals('capital', StaticInflector::camelize('Capital', true));
103102
}
104103

105104
public function test_camelize_with_underscores(): void
106105
{
107106
$this->assertEquals("CamelCase", self::$inflector->camelize('Camel_Case'));
108-
$this->assertEquals("CamelCase", camelize('Camel_Case'));
107+
$this->assertEquals("CamelCase", StaticInflector::camelize('Camel_Case'));
109108
}
110109

111110
public function test_acronyms(): void

tests/bootstrap.php

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)