Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
92 changes: 67 additions & 25 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Continuous Integration
on:
push:
workflow_dispatch:
schedule:
- cron: 0 0 * * 0

Expand All @@ -10,8 +11,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
os: [unbuntu-latest, macos-latest, windows-latest]
php: ['8.1', '8.2']
os: [ubuntu-latest, macos-latest, windows-latest]
php: ['8.2', '8.3', '8.4']
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -23,17 +24,40 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-php${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php${{ matrix.php }}-composer-

- name: Install Dependencies
run: composer install
if: ${{ matrix.php != '8.2' }}

- name: Install Dependencies (Ignore Platform Req)
run: composer install --ignore-platform-req=php
if: ${{ matrix.php == '8.2' }}

- name: Test
run: vendor/bin/phpunit tests --testdox --coverage-clover coverage.xml

- name: Upload Coverage Artifact
uses: actions/upload-artifact@v4
if: ${{ matrix.php == '8.4' && matrix.os == 'ubuntu-latest' }}
with:
name: codecov
path: coverage.xml

codecov:
name: Code Coverage Report
runs-on: ubuntu-latest
needs: phpunit
steps:
- name: Download Coverage Artifact
uses: actions/download-artifact@v4
with:
name: codecov

- name: Upload to CodeCov
uses: codecov/codecov-action@v1
with:
Expand All @@ -46,7 +70,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.1', '8.2']
php: ['8.2', '8.3', '8.4']
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -57,13 +81,19 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-php${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php${{ matrix.php }}-composer-

- name: Install Dependencies
run: composer install
if: ${{ matrix.php != '8.2' }}

- name: Install Dependencies (Ignore Platform Req)
run: composer install --ignore-platform-req=php
if: ${{ matrix.php == '8.2' }}

- name: Run PHPStan
run: vendor/bin/phpstan
Expand All @@ -75,19 +105,25 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.4
tools: composer:v2

- name: Checkout Repository
uses: actions/checkout@v2

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-php${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php${{ matrix.php }}-composer-

- name: Install Dependencies
run: composer install
if: ${{ matrix.php != '8.2' }}

- name: Install Dependencies (Ignore Platform Req)
run: composer install --ignore-platform-req=php
if: ${{ matrix.php == '8.2' }}

- name: PHP Code Sniffer
run: vendor/bin/phpcs
Expand All @@ -98,7 +134,7 @@ jobs:
continue-on-error: true
strategy:
matrix:
php: ['8.1', '8.2']
php: ['8.2', '8.3', '8.4']
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -111,13 +147,19 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-php${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php${{ matrix.php }}-composer-

- name: Install Dependencies
run: composer install
if: ${{ matrix.php != '8.2' }}

- name: Install Dependencies (Ignore Platform Req)
run: composer install --ignore-platform-req=php
if: ${{ matrix.php == '8.2' }}

- name: Check for Mutants
env:
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
}
},
"require": {
"php": "^8.1"
"php": "^8.2"
},
"config": {
"allow-plugins": {
"infection/extension-installer": true
}
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1",
"infection/infection": "^0.26.0",
"phpstan/phpstan": "^2",
"infection/infection": "^0.31.0",
"squizlabs/php_codesniffer": "^3.6",
"roave/security-advisories": "dev-master"
}
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 9
level: max
paths:
- src
27 changes: 20 additions & 7 deletions src/SmartString.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Navarr\SmartString;

use JetBrains\PhpStorm\Immutable;
Expand Down Expand Up @@ -47,8 +49,9 @@
}

#[Pure]
public function stripos(string $needle, int $offset = 0): int|false
public function stripos(Stringable|string $needle, int $offset = 0): int|false

Check warning on line 52 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ return $this->value; } #[Pure] - public function stripos(Stringable|string $needle, int $offset = 0): int|false + public function stripos(Stringable|string $needle, int $offset = 1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 52 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ return $this->value; } #[Pure] - public function stripos(Stringable|string $needle, int $offset = 0): int|false + public function stripos(Stringable|string $needle, int $offset = -1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 52 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ return $this->value; } #[Pure] - public function stripos(Stringable|string $needle, int $offset = 0): int|false + public function stripos(Stringable|string $needle, int $offset = 1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 52 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ return $this->value; } #[Pure] - public function stripos(Stringable|string $needle, int $offset = 0): int|false + public function stripos(Stringable|string $needle, int $offset = -1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 52 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ return $this->value; } #[Pure] - public function stripos(Stringable|string $needle, int $offset = 0): int|false + public function stripos(Stringable|string $needle, int $offset = 1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 52 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ return $this->value; } #[Pure] - public function stripos(Stringable|string $needle, int $offset = 0): int|false + public function stripos(Stringable|string $needle, int $offset = -1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {
{
$needle = (string)$needle;
return match ($this->shouldUseGrapheme) {
true => grapheme_stripos($this->value, $needle, $offset),
false => stripos($this->value, $needle, $offset)
Expand Down Expand Up @@ -89,7 +92,7 @@
#[Pure]
public function findLastPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false
{
if (($flags & static::CASE_INSENSITIVE) == static::CASE_INSENSITIVE) {
if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) {
return $this->strripos($needle, $offset);
}
return $this->strrpos($needle, $offset);
Expand All @@ -112,7 +115,7 @@
}

#[Pure]
public function strpos(Stringable|string $needle, int $offset = 0): int|false

Check warning on line 118 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ return $this->substr($offset, $length); } #[Pure] - public function strpos(Stringable|string $needle, int $offset = 0): int|false + public function strpos(Stringable|string $needle, int $offset = 1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 118 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ return $this->substr($offset, $length); } #[Pure] - public function strpos(Stringable|string $needle, int $offset = 0): int|false + public function strpos(Stringable|string $needle, int $offset = -1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 118 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ return $this->substr($offset, $length); } #[Pure] - public function strpos(Stringable|string $needle, int $offset = 0): int|false + public function strpos(Stringable|string $needle, int $offset = 1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 118 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ return $this->substr($offset, $length); } #[Pure] - public function strpos(Stringable|string $needle, int $offset = 0): int|false + public function strpos(Stringable|string $needle, int $offset = -1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 118 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ return $this->substr($offset, $length); } #[Pure] - public function strpos(Stringable|string $needle, int $offset = 0): int|false + public function strpos(Stringable|string $needle, int $offset = 1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {

Check warning on line 118 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ return $this->substr($offset, $length); } #[Pure] - public function strpos(Stringable|string $needle, int $offset = 0): int|false + public function strpos(Stringable|string $needle, int $offset = -1): int|false { $needle = (string) $needle; return match ($this->shouldUseGrapheme) {
{
$needle = (string)$needle;
return match ($this->shouldUseGrapheme) {
Expand All @@ -122,9 +125,9 @@
}

#[Pure]
public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 1): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = -1): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = 1, int $flags = 0): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = -1, int $flags = 0): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 1): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = -1): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = 1, int $flags = 0): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = -1, int $flags = 0): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 1): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = -1): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = 1, int $flags = 0): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);

