File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66 - composer install
77
88script :
9- - vendor/bin/phpunit
9+ - vendor/bin/phpunit --exclude-group=dummy
Original file line number Diff line number Diff line change 33
44use Bigcommerce \Injector \InjectorInterface ;
55use 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments