Skip to content

Commit

Permalink
Fix: Split test cases used in static code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Feb 29, 2020
1 parent c804947 commit b19cfa3
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 66 deletions.
17 changes: 3 additions & 14 deletions test/StaticAnalysis/Src/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
82 changes: 30 additions & 52 deletions test/StaticAnalysis/Test/BaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
71 changes: 71 additions & 0 deletions test/StaticAnalysis/Test/ProphesizeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Jan Gregor Emge-Triebel
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/Jan0707/phpstan-prophecy
*/

namespace JanGregor\Prophecy\Test\StaticAnalysis\Test;

use JanGregor\Prophecy\Test\StaticAnalysis\Src\BaseModel;
use PHPUnit\Framework;
use Prophecy\Argument;
use Prophecy\Prophecy;

/**
* @internal
*
* @coversNothing
*/
final class ProphesizeTest extends Framework\TestCase
{
/**
* @var BaseModel|Prophecy\ObjectProphecy
*/
private $prophecy;

protected function setUp(): void
{
$this->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));
}
}
40 changes: 40 additions & 0 deletions test/StaticAnalysis/Test/WillExtendTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Jan Gregor Emge-Triebel
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/Jan0707/phpstan-prophecy
*/

namespace JanGregor\Prophecy\Test\StaticAnalysis\Test;

use JanGregor\Prophecy\Test\StaticAnalysis\Src\BaseModel;
use JanGregor\Prophecy\Test\StaticAnalysis\Src\Baz;
use PHPUnit\Framework\TestCase;

/**
* @internal
*
* @coversNothing
*/
final class WillExtendTest extends TestCase
{
public function testInTestMethod(): void
{
$prophecy = $this->prophesize()->willExtend(Baz::class);

$prophecy
->baz()
->shouldBeCalled()
->willReturn('Hmm');

$subject = new BaseModel();

self::assertSame('Hmm', $subject->baz($prophecy->reveal()));
}
}
41 changes: 41 additions & 0 deletions test/StaticAnalysis/Test/WillImplementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Jan Gregor Emge-Triebel
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/Jan0707/phpstan-prophecy
*/

namespace JanGregor\Prophecy\Test\StaticAnalysis\Test;

use JanGregor\Prophecy\Test\StaticAnalysis\Src\Bar;
use JanGregor\Prophecy\Test\StaticAnalysis\Src\BaseModel;
use JanGregor\Prophecy\Test\StaticAnalysis\Src\Foo;
use PHPUnit\Framework;

/**
* @internal
*
* @coversNothing
*/
final class WillImplementTest extends Framework\TestCase
{
public function testInTestMethod(): void
{
$prophecy = $this->prophesize(Foo::class)->willImplement(Bar::class);

$prophecy
->bar()
->shouldBeCalled()
->willReturn('Oh');

$subject = new BaseModel();

self::assertSame('Oh', $subject->bar($prophecy->reveal()));
}
}

0 comments on commit b19cfa3

Please sign in to comment.