mirror of
https://github.com/pnx/neotest-phpunit
synced 2026-06-18 04:10:01 +02:00
convert tests from pest to phpunit
This commit is contained in:
parent
8cbe882c2f
commit
ac9177d15e
4 changed files with 86 additions and 92 deletions
|
|
@ -1,5 +1,11 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
it('is true')
|
use PHPUnit\Framework\TestCase;
|
||||||
->expect(fn () => true)
|
|
||||||
->toBeTrue();
|
class NestingTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testIsTrue(): void
|
||||||
|
{
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,52 +1,78 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
use PHPUnit\Framework\TestCase;
|
||||||
* @var mixed $this
|
|
||||||
*/
|
|
||||||
|
|
||||||
use TestProject\User;
|
use TestProject\User;
|
||||||
|
|
||||||
uses()->group('file group');
|
/**
|
||||||
|
* @group file-group
|
||||||
|
*/
|
||||||
|
class UserTest extends TestCase
|
||||||
|
{
|
||||||
|
private User $sut;
|
||||||
|
|
||||||
$makeUser = fn () => new User(18, 'John');
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->sut = new User(18, 'John');
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(function () use ($makeUser) {
|
public function testClassConstructor(): void
|
||||||
$this->sut = $makeUser();
|
{
|
||||||
});
|
$user = new User(18, 'John');
|
||||||
|
$this->assertSame('John', $user->name);
|
||||||
|
$this->assertSame(18, $user->age);
|
||||||
|
$this->assertEmpty($user->favorite_movies);
|
||||||
|
}
|
||||||
|
|
||||||
test('class constructor')
|
/**
|
||||||
->expect($makeUser)
|
* @group special-tests
|
||||||
->name->toBe('John')
|
*/
|
||||||
->age->toBe(18)
|
public function testTellName(): void
|
||||||
->favorite_movies->toBeEmpty();
|
{
|
||||||
|
$this->assertIsString($this->sut->tellName());
|
||||||
|
$this->assertStringContainsString('John', $this->sut->tellName());
|
||||||
|
}
|
||||||
|
|
||||||
test('tellName', function () {
|
public function testThrows(): void
|
||||||
expect($this->sut)
|
{
|
||||||
->tellName()->toBeString()->toContain('John');
|
$this->expectException(\Exception::class);
|
||||||
})
|
$this->expectExceptionMessage('oops!');
|
||||||
->group('special tests');
|
throw new \Exception('oops!');
|
||||||
|
}
|
||||||
|
|
||||||
it('throws', function () {
|
public function testIsSkipped(): void
|
||||||
throw new \Exception('oops!');
|
{
|
||||||
});
|
$this->markTestSkipped('Skipped to mirror Pest test.');
|
||||||
|
}
|
||||||
|
|
||||||
it('is skipped')->skip();
|
public function testCanTellAge(): void
|
||||||
|
{
|
||||||
|
$user = new User(18, 'John');
|
||||||
|
$this->assertIsString($user->tellAge());
|
||||||
|
$this->assertStringContainsString('18', $user->tellAge());
|
||||||
|
}
|
||||||
|
|
||||||
it('can tellAge')
|
public function testIsFalse(): void
|
||||||
->expect($makeUser)
|
{
|
||||||
->tellAge()->toBeString()->toContain('18');
|
// Mirrors the original failing expectation in Pest
|
||||||
|
$this->assertFalse(true);
|
||||||
|
}
|
||||||
|
|
||||||
it('is false')->expect(true)->toBeFalse();
|
public function testAddFavoriteMovie(): void
|
||||||
|
{
|
||||||
|
$user = new User(18, 'John');
|
||||||
|
$this->assertTrue($user->addFavoriteMovie('Avengers'));
|
||||||
|
$this->assertContains('Avengers', $user->favorite_movies);
|
||||||
|
$this->assertCount(1, $user->favorite_movies);
|
||||||
|
}
|
||||||
|
|
||||||
test('addFavoriteMovie')
|
public function testRemoveFavoriteMovie(): void
|
||||||
->expect($makeUser)
|
{
|
||||||
->addFavoriteMovie('Avengers')
|
$user = new User(18, 'John');
|
||||||
->toBeTrue()
|
$this->assertTrue($user->addFavoriteMovie('Avengers'));
|
||||||
->favorite_movies->toContain('Avengers')->toHaveLength(1);
|
$this->assertTrue($user->addFavoriteMovie('Justice League'));
|
||||||
|
$this->assertTrue($user->removeFavoriteMovie('Avengers'));
|
||||||
|
$this->assertNotContains('Avengers', $user->favorite_movies);
|
||||||
|
$this->assertCount(1, $user->favorite_movies);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test('removeFavoriteMovie')
|
|
||||||
->expect($makeUser)
|
|
||||||
->addFavoriteMovie('Avengers')->toBeTrue()
|
|
||||||
->addFavoriteMovie('Justice League')->toBeTrue()
|
|
||||||
->removeFavoriteMovie('Avengers')->toBeTrue()
|
|
||||||
->favorite_movies->not->toContain('Avengers')->toHaveLength(1);
|
|
||||||
|
|
|
||||||
|
|
@ -1,45 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Test Case
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| The closure you provide to your test functions is always bound to a specific PHPUnit test
|
|
||||||
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
|
|
||||||
| need to change it using the "uses()" function to bind a different classes or traits.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
// uses(Tests\TestCase::class)->in('Feature');
|
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Expectations
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| When you're writing tests, you often need to check that values meet certain conditions. The
|
|
||||||
| "expect()" function gives you access to a set of "expectations" methods that you can use
|
|
||||||
| to assert different things. Of course, you may extend the Expectation API at any time.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
expect()->extend('toBeOne', function () {
|
|
||||||
return $this->toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Functions
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
|
|
||||||
| project that you don't want to repeat in every file. Here you can also expose helpers as
|
|
||||||
| global functions to help you to reduce the number of lines of code in your test files.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
function something()
|
|
||||||
{
|
|
||||||
// ..
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +1,16 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
test('example', function () {
|
use PHPUnit\Framework\TestCase;
|
||||||
expect(true)->toBeTrue();
|
|
||||||
});
|
|
||||||
|
|
||||||
test("double quote example", function() {
|
class ExampleTest extends TestCase
|
||||||
expect(true)->toBeTrue();
|
{
|
||||||
});
|
public function testExample(): void
|
||||||
|
{
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDoubleQuoteExample(): void
|
||||||
|
{
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue