Skip to content

Commit

Permalink
Improve test code style (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude authored Jan 5, 2024
1 parent 44958a9 commit 2500871
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.7/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">

Expand All @@ -19,7 +19,7 @@
</exclude>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
Expand Down
16 changes: 8 additions & 8 deletions tests/Doctrine/PersistentTranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testToStringReturnsTranslatedMessage(): void

$translation = (string) $proxy;

$this->assertEquals('bar', $translation);
self::assertEquals('bar', $translation);
}

public function testToStringReturnsStringIfExceptionOccurredAndNoLoggerIsAvailable(): void
Expand Down Expand Up @@ -59,7 +59,7 @@ public function testToStringLogsExceptionIfLoggerIsAvailable(): void

$proxy->__toString();

$this->assertGreaterThan(0, \count($logger->cleanLogs()), 'Expected at least one log message');
self::assertGreaterThan(0, \count($logger->cleanLogs()), 'Expected at least one log message');
}

public function testLoggedMessageContainsInformationAboutTranslatedProperty(): void
Expand All @@ -75,7 +75,7 @@ public function testLoggedMessageContainsInformationAboutTranslatedProperty(): v
$logs = $logger->cleanLogs();
$logEntry = current($logs);
self::assertIsArray($logEntry);
$this->assertArrayHasKey(1, $logEntry, 'Missing log message.');
self::assertArrayHasKey(1, $logEntry, 'Missing log message.');
$logMessage = $logEntry[1];
self::assertStringContainsString('TestEntity', $logMessage, 'Missing entity class name.');
self::assertStringContainsString('text', $logMessage, 'Missing translated property.');
Expand All @@ -95,7 +95,7 @@ public function testLoggedMessageContainsOriginalException(): void
$logs = $logger->cleanLogs();
$logEntry = current($logs);
self::assertIsArray($logEntry);
$this->assertArrayHasKey(1, $logEntry, 'Missing log message.');
self::assertArrayHasKey(1, $logEntry, 'Missing log message.');
$logMessage = $logEntry[1];
self::assertStringContainsString('Cannot find translations', $logMessage, 'Original exception not contained.');
}
Expand All @@ -108,7 +108,7 @@ public function isTranslatedInto_returns_true_for_primary_translation_if_set():
$proxy->setTranslation('bar', 'de');
$proxy->setTranslation('bar in en', 'en');

$this->assertTrue($proxy->isTranslatedInto('en'));
self::assertTrue($proxy->isTranslatedInto('en'));
}

/** @test */
Expand All @@ -119,7 +119,7 @@ public function isTranslatedInto_returns_true_for_translation_if_set(): void
$proxy->setTranslation('bar', 'de');
$proxy->setTranslation('bar in en', 'en');

$this->assertTrue($proxy->isTranslatedInto('de'));
self::assertTrue($proxy->isTranslatedInto('de'));
}

/** @test */
Expand All @@ -131,7 +131,7 @@ public function isTranslatedInto_returns_false_if_primary_translation_is_empty()
$proxy->setTranslation('', 'en');

$isTranslatedInto = $proxy->isTranslatedInto('en');
$this->assertFalse($isTranslatedInto);
self::assertFalse($isTranslatedInto);
}

/** @test */
Expand All @@ -142,7 +142,7 @@ public function isTranslatedInto_returns_false_if_translation_is_not_set(): void
$proxy->setTranslation('bar', 'de');
$proxy->setTranslation('bar in en', 'en');

$this->assertFalse($proxy->isTranslatedInto('fr'));
self::assertFalse($proxy->isTranslatedInto('fr'));
}

private function createProxy(TestEntity $entity, LoggerInterface $logger = null): PersistentTranslatable
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/TranslatableClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function can_be_serialized_and_retrieved(): void
$serialize = serialize($metadata->sleep());
$unserialized = TranslatableClassMetadata::wakeup(unserialize($serialize));

$this->assertEquals($metadata, $unserialized);
self::assertEquals($metadata, $unserialized);
}

private function createMetadata(LoggerInterface $logger = null): TranslatableClassMetadata
Expand Down
20 changes: 10 additions & 10 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testPersistingEntityWithPlainStringInTranslatableField(): void
$this->infrastructure->import($entity);

$entity = $this->clearAndRefetch($entity);
$this->assertEquals('text', $entity->getText());
self::assertEquals('text', $entity->getText());
}

public function testPersistingEntityWithTranslatableInstanceInTranslatableField(): void
Expand All @@ -48,7 +48,7 @@ public function testPersistingEntityWithTranslatableInstanceInTranslatableField(
$this->infrastructure->import($entity);

$entity = $this->clearAndRefetch($entity);
$this->assertEquals('text', $entity->getText());
self::assertEquals('text', $entity->getText());
}

public function testGettingTranslationsFromManagedEntity(): void
Expand All @@ -57,8 +57,8 @@ public function testGettingTranslationsFromManagedEntity(): void
// plain Translatable instance. This test makes sure we can tuck the entity
// into the DB, refetch it and things still work
$entity = $this->createAndFetchTestEntity();
$this->assertEquals('text en_GB', $entity->getText()->translate('en_GB'));
$this->assertEquals('text de_DE', $entity->getText()->translate('de_DE'));
self::assertEquals('text en_GB', $entity->getText()->translate('en_GB'));
self::assertEquals('text de_DE', $entity->getText()->translate('de_DE'));
}

public function testOnceEntityHasBeenFetchedFromDbTheDefaultLocaleCanBeSwitched(): void
Expand All @@ -68,10 +68,10 @@ public function testOnceEntityHasBeenFetchedFromDbTheDefaultLocaleCanBeSwitched(
// in - see TranslatableTest::testDefaultLocaleProviderCanProvideDefaultLocale

$entity = $this->createAndFetchTestEntity();
$this->assertEquals('text en_GB', (string) $entity->getText());
self::assertEquals('text en_GB', (string) $entity->getText());

$this->defaultLocaleProvider->setDefaultLocale('de_DE');
$this->assertEquals('text de_DE', (string) $entity->getText());
self::assertEquals('text de_DE', (string) $entity->getText());
}

public function testTranslationsAreImplicitlyPersistedForNewEntitiy(): void
Expand All @@ -81,7 +81,7 @@ public function testTranslationsAreImplicitlyPersistedForNewEntitiy(): void
$this->infrastructure->import($newEntity); // just import the "main" entity, which has no 'cascade="..."' configuration

$newEntity = $this->clearAndRefetch($newEntity);
$this->assertEquals('text de_DE', $newEntity->getText()->translate('de_DE')); // translation is available, must have been persisted in the DB
self::assertEquals('text de_DE', $newEntity->getText()->translate('de_DE')); // translation is available, must have been persisted in the DB
}

public function testNewTranslationsAreImplicitlyPersistedForManagedEntitiy(): void
Expand All @@ -92,7 +92,7 @@ public function testNewTranslationsAreImplicitlyPersistedForManagedEntitiy(): vo
$this->entityManager->flush();

$managedEntity = $this->clearAndRefetch($managedEntity);
$this->assertEquals('text xx_XX', $managedEntity->getText()->translate('xx_XX')); // Translation still there, must come from DB
self::assertEquals('text xx_XX', $managedEntity->getText()->translate('xx_XX')); // Translation still there, must come from DB
}

public function testEntityConsideredCleanWhenNoTranslationWasChanged(): void
Expand All @@ -110,9 +110,9 @@ public function testEntityConsideredCleanWhenNoTranslationWasChanged(): void
$this->infrastructure->getEntityManager()->flush();

$queries = $this->getQueries();
$this->assertEquals($queryCount, \count($queries));
self::assertEquals($queryCount, \count($queries));

$this->assertInstanceOf(TranslatableInterface::class, $entity->getText());
self::assertInstanceOf(TranslatableInterface::class, $entity->getText());
}

private function createAndFetchTestEntity(): TestEntity
Expand Down
36 changes: 18 additions & 18 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@ class TranslatableTest extends TestCase
public function testImplementsInterface(): void
{
$t = new Translatable();
$this->assertInstanceOf(TranslatableInterface::class, $t);
self::assertInstanceOf(TranslatableInterface::class, $t);
}

