Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from ekvedaras/analysis-qyjZRG
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
ekvedaras committed Aug 12, 2018
2 parents d2919a6 + 5d48fb0 commit aba5d52
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 130 deletions.
6 changes: 2 additions & 4 deletions src/Exceptions/MockInjectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace Ekvedaras\LaravelTestHelpers\Exceptions;


use Exception;

/**
* Class MockInjectionException
* @package Ekvedaras\LaravelTestHelpers\Exceptions
* Class MockInjectionException.
*/
class MockInjectionException extends Exception
{
Expand All @@ -20,4 +18,4 @@ public static function injectorNotGiven(): self
{
return new static('Injector class must be specified when using "onlyForInjector"');
}
}
}
11 changes: 4 additions & 7 deletions src/Helpers/TestHelpersMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace Ekvedaras\LaravelTestHelpers\Helpers;

/**
* Class TestHelpersMock
* @package Ekvedaras\LaravelTestHelpers\Helpers
* Class TestHelpersMock.
* @method PHPUnit\Framework\MockObject\Builder\InvocationMocker method($constraint)
*/
class TestHelpersMock implements \PHPUnit_Framework_MockObject_MockObject
Expand Down Expand Up @@ -91,8 +90,7 @@ public function consecutive(
int $times,
string $method,
...$vars
): \PHPUnit\Framework\MockObject\Builder\InvocationMocker
{
): \PHPUnit\Framework\MockObject\Builder\InvocationMocker {
return $this->getMock()
->expects(\PHPUnit\Framework\TestCase::exactly($times))
->method($method)
Expand All @@ -118,8 +116,7 @@ public function fail(
string $method,
\Exception $exception,
...$vars
): \PHPUnit\Framework\MockObject\Builder\InvocationMocker
{
): \PHPUnit\Framework\MockObject\Builder\InvocationMocker {
return $this->any($method, ...$vars)->willThrowException($exception);
}
}
}
53 changes: 23 additions & 30 deletions src/Traits/Helpers/BuildsMocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@

namespace Ekvedaras\LaravelTestHelpers\Traits\Helpers;

use Ekvedaras\LaravelTestHelpers\Exceptions\MockInjectionException;
use Ekvedaras\LaravelTestHelpers\Helpers\TestHelpersMock;
use Illuminate\Foundation\Application;
use Mockery;
use ReflectionClass;
use ReflectionException;
use Mockery\Instantiator;
use Mockery\MockInterface;
use PHPUnit\Framework\MockObject\MockBuilder;
use Illuminate\Foundation\Application;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit_Framework_MockObject_MockObject;
use ReflectionClass;
use ReflectionException;
use PHPUnit\Framework\MockObject\MockBuilder;
use Ekvedaras\LaravelTestHelpers\Helpers\TestHelpersMock;
use Ekvedaras\LaravelTestHelpers\Exceptions\MockInjectionException;

