Skip to content

Commit

Permalink
added type inference tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 9, 2021
1 parent ee2c2b8 commit 2b9e798
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"nette/tester": "^2.0",
"latte/latte": "^2.10.2",
"tracy/tracy": "^2.4",
"phpstan/phpstan-nette": "^0.12"
"phpstan/phpstan-nette": "^1.0"
},
"conflict": {
"nette/di": "<3.0-stable",
Expand Down
31 changes: 31 additions & 0 deletions tests/types/ControlsReturnTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* @phpVersion 8.0
*/

declare(strict_types=1);

require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/TestCase.php';


class ControlsReturnTypeTest extends PHPStan\Testing\TypeInferenceTestCase
{
/** @return iterable<mixed> */
public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/Controls.getValue().php');
}


/** @dataProvider dataFileAsserts */
public function testFileAsserts(string $assertType, string $file, mixed ...$args): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}
}


$testCase = new ControlsReturnTypeTest;
$testCase->run();
22 changes: 22 additions & 0 deletions tests/types/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace PHPUnit\Framework;

use Tester\Assert;


abstract class TestCase extends \Tester\TestCase
{
protected function assertSame(mixed $expected, mixed $actual, string $message = ''): void
{
Assert::same($expected, $actual, $message);
}


protected function assertTrue(mixed $actual, string $message = ''): void
{
Assert::true($actual, $message);
}
}
60 changes: 60 additions & 0 deletions tests/types/data/Controls.getValue().php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

use Nette\Forms\Form;
use function PHPStan\Testing\assertType;


$form = new Form;

$input = $form->addText('Text');
assertType('string|null', $input->getValue());

$input = $form->addPassword('Password');
assertType('string|null', $input->getValue());

$input = $form->addTextArea('TextArea');
assertType('string|null', $input->getValue());

$input = $form->addEmail('Email');
assertType('string|null', $input->getValue());

$input = $form->addInteger('Integer');
assertType('string|null', $input->getValue());

$input = $form->addUpload('Upload');
assertType('array<Nette\Http\FileUpload>|Nette\Http\FileUpload|null', $input->getValue());

$input = $form->addMultiUpload('MultiUpload');
assertType('array<Nette\Http\FileUpload>|Nette\Http\FileUpload|null', $input->getValue());

$input = $form->addHidden('Hidden');
assertType('string|null', $input->getValue());

$input = $form->addCheckbox('Checkbox');
assertType('bool|null', $input->getValue());

$input = $form->addRadioList('RadioList');
assertType('int|string|null', $input->getValue());

$input = $form->addCheckboxList('CheckboxList');
assertType('array<(int|string)>', $input->getValue());

$input = $form->addSelect('Select');
assertType('int|string|null', $input->getValue());

$input = $form->addMultiSelect('MultiSelect');
assertType('array<(int|string)>', $input->getValue());

$input = $form->addSubmit('Submit');
assertType('string|null', $input->getValue());

$input = $form->addButton('Button');
assertType('string|null', $input->getValue());

$input = $form->addImageButton('ImageButton');
assertType('array|null', $input->getValue());

$input = $form->addImage('Image');
assertType('array|null', $input->getValue());

0 comments on commit 2b9e798

Please sign in to comment.