public function testReturnsMainValueAndTranslations(): void
{
$t = new Translatable('text locale_A', 'locale_A');
$t->setTranslation('text locale_B', 'locale_B');

$this->assertEquals('text locale_A', $t->translate('locale_A'));
$this->assertEquals('text locale_B', $t->translate('locale_B'));
self::assertEquals('text locale_A', $t->translate('locale_A'));
self::assertEquals('text locale_B', $t->translate('locale_B'));
}

public function testCanBeCreatedWithoutLocale(): void
{
$t = new Translatable('text locale_A');
$this->assertEquals('text locale_A', $t->translate());
self::assertEquals('text locale_A', $t->translate());
}

public function testReturnsNullForUnknownLocale(): void
{
$t = new Translatable('text locale_A', 'locale_A');
$this->assertNull($t->translate('unknown'));
self::assertNull($t->translate('unknown'));
}

public function testDeferredSettingOfDefaultLocale(): void
{
$t = new Translatable('some text');
$t->setDefaultLocale('foo');

$this->assertEquals('some text', $t->translate('foo'));
self::assertEquals('some text', $t->translate('foo'));
}

public function testCopyTranslations(): void
Expand All @@ -54,15 +54,15 @@ public function testCopyTranslations(): void

$t->copy($other);

$this->assertEquals('text locale_A', $other->translate('locale_A'));
$this->assertEquals('text locale_B', $other->translate('locale_B'));
$this->assertEquals('text locale_C', $other->translate('locale_C'));
self::assertEquals('text locale_A', $other->translate('locale_A'));
self::assertEquals('text locale_B', $other->translate('locale_B'));
self::assertEquals('text locale_C', $other->translate('locale_C'));
}

public function testReturnsDefaultValueWhenCastToString(): void
{
$t = new Translatable('text locale_A', 'locale_A');
$this->assertEquals('text locale_A', (string) $t);
self::assertEquals('text locale_A', (string) $t);
}

public function testDefaultLocaleProviderCanProvideDefaultLocale(): void
Expand All @@ -72,20 +72,20 @@ public function testDefaultLocaleProviderCanProvideDefaultLocale(): void
$t = new Translatable('text locale_A', $defaultLocaleProvider);
$t->setTranslation('text locale_B', 'locale_B');

$this->assertEquals('text locale_A', (string) $t);
$this->assertEquals('text locale_A', $t->translate());
self::assertEquals('text locale_A', (string) $t);
self::assertEquals('text locale_A', $t->translate());

$defaultLocaleProvider->setDefaultLocale('locale_B');
$this->assertEquals('text locale_B', (string) $t);
$this->assertEquals('text locale_B', $t->translate());
self::assertEquals('text locale_B', (string) $t);
self::assertEquals('text locale_B', $t->translate());
}

/** @test */
public function isTranslatedInto_returns_true_for_primary_translation_if_set(): void
{
$translatable = new Translatable('text locale_A', 'locale_A');

$this->assertTrue($translatable->isTranslatedInto('locale_A'));
self::assertTrue($translatable->isTranslatedInto('locale_A'));
}

/** @test */
Expand All @@ -94,22 +94,22 @@ public function isTranslatedInto_returns_true_for_translation_if_set(): void
$translatable = new Translatable('text locale_A', 'locale_A');
$translatable->setTranslation('text locale_B', 'locale_B');

$this->assertTrue($translatable->isTranslatedInto('locale_B'));
self::assertTrue($translatable->isTranslatedInto('locale_B'));
}

/** @test */
public function isTranslatedInto_returns_false_if_primary_translation_is_empty(): void
{
$translatable = new Translatable('', 'locale_A');

$this->assertFalse($translatable->isTranslatedInto('locale_A'));
self::assertFalse($translatable->isTranslatedInto('locale_A'));
}

/** @test */
public function isTranslatedInto_returns_false_if_translation_is_not_set(): void
{
$translatable = new Translatable('', 'locale_A');

$this->assertFalse($translatable->isTranslatedInto('locale_B'));
self::assertFalse($translatable->isTranslatedInto('locale_B'));
}
}

0 comments on commit 2500871

Please sign in to comment.