Check warning on line 128 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ }; } #[Pure] - public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false + public function findPosition(Stringable|string $needle, int $offset = -1, int $flags = 0): int|false { if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset);
{
if (($flags & static::CASE_INSENSITIVE) == static::CASE_INSENSITIVE) {
if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) {

Check warning on line 130 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "BitwiseAnd": @@ @@ #[Pure] public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false { - if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { + if (($flags | self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset); } return $this->strpos($needle, $offset);

Check warning on line 130 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "BitwiseAnd": @@ @@ #[Pure] public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false { - if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { + if (($flags | self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset); } return $this->strpos($needle, $offset);

Check warning on line 130 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "BitwiseAnd": @@ @@ #[Pure] public function findPosition(Stringable|string $needle, int $offset = 0, int $flags = 0): int|false { - if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { + if (($flags | self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) { return $this->stripos($needle, $offset); } return $this->strpos($needle, $offset);
return $this->stripos($needle, $offset);
}
return $this->strpos($needle, $offset);
Expand All @@ -144,13 +147,23 @@
#[Pure]
public function substringFromNeedle(
Stringable|string $needle,
bool $beforeNeedle = false,
int $flags = 0
): SmartString|false {
if (($flags & static::CASE_INSENSITIVE) == static::CASE_INSENSITIVE) {
return $this->stristr($needle, $beforeNeedle);
if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) {
return $this->stristr($needle);
}
return $this->strstr($needle);
}

#[Pure]
public function substringUntilNeedle(
Stringable|string $needle,
int $flags = 0
): SmartString|false {
if (($flags & self::CASE_INSENSITIVE) == self::CASE_INSENSITIVE) {
return $this->stristr($needle, true);
}
return $this->strstr($needle, $beforeNeedle);
return $this->strstr($needle, true);
}

#[Pure]
Expand Down Expand Up @@ -179,7 +192,7 @@
{
if ($this->shouldUseGrapheme) {
// If we're already multibyte characters, then the result of a concatenation will be too
return new SmartString($this . $additionalValue, $this->shouldUseGrapheme);

Check warning on line 195 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.4)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ public function concat(Stringable|string $additionalValue): SmartString { if ($this->shouldUseGrapheme) { - // If we're already multibyte characters, then the result of a concatenation will be too - return new SmartString($this . $additionalValue, $this->shouldUseGrapheme); + } return static::build($this . $additionalValue); } }

Check warning on line 195 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.3)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ public function concat(Stringable|string $additionalValue): SmartString { if ($this->shouldUseGrapheme) { - // If we're already multibyte characters, then the result of a concatenation will be too - return new SmartString($this . $additionalValue, $this->shouldUseGrapheme); + } return static::build($this . $additionalValue); } }

Check warning on line 195 in src/SmartString.php

View workflow job for this annotation

GitHub Actions / Infection Check (8.2)

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ public function concat(Stringable|string $additionalValue): SmartString { if ($this->shouldUseGrapheme) { - // If we're already multibyte characters, then the result of a concatenation will be too - return new SmartString($this . $additionalValue, $this->shouldUseGrapheme); + } return static::build($this . $additionalValue); } }
}
return static::build($this . $additionalValue);
}
Expand Down
51 changes: 51 additions & 0 deletions tests/ConcatenateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Navarr\SmartString\Test;

use Navarr\SmartString\SmartString;
use Navarr\SmartString\SmartStringFactory;
use PHPUnit\Framework\TestCase;
use Stringable;

class ConcatenateTest extends TestCase
{
private SmartStringFactory $factory;

public function setUp(): void
{
$this->factory = new SmartStringFactory();
parent::setUp();
}

public function getData(): array
{
return [
['a', 'b', 'ab'],
['a', SmartString::build('b'), 'ab'],
['a', '🏴', 'a🏴'],
['🏴', 'a', '🏴a'],
];
}

/**
* @dataProvider getData
*/
public function testConcat(string $initial, Stringable|string $additional, string $expectedResult): void
{
$object = $this->factory->create($initial);
$result = $object->concat($additional);
$this->assertInstanceOf(SmartString::class, $result);
$this->assertEquals($expectedResult, (string)$result);
}

/**
* @dataProvider getData
*/
public function testConcatenate(string $initial, Stringable|string $additional, string $expectedResult): void
{
$object = $this->factory->create($initial);
$result = $object->concatenate($additional);
$this->assertInstanceOf(SmartString::class, $result);
$this->assertEquals($expectedResult, (string)$result);
}
}
64 changes: 64 additions & 0 deletions tests/ConstructionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Navarr\SmartString\Test;

use Navarr\SmartString\SmartString;
use Navarr\SmartString\SmartStringFactory;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Stringable;

class ConstructionTest extends TestCase
{
private SmartStringFactory $factory;

public function setUp(): void
{
$this->factory = new SmartStringFactory();
}

public function getData(): array
{
return [
['a', 'a', false],
[SmartString::build('a'), 'a', false],
['🏴', '🏴', true],
[SmartString::build('🏴'), '🏴', true],
];
}

/**
* @dataProvider getData
*/
public function testBuild(
Stringable|string $suppliedValue,
string $expectedValue,
bool $expectedUseGraphemeValue
): void {
$object = SmartString::build($suppliedValue);
$this->testObject($object, $expectedValue, $expectedUseGraphemeValue);
}

/**
* @dataProvider getData
*/
public function testFactory(
Stringable|string $suppliedValue,
string $expectedValue,
bool $expectedUseGraphemeValue
): void {
$object = $this->factory->create($suppliedValue);
$this->testObject($object, $expectedValue, $expectedUseGraphemeValue);
}

private function testObject(SmartString $object, string $expectedValue, bool $expectedUseGraphemeValue)
{
$reflectionObject = new ReflectionClass($object);

$valueProp = $reflectionObject->getProperty('value');
$shouldUseGraphemeProp = $reflectionObject->getProperty('shouldUseGrapheme');

$this->assertEquals($expectedValue, $valueProp->getValue($object));
$this->assertEquals($expectedUseGraphemeValue, $shouldUseGraphemeProp->getValue($object));
}
}
Loading
Loading