Skip to content

Commit 0f87c3c

Browse files
authored
Merge pull request #8 from petrkotek/risky-tests-fix
Risky Tests Handling: Test shouldn't be marked as Risky when it has Prophecy predictions
2 parents d26c4b2 + 90a3787 commit 0f87c3c

4 files changed

Lines changed: 81 additions & 2 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ install:
66
- composer install
77

88
script:
9-
- vendor/bin/phpunit
9+
- vendor/bin/phpunit --exclude-group=dummy

src/AutoMockingTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use Bigcommerce\Injector\InjectorInterface;
55
use PHPUnit\Framework\TestCase;
6+
use Prophecy\Prophecy\MethodProphecy;
7+
use Prophecy\Prophet;
68

79
/**
810
* Configured PHPUnit TestCase providing auto-mocking of dependency injection components using the
@@ -15,14 +17,18 @@ abstract class AutoMockingTest extends TestCase
1517
/** @var InjectorInterface|MockInjector */
1618
protected $injector;
1719

20+
/** @var Prophet */
21+
private $mockingContainerProphet;
22+
1823
/**
1924
* Sets up the fixture, for example, open a network connection.
2025
* This method is called before a test is executed.
2126
*/
2227
protected function setUp()
2328
{
2429
parent::setUp();
25-
$this->injector = new MockInjector();
30+
$this->mockingContainerProphet = new Prophet();
31+
$this->injector = new MockInjector(new ProphecyMockingContainer($this->mockingContainerProphet));
2632
}
2733

2834
/**
@@ -44,5 +50,14 @@ protected function tearDown()
4450
{
4551
parent::tearDown();
4652
$this->injector->checkPredictions();
53+
54+
foreach ($this->mockingContainerProphet->getProphecies() as $objectProphecy) {
55+
foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
56+
/** @var MethodProphecy[] $methodProphecies */
57+
foreach ($methodProphecies as $methodProphecy) {
58+
$this->addToAssertionCount(count($methodProphecy->getCheckedPredictions()));
59+
}
60+
}
61+
}
4762
}
4863
}

tests/AutoMockingTestTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Tests\Dummy\DummyTest;
8+
9+
class AutoMockingTestTest extends TestCase
10+
{
11+
public function testTestShouldNotBeRiskyWhenItHasProphecyExpectations()
12+
{
13+
$riskyTest = new DummyTest('testWithProphecyExpectations');
14+
15+
$riskyTest->runBare();
16+
17+
$this->assertSame(1, $riskyTest->getNumAssertions());
18+
}
19+
20+
public function testTestShouldBeRiskyWhenItHasNoAssertions()
21+
{
22+
$riskyTest = new DummyTest('testWithoutAssertions');
23+
24+
$riskyTest->runBare();
25+
26+
$this->assertSame(0, $riskyTest->getNumAssertions());
27+
}
28+
}

tests/Dummy/DummyTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Tests\Dummy;
5+
6+
use Bigcommerce\MockInjector\AutoMockingTest;
7+
use Prophecy\Prophecy\ObjectProphecy;
8+
9+
/**
10+
* @group dummy
11+
*/
12+
class DummyTest extends AutoMockingTest
13+
{
14+
/**
15+
* This is a dummy test which is called by AutoMockInjectorTestTest. It has one expectation configured using
16+
* Prophecy (see `->shouldBeCalled()`), therefore there is 1 assertion and the test shouldn't be marked as Risky.
17+
*/
18+
public function testWithProphecyExpectations()
19+
{
20+
/** @var DummyDependency $dummyDependency */
21+
$dummyDependency = $this->createWithMocks(DummyDependency::class);
22+
23+
/** @var DummySubDependency|ObjectProphecy $subDependency */
24+
$subDependency = $this->injector->getProphecy(DummySubDependency::class);
25+
$subDependency->setEnabled(true)->shouldBeCalled();
26+
27+
$dummyDependency->getDependency()->setEnabled(true);
28+
}
29+
30+
/**
31+
* This is a dummy test which doesn't have any assertions, therefore is risky.
32+
*/
33+
public function testWithoutAssertions()
34+
{
35+
}
36+
}

0 commit comments

Comments
 (0)