/**
* Trait BuildsMocks
* @package Ekvedaras\LaravelTestHelpers\Helpers
* Trait BuildsMocks.
*
* @property-read Application $app
* @method MockBuilder getMockBuilder(string $mockClass)
Expand All @@ -39,8 +38,7 @@ protected function mock(
$methods = false,
array $constructorArgs = null,
bool $onlyForInjector = false
): PHPUnit_Framework_MockObject_MockObject
{
): PHPUnit_Framework_MockObject_MockObject {
$this->forgetInstances($mockClass, $injectorClass);
$mock = $this->createPHPUnitMock($mockClass, $constructorArgs, $methods);
$this->injectMockToLaravel($mockClass, $mock, $onlyForInjector, $injectorClass);
Expand All @@ -58,8 +56,7 @@ protected function mockery(
string $mockClass,
string $injectorClass = null,
bool $onlyForInjector = false
): MockInterface
{
): MockInterface {
$this->forgetInstances($mockClass, $injectorClass);
$mock = Mockery::mock($mockClass);
$this->injectMockToLaravel($mockClass, $mock, $onlyForInjector, $injectorClass);
Expand All @@ -77,8 +74,7 @@ protected function spy(
string $mockClass,
string $injectorClass = null,
bool $onlyForInjector = false
): MockInterface
{
): MockInterface {
$this->forgetInstances($mockClass, $injectorClass);
$mock = Mockery::spy($mockClass);
$this->injectMockToLaravel($mockClass, $mock, $onlyForInjector, $injectorClass);
Expand Down Expand Up @@ -110,8 +106,7 @@ private function createPHPUnitMock(
string $mockClass,
array $constructorArgs = null,
$methods = false
): PHPUnit_Framework_MockObject_MockObject
{
): PHPUnit_Framework_MockObject_MockObject {
$builder = $this->getMockBuilder($mockClass);

if (isset($constructorArgs)) {
Expand All @@ -127,9 +122,9 @@ private function createPHPUnitMock(
$mock = $builder->getMock();
$mockedClass = get_class($mock);
$helperClass = $this->getClassShortName(TestHelpersMock::class);
$wrapperClass = $helperClass . '_' . $mockedClass . '_' . str_random();
$wrapperClass = $helperClass.'_'.$mockedClass.'_'.str_random();

$template = file_get_contents(__DIR__ . '/../../Helpers/TestHelpersMock.php');
$template = file_get_contents(__DIR__.'/../../Helpers/TestHelpersMock.php');
$template = str_replace("class $helperClass", "class $wrapperClass extends $mockedClass", $template);
$template = substr($template, strpos($template, "class $wrapperClass"));

Expand All @@ -138,8 +133,8 @@ private function createPHPUnitMock(
$wrapperClass,
$mock,
$mockClass,
!is_null($constructorArgs),
(array)$constructorArgs
! is_null($constructorArgs),
(array) $constructorArgs
);
}

Expand All @@ -162,16 +157,15 @@ private function getObject(
$proxyTarget,
string $type = '',
bool $callOriginalConstructor = false,
array $arguments = [ ],
array $arguments = [],
bool $callAutoload = false,
bool $returnValueGeneration = true
)
{
) {
$this->evalClass($code, $className);

if ($callOriginalConstructor &&
is_string($type) &&
!interface_exists($type, $callAutoload)) {
! interface_exists($type, $callAutoload)) {
if (count($arguments) === 0) {
$object = new $className;
} else {
Expand All @@ -198,7 +192,7 @@ private function getObject(
*/
private function evalClass(string $code, string $className): void
{
if (!class_exists($className, false)) {
if (! class_exists($className, false)) {
eval($code);
}
}
Expand All @@ -215,14 +209,13 @@ private function injectMockToLaravel(
$mock,
bool $onlyForInjector = false,
string $injectorClass = null
): void
{
): void {
if ($onlyForInjector) {
if (!isset($injectorClass)) {
if (! isset($injectorClass)) {
throw MockInjectionException::injectorNotGiven();
}

$this->app->when($injectorClass)->needs($mockClass)->give(function() use ($mock) {
$this->app->when($injectorClass)->needs($mockClass)->give(function () use ($mock) {
return $mock;
});
} else {
Expand All @@ -238,4 +231,4 @@ private function getClassShortName(string $class): string
{
return substr(strrchr($class, '\\'), 1);
}
}
}
7 changes: 3 additions & 4 deletions src/Traits/Helpers/ChecksSingletons.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Illuminate\Foundation\Application;

/**
* Trait ChecksSingletons
* @package Ekvedaras\LaravelTestHelpers\Helpers
* Trait ChecksSingletons.
*
* @property-read Application $app
* @method void assertSame($expected, $actual, $message = '')
Expand All @@ -23,7 +22,7 @@ protected function assertSingleton(string $class): void
$this->assertSame(
$this->app->make($class),
$this->app->make($class),
$class . ' must be registered as singleton'
$class.' must be registered as singleton'
);
}
}
}
5 changes: 2 additions & 3 deletions src/Traits/Helpers/TestsCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use Symfony\Component\Console\Tester\CommandTester;

/**
* Trait TestsCommands
* @package Ekvedaras\LaravelTestHelpers\Helpers
* Trait TestsCommands.
*
* @property-read Application $app
*/
Expand All @@ -28,4 +27,4 @@ protected function getCommandTester(string $commandClass): CommandTester

return new CommandTester($command);
}
}
}
18 changes: 9 additions & 9 deletions src/Traits/PortToMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

namespace Ekvedaras\LaravelTestHelpers\Traits;

use PHPUnit\Framework\MockObject\Matcher\Invocation;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\Matcher\Invocation;

/**
* Trait PortToMock
* @package Ekvedaras\LaravelTestHelpers\Traits
* Trait PortToMock.
*/
trait PortToMock
{
Expand All @@ -19,40 +18,41 @@ trait PortToMock
abstract public function getMock(): PHPUnit_Framework_MockObject_MockObject;

/**
* @inheritdoc
* {@inheritdoc}
*/
public function expects(Invocation $matcher)
{
return $this->getMock()->expects($matcher);
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function __phpunit_setOriginalObject($originalObject)
{
$this->mock = $originalObject;

return $this->getMock();
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function __phpunit_getInvocationMocker()
{
return $this->getMock()->__phpunit_getInvocationMocker();
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function __phpunit_verify()
{
return $this->getMock()->__phpunit_verify();
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function __phpunit_hasMatchers()
{
Expand All @@ -67,4 +67,4 @@ public function __phpunit_setReturnValueGeneration(bool $returnValueGeneration)
{
return $this->getMock()->__phpunit_setReturnValueGeneration($returnValueGeneration);
}
}
}
7 changes: 3 additions & 4 deletions src/Traits/TestHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
namespace Ekvedaras\LaravelTestHelpers\Traits;

use Ekvedaras\LaravelTestHelpers\Traits\Helpers\BuildsMocks;
use Ekvedaras\LaravelTestHelpers\Traits\Helpers\ChecksSingletons;
use Ekvedaras\LaravelTestHelpers\Traits\Helpers\TestsCommands;
use Ekvedaras\LaravelTestHelpers\Traits\Helpers\ChecksSingletons;

/**
* Trait TestingHelpers
* @package Ekvedaras\LaravelTestHelpers\Traits
* Trait TestingHelpers.
*/
trait TestHelpers
{
use BuildsMocks,
ChecksSingletons,
TestsCommands;
}
}
9 changes: 4 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

namespace Tests;

use Carbon\Carbon;
use Exception;
use Carbon\Carbon;
use Orchestra\Testbench\TestCase as BaseTestCase;

/**
* Class TestCase
* @package Tests
* Class TestCase.
*/
class TestCase extends BaseTestCase
{
/**
* Set carbon date to avoid test timing issues
* Set carbon date to avoid test timing issues.
* @throws Exception
*/
protected function setUp()
Expand All @@ -24,4 +23,4 @@ protected function setUp()

Carbon::setTestNow(now());
}
}
}
Loading

0 comments on commit aba5d52

Please sign in to comment.