Skip to content

Commit

Permalink
Replaced mockbuilder use with mockery
Browse files Browse the repository at this point in the history
  • Loading branch information
czim committed Sep 23, 2019
1 parent 179ad43 commit 7cd7fe4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 39 deletions.
23 changes: 15 additions & 8 deletions tests/PipelineProcessorTest.php
Expand Up @@ -7,6 +7,7 @@
use Czim\Processor\Test\Helpers\TestPipelineProcessorWithInitStep;
use Exception;
use Illuminate\Support\Facades\DB;
use Mockery;

class PipelineProcessorTest extends TestCase
{
Expand All @@ -21,13 +22,19 @@ function it_takes_a_process_context_on_construction()
DB::shouldReceive('beginTransaction')->once();
DB::shouldReceive('commit')->once();

/** @var ProcessContextInterface $data */
$context = $this->getMockBuilder(ProcessContextInterface::class)->getMock();
/** @var ProcessContextInterface|Mockery\Mock|Mockery\MockInterface $data */
$context = Mockery::mock(ProcessContextInterface::class);

$processor = new TestPipelineProcessor([], $context);

/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
$data = Mockery::mock(DataObjectInterface::class);

$context->shouldReceive('setData');
$context->shouldReceive('setProcessor');
$context->shouldReceive('setSetting');
$context->shouldReceive('getData')->andReturn($data);


$processor->process($data);

Expand All @@ -45,7 +52,7 @@ function it_builds_a_standard_process_context_if_not_injected_on_construction()
$processor = new TestPipelineProcessor();

/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
$data = Mockery::mock(DataObjectInterface::class);

$processor->process($data);

Expand All @@ -66,7 +73,7 @@ function it_runs_an_init_step_if_defined()
$processor = new TestPipelineProcessorWithInitStep();

/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
$data = Mockery::mock(DataObjectInterface::class);

$processor->process($data);

Expand All @@ -88,7 +95,7 @@ function it_runs_a_main_step()
$processor = new TestPipelineProcessor();

/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
$data = Mockery::mock(DataObjectInterface::class);

$processor->process($data);

Expand All @@ -109,7 +116,7 @@ function it_runs_the_main_process_steps_in_a_database_transaction()
$processor = new TestPipelineProcessor();

/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
$data = Mockery::mock(DataObjectInterface::class);

$processor->process($data);
}
Expand All @@ -126,7 +133,7 @@ function it_does_a_rollback_for_main_process_steps_if_an_exception_is_thrown()
$processor = new TestPipelineProcessor([], null, self::EXCEPTION_IN_MAIN_STEP);

/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
$data = Mockery::mock(DataObjectInterface::class);

