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 64d2628
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 71 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
90 changes: 33 additions & 57 deletions test/StaticAnalysis/Test/BaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,94 +13,70 @@

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\Baz;
use JanGregor\Prophecy\Test\StaticAnalysis\Src\Foo;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use JanGregor\Prophecy\Test\StaticAnalysis\Src;
use PHPUnit\Framework;

/**
* @internal
*
* @coversNothing
* @covers \JanGregor\Prophecy\Test\StaticAnalysis\Src\BaseModel
*/
final class BaseModelTest extends TestCase
final class BaseModelTest extends Framework\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 Src\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 Src\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 Src\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(Src\Bar::class);

$subject = new BaseModel();
$bar
->bar()
->willReturn($value);

$model = new Src\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(Src\Baz::class);

$fooThatAlsoBars
->bar()
->shouldBeCalled()
->willReturn('Oh');
$baz
->baz()
->willReturn($value);

$subject = new BaseModel();
$model = new Src\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;
use PHPUnit\Framework;
use Prophecy\Argument;
use Prophecy\Prophecy;

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

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

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

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

$subject = new Src\BaseModel();

self::assertSame('Hmm', $subject->baz($prophecy->reveal()));
}
}
39 changes: 39 additions & 0 deletions test/StaticAnalysis/Test/WillImplementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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;
use PHPUnit\Framework;

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

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

$subject = new Src\BaseModel();

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

0 comments on commit 64d2628

Please sign in to comment.