From b19cfa3759378b74cc74e74a05c8e28780a139f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sat, 29 Feb 2020 11:30:09 +0100 Subject: [PATCH] Fix: Split test cases used in static code analysis --- test/StaticAnalysis/Src/BaseModel.php | 17 +--- test/StaticAnalysis/Test/BaseModelTest.php | 82 +++++++------------ test/StaticAnalysis/Test/ProphesizeTest.php | 71 ++++++++++++++++ test/StaticAnalysis/Test/WillExtendTest.php | 40 +++++++++ .../StaticAnalysis/Test/WillImplementTest.php | 41 ++++++++++ 5 files changed, 185 insertions(+), 66 deletions(-) create mode 100644 test/StaticAnalysis/Test/ProphesizeTest.php create mode 100644 test/StaticAnalysis/Test/WillExtendTest.php create mode 100644 test/StaticAnalysis/Test/WillImplementTest.php diff --git a/test/StaticAnalysis/Src/BaseModel.php b/test/StaticAnalysis/Src/BaseModel.php index 059ac86..c4db22c 100644 --- a/test/StaticAnalysis/Src/BaseModel.php +++ b/test/StaticAnalysis/Src/BaseModel.php @@ -16,32 +16,21 @@ class BaseModel { /** - * @var string + * @var null|string */ private $foo; - /** - * @return string - */ - public function getFoo(): string + public function getFoo(): ?string { return $this->foo; } - /** - * @param string $foo - */ public function setFoo(string $foo): void { $this->foo = $foo; } - /** - * @param int $number - * - * @return int - */ - public function doubleTheNumber(int $number) + public function doubleTheNumber(int $number): int { return 2 * $number; } diff --git a/test/StaticAnalysis/Test/BaseModelTest.php b/test/StaticAnalysis/Test/BaseModelTest.php index f5ab955..4546e02 100644 --- a/test/StaticAnalysis/Test/BaseModelTest.php +++ b/test/StaticAnalysis/Test/BaseModelTest.php @@ -16,91 +16,69 @@ use JanGregor\Prophecy\Test\StaticAnalysis\Src\Bar; use JanGregor\Prophecy\Test\StaticAnalysis\Src\BaseModel; use JanGregor\Prophecy\Test\StaticAnalysis\Src\Baz; -use JanGregor\Prophecy\Test\StaticAnalysis\Src\Foo; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\Prophecy\ObjectProphecy; /** * @internal * - * @coversNothing + * @covers \JanGregor\Prophecy\Test\StaticAnalysis\Src\BaseModel */ final class BaseModelTest extends TestCase { - /** - * @var \JanGregor\Prophecy\Test\StaticAnalysis\Src\BaseModel|ObjectProphecy - */ - private $subject; - - public function testBasicProperty(): void + public function testDefaults(): void { - $word = 'bar'; - - $subject = new BaseModel(); - $subject->setFoo($word); + $model = new BaseModel(); - self::assertEquals($word, $subject->getFoo()); - self::assertEquals(4, $subject->doubleTheNumber(2)); + self::assertNull($model->getFoo()); } - public function testWithProphecy(): void + public function testCanSetAndGetFoo(): void { - $subject = $this->prophesize(BaseModel::class); - $subject->getFoo()->willReturn('bar'); + $foo = 'Hello!'; - $subject->doubleTheNumber(Argument::is(2))->willReturn(5); + $model = new BaseModel(); - self::assertEquals('bar', $subject->reveal()->getFoo()); - self::assertEquals(5, $subject->reveal()->doubleTheNumber(2)); - } + $model->setFoo($foo); - /** - * @before - */ - public function createSubject(): void - { - $this->subject = $this->prophesize(BaseModel::class); + self::assertSame($foo, $model->getFoo()); } - public function testProphesizedAttributesShouldAlsoWork(): void + public function testCanDoubleTheNumber(): void { - $this->subject->getFoo()->willReturn('bar'); - $this->subject->doubleTheNumber(Argument::is(2))->willReturn(5); + $number = 9000; - $subject = $this->subject->reveal(); + $model = new BaseModel(); - self::assertEquals('bar', $subject->getFoo()); - self::assertEquals(5, $subject->doubleTheNumber(2)); + self::assertSame(2 * $number, $model->doubleTheNumber($number)); } - public function testWillExtendWorks(): void + public function testBarReturnsBar(): void { - $baz = $this->prophesize()->willExtend(Baz::class); + $value = 'Hmm'; - $baz - ->baz() - ->shouldBeCalled() - ->willReturn('Hmm'); + $bar = $this->prophesize(Bar::class); - $subject = new BaseModel(); + $bar + ->bar() + ->willReturn($value); + + $model = new BaseModel(); - self::assertSame('Hmm', $subject->baz($baz->reveal())); + self::assertSame($value, $model->bar($bar->reveal())); } - public function testWillImplementWorks(): void + public function testBazReturnsBaz(): void { - $fooThatAlsoBars = $this->prophesize(Foo::class); + $value = 'Ah!'; - $fooThatAlsoBars->willImplement(Bar::class); + $baz = $this->prophesize(Baz::class); - $fooThatAlsoBars - ->bar() - ->shouldBeCalled() - ->willReturn('Oh'); + $baz + ->baz() + ->willReturn($value); - $subject = new BaseModel(); + $model = new BaseModel(); - self::assertSame('Oh', $subject->bar($fooThatAlsoBars->reveal())); + self::assertSame($value, $model->baz($baz->reveal())); } } diff --git a/test/StaticAnalysis/Test/ProphesizeTest.php b/test/StaticAnalysis/Test/ProphesizeTest.php new file mode 100644 index 0000000..5af9815 --- /dev/null +++ b/test/StaticAnalysis/Test/ProphesizeTest.php @@ -0,0 +1,71 @@ +prophecy = $this->prophesize(BaseModel::class); + } + + public function testInSetUp(): void + { + $this->prophecy + ->getFoo() + ->willReturn('bar'); + + $this->prophecy + ->doubleTheNumber(Argument::is(2)) + ->willReturn(5); + + $testDouble = $this->prophecy->reveal(); + + self::assertEquals('bar', $testDouble->getFoo()); + self::assertEquals(5, $testDouble->doubleTheNumber(2)); + } + + public function testInTestMethod(): void + { + $prophecy = $this->prophesize(BaseModel::class); + + $prophecy + ->getFoo() + ->willReturn('bar'); + + $prophecy + ->doubleTheNumber(Argument::is(2)) + ->willReturn(5); + + $testDouble = $prophecy->reveal(); + + self::assertEquals('bar', $testDouble->getFoo()); + self::assertEquals(5, $testDouble->doubleTheNumber(2)); + } +} diff --git a/test/StaticAnalysis/Test/WillExtendTest.php b/test/StaticAnalysis/Test/WillExtendTest.php new file mode 100644 index 0000000..0750235 --- /dev/null +++ b/test/StaticAnalysis/Test/WillExtendTest.php @@ -0,0 +1,40 @@ +prophesize()->willExtend(Baz::class); + + $prophecy + ->baz() + ->shouldBeCalled() + ->willReturn('Hmm'); + + $subject = new BaseModel(); + + self::assertSame('Hmm', $subject->baz($prophecy->reveal())); + } +} diff --git a/test/StaticAnalysis/Test/WillImplementTest.php b/test/StaticAnalysis/Test/WillImplementTest.php new file mode 100644 index 0000000..92f24c9 --- /dev/null +++ b/test/StaticAnalysis/Test/WillImplementTest.php @@ -0,0 +1,41 @@ +prophesize(Foo::class)->willImplement(Bar::class); + + $prophecy + ->bar() + ->shouldBeCalled() + ->willReturn('Oh'); + + $subject = new BaseModel(); + + self::assertSame('Oh', $subject->bar($prophecy->reveal())); + } +}