Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the implementation to use only a trait. #20

Merged
merged 2 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 13 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,6 @@
Prophecy PhpUnit integrates the [Prophecy](https://github.com/phpspec/prophecy) mocking
library with [PHPUnit](https://phpunit.de) to provide an easier mocking in your testsuite.

## Usage

```php
<?php

use Prophecy\PhpUnit\ProphecyTestCase;

class UserTest extends ProphecyTestCase
{
public function testPasswordHashing()
{
$hasher = $this->prophesize('App\Security\Hasher');
$user = new App\Entity\User($hasher->reveal());

$hasher->generateHash($user, 'qwerty')->willReturn('hashed_pass');

$user->setPassword('qwerty');

$this->assertEquals('hashed_pass', $user->getPassword());
}
}
```

## Installation

### Prerequisites
Expand All @@ -45,33 +22,35 @@ You can read more about Composer on its [official webpage](https://getcomposer.o

## How to use it

The special ``ProphecyTestCase`` exposes a method ``prophesize($classOrInterface = null)``
to use Prophecy.
The trait ``ProphecyTrait`` provides a method ``prophesize($classOrInterface = null)`` to use Prophecy.
For the usage of the Prophecy doubles, please refer to the [Prophecy documentation](https://github.com/phpspec/prophecy).

If you need to use the Prophecy integration alongside a custom base TestCase rather than the PHPUnit one, a trait is available with all the necessary logic, except the override of the PHPUnit `verifyMockObjects` method (which cannot be provided by a trait). Use it like that:
Below is a usage example:

```php
<?php

namespace App;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use App\Security\Hasher;
use App\Entity\User;

class MyCustomTest extends ExternalTestCase
class UserTest extends TestCase
{
use ProphecyTrait;

protected function verifyMockObjects(): void
public function testPasswordHashing()
{
parent::verifyMockObjects();
$hasher = $this->prophesize(Hasher::class);
$user = new User($hasher->reveal());

$this->verifyProphecyDoubles();
}
$hasher->generateHash($user, 'qwerty')->willReturn('hashed_pass');

public function testSomething()
{
// You have the same features than when extending ProphecyTestCase now.
$user->setPassword('qwerty');

$this->assertEquals('hashed_pass', $user->getPassword());
}
}
```
7 changes: 5 additions & 2 deletions fixtures/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Prophecy\PhpUnit\Tests\Fixtures;

use Prophecy\PhpUnit\ProphecyTestCase;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class Error extends ProphecyTestCase
class Error extends TestCase
{
use ProphecyTrait;

public function testMethod()
{
$prophecy = $this->prophesize('stdClass');
Expand Down
7 changes: 5 additions & 2 deletions fixtures/MockFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Prophecy\PhpUnit\Tests\Fixtures;

use Prophecy\PhpUnit\ProphecyTestCase;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class MockFailure extends ProphecyTestCase
class MockFailure extends TestCase
{
use ProphecyTrait;

public function testMethod()
{
$prophecy = $this->prophesize('DateTime');
Expand Down
7 changes: 5 additions & 2 deletions fixtures/SpyFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Prophecy\PhpUnit\Tests\Fixtures;

use Prophecy\PhpUnit\ProphecyTestCase;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class SpyFailure extends ProphecyTestCase
class SpyFailure extends TestCase
{
use ProphecyTrait;

public function testMethod()
{
$prophecy = $this->prophesize('DateTime');
Expand Down
7 changes: 5 additions & 2 deletions fixtures/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Prophecy\PhpUnit\Tests\Fixtures;

use Prophecy\PhpUnit\ProphecyTestCase;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class Success extends ProphecyTestCase
class Success extends TestCase
{
use ProphecyTrait;

public function testMethod()
{
$prophecy = $this->prophesize('DateTime');
Expand Down
17 changes: 0 additions & 17 deletions src/ProphecyTestCase.php

This file was deleted.

7 changes: 5 additions & 2 deletions src/ProphecyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ protected function prophesize(?string $classOrInterface = null): ObjectProphecy
return $this->getProphet()->prophesize($classOrInterface);
}

private function verifyProphecyDoubles(): void
/**
* @postCondition
*/
protected function verifyProphecyDoubles(): void
{
if ($this->prophet === null) {
return;
Expand All @@ -64,7 +67,7 @@ private function verifyProphecyDoubles(): void
/**
* @after
*/
protected function prophecyTearDown(): void
protected function tearDownProphecy(): void
{
if (null !== $this->prophet && !$this->prophecyAssertionsCounted) {
// Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Prophecy\PhpUnit\Tests\Fixtures\Success;

/**
* @covers \Prophecy\PhpUnit\ProphecyTestCase
* @covers \Prophecy\PhpUnit\ProphecyTrait
*/
final class ProphecyTestCaseTest extends TestCase
final class ProphecyTraitTest extends TestCase
{
protected function setUp(): void
{
Expand Down