Skip to content

Commit

Permalink
add unit tests to phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Dec 23, 2023
1 parent 9f8f627 commit 0a470ae
Show file tree
Hide file tree
Showing 33 changed files with 133 additions and 84 deletions.
1 change: 1 addition & 0 deletions .phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parameters:
level: 9
paths:
- ./src
- ./tests/phpunit
excludePaths:
- ./src/vendor/*
checkExplicitMixed: false
15 changes: 12 additions & 3 deletions src/Bundles/Exchange/CSV/Services/CSVWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPUnuhi\Bundles\Exchange\CSV\Services;

use Exception;

class CSVWriter implements CSVWriterInterface
{

Expand All @@ -28,11 +30,18 @@ public function deleteFile(string $filename): void

/**
* @param string $filename
* @return false|resource
* @throws Exception
* @return mixed
*/
public function open(string $filename)
public function open(string $filename) : mixed
{
return fopen($filename, 'ab');
$resource = fopen($filename, 'ab');

if (!$resource) {
throw new Exception('Could not open file: ' . $filename);
}

return $resource;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Bundles/Exchange/CSV/Services/CSVWriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function deleteFile(string $filename): void;
* @param string $filename
* @return resource
*/
public function open(string $filename);
public function open(string $filename) : mixed;

/**
* @param resource $file
Expand Down
25 changes: 24 additions & 1 deletion tests/phpunit/Bundles/Storage/JSON/Services/JsonSaverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ class JsonStorageTest extends TestCase
*/
private $locale;

/**
* @var array<string>
*/
private $files = [];


/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();
Expand All @@ -26,13 +33,19 @@ protected function setUp(): void
$this->locale = $locale;
}

/**
* @return void
*/
protected function tearDown(): void
{
parent::tearDown();

\array_map('unlink', \array_filter($this->files, 'is_file'));
}

/**
* @return void
*/
public function testIndentBy4WithNewLineAtTheEnd(): void
{
$testFile = $this->createRandomFile();
Expand All @@ -53,6 +66,9 @@ public function testIndentBy4WithNewLineAtTheEnd(): void
static::assertSame($expected, $json);
}

/**
* @return void
*/
public function testIndentBy2WithoutNewLineAtTheEnd(): void
{
$testFile = $this->createRandomFile();
Expand All @@ -72,6 +88,10 @@ public function testIndentBy2WithoutNewLineAtTheEnd(): void
static::assertSame($expected, $json);
}


/**
* @return void
*/
public function testIndentBy5AndSortedKeys(): void
{
$testFile = $this->createRandomFile();
Expand All @@ -91,9 +111,12 @@ public function testIndentBy5AndSortedKeys(): void
static::assertSame($expected, $json);
}

/**
* @return string
*/
private function createRandomFile(): string
{
$result = \tempnam(\sys_get_temp_dir(), 'phpunuhiJsonStorageTest');
$result = (string)\tempnam(\sys_get_temp_dir(), 'phpunuhiJsonStorageTest');

$this->files[] = $result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use PHPUnuhi\Configuration\Services\StyleLoader;
use PHPUnuhi\Models\Configuration\CaseStyle;
use SimpleXMLElement;

class StyleLoaderTest extends TestCase
{
Expand All @@ -22,8 +23,7 @@ public function testLoadStyles(): void
</styles>
XML;

$xml = simplexml_load_string($xmlString);

$xml = $this->loadXml($xmlString);

$loader = new StyleLoader();
$result = $loader->loadStyles($xml);
Expand Down Expand Up @@ -55,7 +55,7 @@ public function testLoadWithoutStylesNode(): void
</root>
XML;

$xml = simplexml_load_string($xmlString);
$xml = $this->loadXml($xmlString);

$loader = new StyleLoader();
$result = $loader->loadStyles($xml);
Expand All @@ -75,11 +75,26 @@ public function testLoadWithInvalidStyle(): void
</styles>
XML;

$xml = simplexml_load_string($xmlString);
$xml = $this->loadXml($xmlString);

$loader = new StyleLoader();
$result = $loader->loadStyles($xml);

$this->assertCount(2, $result);
}

/**
* @param string $xml
* @return SimpleXMLElement
*/
private function loadXml(string $xml): SimpleXMLElement
{
$element = simplexml_load_string($xml);

if ($element === false) {
return new SimpleXMLElement('');
}

return $element;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CaseStyleValidatorFactoryTest extends TestCase
*/
public function testUnknownIdentifierThrowsException(): void
{
$factory = new CaseStyleValidatorFactory('', [], '');
$factory = new CaseStyleValidatorFactory();

$this->expectException(CaseStyleNotFoundException::class);

Expand All @@ -30,7 +30,7 @@ public function testUnknownIdentifierThrowsException(): void
*/
public function testValidatorFound(): void
{
$factory = new CaseStyleValidatorFactory('', [], '');
$factory = new CaseStyleValidatorFactory();

$validator = $factory->fromIdentifier('camel');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testIdentifier(): void
}

/**
* @return array[]
* @return array<mixed>
*/
public function getData(): array
{
Expand All @@ -49,6 +49,9 @@ public function getData(): array

/**
* @dataProvider getData
*
* @param bool $expectedValid
* @param string $text
* @return void
*/
public function testIsValid(bool $expectedValid, string $text): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testIdentifier(): void
}

/**
* @return array[]
* @return array<mixed>
*/
public function getData(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testIdentifier(): void
}

/**
* @return array[]
* @return array<mixed>
*/
public function getData(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testIdentifier(): void
}

/**
* @return array[]
* @return array<mixed>
*/
public function getData(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testIdentifier(): void
}

/**
* @return array[]
* @return array<mixed>
*/
public function getData(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testIdentifier(): void
}

/**
* @return array[]
* @return array<mixed>
*/
public function getData(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testIdentifier(): void
}

/**
* @return array[]
* @return array<mixed>
*/
public function getData(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testIdentifier(): void
}

/**
* @return array[]
* @return array<mixed>
*/
public function getData(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function testSingleHierarchyValidation(): void

/**
* @param Locale $locale
* @param array $caseStyles
* @param CaseStyle[] $caseStyles
* @return TranslationSet
*/
private function buildSet(Locale $locale, array $caseStyles): TranslationSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testAllValid(): void

$set = $this->buildSet([$localeDE, $localeEN]);

$storage = new JsonStorage(3, true);
$storage = new JsonStorage();

$result = $this->validator->validate($set, $storage);

Expand All @@ -76,7 +76,7 @@ public function testEmptyContentFound(): void

$set = $this->buildSet([$localeDE, $localeEN]);

$storage = new JsonStorage(3, true);
$storage = new JsonStorage();

$result = $this->validator->validate($set, $storage);

Expand All @@ -85,7 +85,7 @@ public function testEmptyContentFound(): void


/**
* @param array $locales
* @param Locale[] $locales
* @return TranslationSet
*/
private function buildSet(array $locales): TranslationSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testAllValid(): void

$set = $this->buildSet([$localeDE, $localeEN]);

$storage = new JsonStorage(3, true);
$storage = new JsonStorage();

$result = $this->validator->validate($set, $storage);

Expand All @@ -75,7 +75,7 @@ public function testMixedStructureFound(): void

$set = $this->buildSet([$localeDE, $localeEN]);

$storage = new JsonStorage(3, true);
$storage = new JsonStorage();


$result = $this->validator->validate($set, $storage);
Expand All @@ -85,7 +85,7 @@ public function testMixedStructureFound(): void


/**
* @param array $locales
* @param Locale[] $locales
* @return TranslationSet
*/
private function buildSet(array $locales): TranslationSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void
'en_US',
'en.json',
'ID-123',
'14'
14
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testAllValid(): void

$set = $this->buildSet([$localeDE, $localeEN]);

$storage = new JsonStorage(3, true);
$storage = new JsonStorage();

$validator = new DisallowedTextsRule([]);
$result = $validator->validate($set, $storage);
Expand All @@ -65,7 +65,7 @@ public function testDisallowedTextFound(): void

$set = $this->buildSet([$localeDE, $localeEN]);

$storage = new JsonStorage(3, true);
$storage = new JsonStorage();

$validator = new DisallowedTextsRule(
[
Expand All @@ -80,7 +80,7 @@ public function testDisallowedTextFound(): void


/**
* @param array $locales
* @param Locale[] $locales
* @return TranslationSet
*/
private function buildSet(array $locales): TranslationSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testDuplicateInDifferentLocalesIsValid(): void


/**
* @param array $locales
* @param Locale[] $locales
* @return TranslationSet
*/
private function buildSet(array $locales): TranslationSet
Expand Down
Loading

0 comments on commit 0a470ae

Please sign in to comment.