try {

Expand Down
31 changes: 16 additions & 15 deletions tests/ProcessStepTest.php
Expand Up @@ -5,6 +5,7 @@
use Czim\Processor\Contracts\ProcessContextInterface;
use Czim\Processor\Test\Helpers\TestProcessStep;
use Exception;
use Mockery;

class ProcessStepTest extends TestCase
{
Expand All @@ -14,9 +15,10 @@ class ProcessStepTest extends TestCase
*/
function it_calls_its_process_function_when_run_by_a_processor()
{
/** @var ProcessContextInterface $context */
$context = $this->getMockBuilder(ProcessContextInterface::class)
->getMock();
/** @var ProcessContextInterface|Mockery\Mock|Mockery\MockInterface $context */
$context = Mockery::mock(ProcessContextInterface::class);
$context->shouldReceive('setSetting');
$context->shouldReceive('getData')->andReturn(Mockery::mock(DataObjectInterface::class));

$step = new TestProcessStep();

Expand All @@ -31,12 +33,13 @@ function it_calls_its_process_function_when_run_by_a_processor()
*/
function it_calls_the_next_step_in_the_pipeline()
{
$this->expectException(\Exception::class);
$this->expectException(Exception::class);
$this->expectExceptionMessage('!next was called!');

/** @var ProcessContextInterface $context */
$context = $this->getMockBuilder(ProcessContextInterface::class)
->getMock();
/** @var ProcessContextInterface|Mockery\Mock|Mockery\MockInterface $context */
$context = Mockery::mock(ProcessContextInterface::class);
$context->shouldReceive('setSetting');
$context->shouldReceive('getData')->andReturn(Mockery::mock(DataObjectInterface::class));

$closure = function () {
// throw custom exception to assert that we got here
Expand All @@ -54,15 +57,13 @@ function it_calls_the_next_step_in_the_pipeline()
*/
function it_stores_context_and_context_data_before_process_is_called()
{
/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)
->getMock();

$context = $this->getMockBuilder(ProcessContextInterface::class)
->getMock();
/** @var DataObjectInterface|Mockery\Mock|Mockery\MockInterface $data */
$data = Mockery::mock(DataObjectInterface::class);

$context->method('getData')
->willReturn($data);
/** @var ProcessContextInterface|Mockery\Mock|Mockery\MockInterface $context */
$context = Mockery::mock(ProcessContextInterface::class);
$context->shouldReceive('setSetting');
$context->shouldReceive('getData')->andReturn($data);

$step = new TestProcessStep();

Expand Down
12 changes: 7 additions & 5 deletions tests/RepositoryContextTraitTest.php
@@ -1,4 +1,6 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */

namespace Czim\Processor\Test;

use Czim\DataObject\Contracts\DataObjectInterface;
Expand All @@ -14,12 +16,12 @@ class RepositoryContextTraitTest extends TestCase
*/
function it_takes_and_retrieves_a_repository_by_name()
{
/** @var DataObjectInterface $data */
/** @var DataObjectInterface|Mockery\Mock|Mockery\MockInterface $data */
$data = Mockery::mock(DataObjectInterface::class);

$context = new TestRepositoryContext($data);

/** @var BaseRepositoryInterface $repository */
/** @var BaseRepositoryInterface|Mockery\Mock|Mockery\MockInterface $repository */
$repository = Mockery::mock(BaseRepositoryInterface::class);

$context->addRepository('test_repo', $repository);
Expand All @@ -35,7 +37,7 @@ function it_throws_an_exception_if_it_cannot_find_a_repository()
$this->expectException(ContextRepositoryException::class);
$this->expectExceptionMessageRegExp('#not found.* unset_repo#');

/** @var DataObjectInterface $data */
/** @var DataObjectInterface|Mockery\Mock|Mockery\MockInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();

$context = new TestRepositoryContext($data);
Expand All @@ -48,7 +50,7 @@ function it_throws_an_exception_if_it_cannot_find_a_repository()
*/
function it_delegates_method_calls_to_a_repository_by_name()
{
/** @var DataObjectInterface $data */
/** @var DataObjectInterface|Mockery\Mock|Mockery\MockInterface $data */
$data = Mockery::mock(DataObjectInterface::class);

$context = new TestRepositoryContext($data);
Expand All @@ -58,7 +60,7 @@ function it_delegates_method_calls_to_a_repository_by_name()
$repository = Mockery::mock(BaseRepositoryInterface::class);
$repository->shouldReceive('testMethod')->once();

/** @var BaseRepositoryInterface $repository */
/** @var BaseRepositoryInterface|Mockery\Mock|Mockery\MockInterface $repository */
$context->addRepository('test_repo', $repository);

$context->repository('test_repo', 'testMethod', [ 'parameter' ]);
Expand Down
25 changes: 14 additions & 11 deletions tests/SimpleProcessContextTest.php
Expand Up @@ -3,6 +3,9 @@

use Czim\DataObject\Contracts\DataObjectInterface;
use Czim\Processor\Contexts\SimpleProcessContext;
use Mockery;
use Mockery\Mock;
use Mockery\MockInterface;

class SimpleProcessContextTest extends TestCase
{
Expand All @@ -12,8 +15,8 @@ class SimpleProcessContextTest extends TestCase
*/
function it_can_be_instantiated_with_settings()
{
/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
/** @var DataObjectInterface|Mock|MockInterface $data */
$data = Mockery::mock(DataObjectInterface::class);

$context = new SimpleProcessContext($data, [ 'test' => 'setting' ]);

Expand All @@ -26,27 +29,27 @@ function it_can_be_instantiated_with_settings()
*/
function it_takes_and_retrieves_data()
{
/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
/** @var DataObjectInterface|Mock|MockInterface $data */
$data = Mockery::mock(DataObjectInterface::class);

$context = new SimpleProcessContext($data);

/** @var DataObjectInterface $newData */
$newData = $this->getMockBuilder(DataObjectInterface::class)->getMock();
/** @var DataObjectInterface|Mock|MockInterface $newData */
$newData = Mockery::mock(DataObjectInterface::class);

// set new data
$context->setData($newData);

static::assertSame($newData, $context->getData(), 'New data was not stored correctly');
}

/**
* @test
*/
function it_sets_and_retrieves_cache_by_key()
{
/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
/** @var DataObjectInterface|Mock|MockInterface $data */
$data = Mockery::mock(DataObjectInterface::class);

$context = new SimpleProcessContext($data);

Expand All @@ -62,8 +65,8 @@ function it_sets_and_retrieves_cache_by_key()
*/
function it_sets_and_retrieves_settings_by_key()
{
/** @var DataObjectInterface $data */
$data = $this->getMockBuilder(DataObjectInterface::class)->getMock();
/** @var DataObjectInterface|Mock|MockInterface $data */
$data = Mockery::mock(DataObjectInterface::class);

$context = new SimpleProcessContext($data);

Expand Down

0 comments on commit 7cd7fe4

Please sign in to comment.