PHP code example of ikvasnica / phpstan-clean-test
1. Go to this page and download the library: Download ikvasnica/phpstan-clean-test library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ikvasnica / phpstan-clean-test example snippets
// tests/ExampleTestCase/Unit/UnitExtendsInvalidTest.php
namespace ExampleTestCase\Unit;
final class UnitExtendsInvalidTest extends \Dummy\FunctionalDummyTest {}
// tests/ExampleTestCase/Unit/UnitExtendsUnitTest.php
namespace ExampleTestCase\Unit;
final class UnitExtendsUnitTest extends \PHPUnit\Framework\TestCase {}
// tests/ExampleTestCase/Unit/DisallowSetupConstructInvaliTest.php
namespace ExampleTestCase\Unit;
use PHPUnit\Framework\Assert;
final class DisallowSetupConstructInvaliTest extends \PHPUnit\Framework\TestCase
{
private $something;
public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
}
protected function setUp(): void
{
parent::setUp();
$this->something = true;
}
public function testSomeThing(): void
{
Assert::assertTrue($this->something);
}
}
// tests/ExampleTestCase/Unit/DisallowSetupConstructOkTest.php
namespace ExampleTestCase\Unit;
use PHPUnit\Framework\Assert;
final class DisallowSetupConstructOkTest extends \PHPUnit\Framework\TestCase
{
public function testSomeThing(): void
{
Assert::assertTrue(true);
}
}
// tests/ExampleTestCase/Unit/InvalidAssertEqualsUses.php
use PHPUnit\Framework\Assert;
$booleanValue = false;
$exception = new Exception('A bad thing has happened.');
Assert::assertEquals(true, $booleanValue);
Assert::assertEquals('exception message', (string) $exception);
// tests/ExampleTestCase/Unit/ValidAsserts.php
use PHPUnit\Framework\Assert;
$booleanValue = false;
$exception = new Exception('A bad thing has happened.');
$emptyArray = [];
Assert::assertTrue($booleanValue);
Assert::assertSame('exception message', (string) $exception);
Assert::assertEquals([], $emptyArray);
// tests/ExampleTestCase/Unit/InvalidAssertUsage.php
namespace ExampleTestCase;
final class InvalidAssertUsageTest extends \PHPUnit\Framework\TestCase
{
public function dummyTest(): void
{
// will fail
$this->assertSame(5, 5);
$this->assertTrue(false);
self::assertArrayHasKey(5, [5]);
static::assertCount(0, []);
\ExampleTestCase\StaticAssertOverThisAndStaticRule::assertTrue(true);
InvalidAssertUsageTest::assertTrue(true);
}
}
// tests/ExampleTestCase/Unit/ValidAssertsUsage.php
namespace ExampleTestCase;
use PHPUnit\Framework\Assert;
final class ValidAssertUsageTest extends \PHPUnit\Framework\TestCase
{
public function dummyTest(): void
{
// Assert::anything is OK
Assert::assertEquals(5, 5);
Assert::assertCount(1, [1, 2]);
Assert::assertTrue(false);
\PHPUnit\Framework\Assert::assertTrue(true);
}
}
// tests/ExampleTestCase/Unit/NullableArgumentsInTest.php
namespace ExampleTestCase;
final class NullableArgumentsInTest extends \PHPUnit\Framework\TestCase
{
public function testSomething($mixedTypeArgument, ?string $stringOrNullArgument): void
{
// will fail, because the first argument has no type and the second one is nullable
}
/**
* @test
* @param string|null $maybeString
*/
public function someTestMethod(?string $maybeString): void
{
// will fail too, because this one is a test method due to the annotation
}
}
// tests/ExampleTestCase/Unit/ValidArgumentsInTest.php
namespace ExampleTestCase;
final class ValidArgumentsInTest extends \PHPUnit\Framework\TestCase
{
public function testSomething(string $definitelyString): void
{
// this is a way to go!
}
public function testSomethingElse(): void
{
// no arguments are, of course, allowed
}
public function anotherPublicMethod(?bool $maybeTrueOrFalse): void
{
// although weird, you may have other non-test public methods with a nullable type as well
}
private function testHelperMethod(?string $maybeString): void
{
// private and protected methods are allowed to have nullable arguments
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.