1
0
Fork 0
mirror of https://github.com/pnx/neotest-phpunit synced 2026-06-16 03:54:55 +02:00

convert tests from pest to phpunit

This commit is contained in:
Henrik Hautakoski 2025-09-29 15:14:53 +02:00
parent 8cbe882c2f
commit ac9177d15e
4 changed files with 86 additions and 92 deletions

View file

@ -1,5 +1,11 @@
<?php
it('is true')
->expect(fn () => true)
->toBeTrue();
use PHPUnit\Framework\TestCase;
class NestingTest extends TestCase
{
public function testIsTrue(): void
{
$this->assertTrue(true);
}
}

View file

@ -1,52 +1,78 @@
<?php
/**
* @var mixed $this
*/
use PHPUnit\Framework\TestCase;
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) {
$this->sut = $makeUser();
});
public function testClassConstructor(): void
{
$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)
->name->toBe('John')
->age->toBe(18)
->favorite_movies->toBeEmpty();
/**
* @group special-tests
*/
public function testTellName(): void
{
$this->assertIsString($this->sut->tellName());
$this->assertStringContainsString('John', $this->sut->tellName());
}
test('tellName', function () {
expect($this->sut)
->tellName()->toBeString()->toContain('John');
})
->group('special tests');
it('throws', function () {
public function testThrows(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('oops!');
throw new \Exception('oops!');
});
}
it('is skipped')->skip();
public function testIsSkipped(): void
{
$this->markTestSkipped('Skipped to mirror Pest test.');
}
it('can tellAge')
->expect($makeUser)
->tellAge()->toBeString()->toContain('18');
public function testCanTellAge(): void
{
$user = new User(18, 'John');
$this->assertIsString($user->tellAge());
$this->assertStringContainsString('18', $user->tellAge());
}
it('is false')->expect(true)->toBeFalse();
public function testIsFalse(): void
{
// Mirrors the original failing expectation in Pest
$this->assertFalse(true);
}
test('addFavoriteMovie')
->expect($makeUser)
->addFavoriteMovie('Avengers')
->toBeTrue()
->favorite_movies->toContain('Avengers')->toHaveLength(1);
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);
}
public function testRemoveFavoriteMovie(): void
{
$user = new User(18, 'John');
$this->assertTrue($user->addFavoriteMovie('Avengers'));
$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);

View file

@ -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()
{
// ..
}

View file

@ -1,9 +1,16 @@
<?php
test('example', function () {
expect(true)->toBeTrue();
});
use PHPUnit\Framework\TestCase;
test("double quote example", function() {
expect(true)->toBeTrue();
});
class ExampleTest extends TestCase
{
public function testExample(): void
{
$this->assertTrue(true);
}
public function testDoubleQuoteExample(): void
{
$this->assertTrue(true);
}
}