mirror of
https://github.com/pnx/neotest-phpunit
synced 2026-06-17 04:00:03 +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,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');
|
||||
public function testThrows(): void
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('oops!');
|
||||
throw new \Exception('oops!');
|
||||
}
|
||||
|
||||
it('throws', function () {
|
||||
throw new \Exception('oops!');
|
||||
});
|
||||
public function testIsSkipped(): void
|
||||
{
|
||||
$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')
|
||||
->expect($makeUser)
|
||||
->tellAge()->toBeString()->toContain('18');
|
||||
public function testIsFalse(): void
|
||||
{
|
||||
// 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')
|
||||
->expect($makeUser)
|
||||
->addFavoriteMovie('Avengers')
|
||||
->toBeTrue()
|
||||
->favorite_movies->toContain('Avengers')->toHaveLength(1);
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue