Skip to content

Commit

Permalink
update unit tests to be compatible with PHPUnit 11
Browse files Browse the repository at this point in the history
  • Loading branch information
RinkAttendant6 committed Jan 11, 2025
1 parent dc53a09 commit 9c7e231
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
7 changes: 6 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php">
<phpunit bootstrap="vendor/autoload.php"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true">
</phpunit>
14 changes: 2 additions & 12 deletions tests/DateFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ protected function setUp(): void
$this->object = new DateFormat('fr-CA');
}

/**
* @covers ::__construct
*/
public function testDefaultConstructor(): void
{
$obj2 = new DateFormat('en-CA');

static::assertSame('en-CA', Assert::readAttribute($obj2, 'locale'));
}

/**
* @covers ::__construct
*/
Expand All @@ -59,7 +49,7 @@ public function testAddResource(): void
{
$this->object->addResource(__DIR__ . '/resources/dateformats.json');

$ref = Assert::readAttribute($this->object, 'formatters');
$ref = $this->object->__debugInfo();

static::assertArrayHasKey('day_date', $ref['en-CA']);
static::assertArrayHasKey('day_date_time', $ref['fr-CA']);
Expand All @@ -85,7 +75,7 @@ public function testFormat(string $expected, $input, string $locale): void
static::assertSame($expected, $this->object->format($input, 'day', $locale));
}

public function formatDataProvider(): iterable
public static function formatDataProvider(): iterable
{
$date = new DateTime('2014-01-01', new DateTimeZone(self::TIMEZONE));
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2014-07-01 08:00:00');
Expand Down
39 changes: 24 additions & 15 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Iterator;
use JsonI18n\Resource;
use PHPUnit\Framework\Error\Error;
use PHPUnit\Framework\Error\Notice;
use PHPUnit\Framework\TestCase;

Expand All @@ -33,7 +32,7 @@ class ResourceTest extends TestCase
*/
protected $data = [
'station' => "Station",
'goodbye' => "Have a good day!"
'goodbye' => "Have a good day!",
];

/**
Expand All @@ -44,6 +43,7 @@ class ResourceTest extends TestCase

protected function setUp(): void
{
error_reporting(\E_ALL);
$this->json = json_encode([$this->locale => $this->data]);
$this->object = new Resource($this->locale, $this->data);
}
Expand All @@ -58,7 +58,7 @@ public function testFromJson(): void
$resource = Resource::fromJson($this->json);

static::assertSame($this->locale, $resource->getLocale());
static::assertSame($this->data, $this->readAttribute($this->object, 'data'));
static::assertSame($this->data, $this->object->getData());
}

/**
Expand All @@ -84,12 +84,12 @@ public function testAddData(): void
{
$new = [
'station' => 'Terminal',
'greeting' => 'Hello'
'greeting' => 'Hello',
];

$this->object->addData($new);

$data = $this->readAttribute($this->object, 'data');
$data = $this->object->getData();

static::assertArrayHasKey('greeting', $data);
static::assertSame('Terminal', $data['station']);
Expand All @@ -102,12 +102,12 @@ public function testAddDataNoOverwrite(): void
{
$new = [
'station' => 'Terminal',
'greeting' => 'Hello'
'greeting' => 'Hello',
];

$this->object->addData($new, false);

$data = $this->readAttribute($this->object, 'data');
$data = $this->object->getData();
static::assertSame('Station', $data['station']);
}

Expand All @@ -126,12 +126,12 @@ public function testMerge(): void
{
$new = [
'station' => 'Terminal',
'greeting' => 'Hello'
'greeting' => 'Hello',
];

$this->object->merge(new Resource($this->locale, $new));

$data = $this->readAttribute($this->object, 'data');
$data = $this->object->getData();
static::assertSame('Terminal', $data['station']);
static::assertArrayHasKey('greeting', $new);
}
Expand All @@ -143,12 +143,12 @@ public function testMergeNoOverwrite(): void
{
$new = [
'station' => 'Terminal',
'greeting' => 'Hello'
'greeting' => 'Hello',
];

$this->object->merge(new Resource($this->locale, $new), false);

$data = $this->readAttribute($this->object, 'data');
$data = $this->object->getData();
static::assertSame('Station', $data['station']);
static::assertArrayHasKey('greeting', $new);
}
Expand All @@ -158,7 +158,16 @@ public function testMergeNoOverwrite(): void
*/
public function testMergeDifferentLocales(): void
{
static::expectException(Notice::class);
set_error_handler(
static function ( $errno, $errstr ) {
restore_error_handler();
throw new \Exception( $errstr, $errno );
},
E_ALL
);

static::expectException(\Exception::class);
static::expectExceptionCode(\E_USER_NOTICE);

$this->object->merge(new Resource('zh-CN'));
}
Expand Down Expand Up @@ -201,7 +210,7 @@ public function testOffsetGet(): void
*/
public function testOffsetGetInexistent(): void
{
static::expectException(Error::class);
static::expectException(\TypeError::class);

$this->object['foo'];
}
Expand All @@ -213,7 +222,7 @@ public function testOffsetSet(): void
{
$this->object['greeting'] = 'Hello';

static::assertArrayHasKey('greeting', $this->readAttribute($this->object, 'data'));
static::assertArrayHasKey('greeting', $this->object->getData());
}

/**
Expand All @@ -223,7 +232,7 @@ public function testOffsetUnset(): void
{
unset($this->object['station']);

static::assertArrayNotHasKey('station', $this->readAttribute($this->object, 'data'));
static::assertArrayNotHasKey('station', $this->object->getData());
}

/**
Expand Down
31 changes: 14 additions & 17 deletions tests/TranslateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use JsonI18n\Translate;
use LengthException;
use OutOfBoundsException;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use TypeError;
use const JSON_ERROR_SYNTAX;
Expand Down Expand Up @@ -38,7 +37,7 @@ class TranslateTest extends TestCase
'headsign_en' => 'Blackbird Mall',
'headsign_fr' => 'Centre commercial Blackbird',
'subtext_en' => 'Main Street',
'subtext_fr' => 'Rue Main'
'subtext_fr' => 'Rue Main',
];

/**
Expand All @@ -48,7 +47,7 @@ class TranslateTest extends TestCase
protected $arr2D = [
['headsign_en' => 'University', 'headsign_fr' => 'Université'],
['headsign_en' => 'College', 'headsign_fr' => 'Collège'],
['headsign_en' => 'Airport', 'headsign_fr' => 'Aéroport']
['headsign_en' => 'Airport', 'headsign_fr' => 'Aéroport'],
];

/**
Expand All @@ -62,8 +61,8 @@ class TranslateTest extends TestCase
'crosstown' => [
['headsign_en' => 'Blackbird Mall', 'headsign_fr' => 'Centre commercial Blackbird'],
['headsign_en' => 'University', 'headsign_fr' => 'Université'],
['headsign_en' => 'College', 'headsign_fr' => 'Collège']
]
['headsign_en' => 'College', 'headsign_fr' => 'Collège'],
],
];

protected function setUp(): void
Expand All @@ -78,7 +77,7 @@ public function testDefaultConstructor(): void
{
$obj2 = new Translate('en-CA');

static::assertSame('en-CA', Assert::readAttribute($obj2, 'lang'));
static::assertSame('en-CA', $this->object->getLanguage());
}

/**
Expand Down Expand Up @@ -135,12 +134,11 @@ public function testAddResourceFile(): void
{
$this->object->addResource(__DIR__ . '/resources/test.json');

$data = Assert::readAttribute($this->object, 'data');
$data = $this->object->__debugInfo();

static::assertNotEmpty($data);
static::assertArrayHasKey('en-CA', $data);
static::assertArrayHasKey('fr-CA', $data);
static::assertNotEmpty(Assert::readAttribute($this->object, 'arrayGroups'));
}

/**
Expand All @@ -156,7 +154,7 @@ public function testAddResourceArray(): void

$this->object->addResource($array);

$data = Assert::readAttribute($this->object, 'data');
$data = $this->object->__debugInfo();

static::assertArrayHasKey('en-CA', $data);
static::assertArrayHasKey('fr-CA', $data);
Expand All @@ -171,12 +169,11 @@ public function testAddResourceJson(): void
{
$this->object->addResource($this->json, 'json');

$data = Assert::readAttribute($this->object, 'data');
$data = $this->object->__debugInfo();

static::assertNotEmpty($data);
static::assertArrayHasKey('en-CA', $data);
static::assertArrayHasKey('fr-CA', $data);
static::assertNotEmpty(Assert::readAttribute($this->object, 'arrayGroups'));
}

/**
Expand Down Expand Up @@ -362,7 +359,7 @@ public function testLocalize1DArray(): void

static::assertEquals([
'headsign' => 'Blackbird Mall',
'subtext' => 'Rue Main'
'subtext' => 'Rue Main',
], $arr1);
}

Expand All @@ -381,13 +378,13 @@ public function testLocalize2DArray(): void
static::assertEquals([
['headsign' => 'University'],
['headsign' => 'College'],
['headsign' => 'Airport']
['headsign' => 'Airport'],
], $arr1);

static::assertEquals([
['headsign' => 'Université'],
['headsign' => 'Collège'],
['headsign' => 'Aéroport']
['headsign' => 'Aéroport'],
], $arr2);
}

Expand All @@ -408,9 +405,9 @@ public function testLocalize3DArray(): void
'crosstown' => [
['headsign' => 'Blackbird Mall'],
['headsign' => 'University'],
['headsign' => 'College']
]], $arr
);
['headsign' => 'College'],
],
], $arr);
}

/**
Expand Down

0 comments on commit 9c7e231

Please sign in to comment.