chuk-shirley
8/5/2016 - 2:48 PM

Mocking the IBM i Toolkit

Mocking the IBM i Toolkit

<?php
namespace CheckTest;

use Check\Domain\Command\CheckNumber;
use Check\Service\VoidCheckCommand;
use Check\Service\VoidCheckCommandHandler;
use PHPUnit_Framework_TestCase;
use Application\ConfigInterface;
use Application\IbmiToolkitInterface;

/**
 * Class VoidCheckCommandHandlerTest
 * @package CheckTest
 */
class VoidCheckCommandHandlerTest extends PHPUnit_Framework_TestCase
{
    /** @var ConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
    private $config;

    /** @var IbmiToolkitInterface|\PHPUnit_Framework_MockObject_MockObject */
    private $toolkit;

    /** @var VoidCheckCommandHandler|\PHPUnit_Framework_MockObject_MockObject  */
    private $commandHandler;

    public function setUp()
    {
        parent::setUp();

        $this->config = $this->createMock(ConfigInterface::class);
        $this->toolkit = $this->createMock(IbmiToolkitInterface::class);
        $this->commandHandler = new VoidCheckCommandHandler(
            $this->config,
            $this->toolkit
        );
    }

    public function testCallsRpgProgram()
    {
        $expected = ['io_param' => []];
        $this->toolkit->expects(self::once())->method('AddParameterPackedDecimal')->withAnyParameters()
            ->willReturn([]);
        $this->toolkit->expects(self::once())->method('pgmCall')->withAnyParameters()->willReturn($expected);
        $this->config->expects(self::once())->method('get')->with('programCallLibrary')->willReturn('TESTLIB');

        $command = new VoidCheckCommand(
            CheckNumber::fromInteger(12345)
        );
        $actual = $this->commandHandler->handle($command);
        $this->assertEquals($expected, $actual);
    }

    /**
     * @expectedException \Application\InvalidArgumentException
     */
    public function testWrongArgumentThrowsException()
    {
        $command = new \stdClass();
        $commandHandler = new VoidCheckCommandHandler(
            $this->config,
            $this->toolkit
        );
        $commandHandler->handle($command);
    }
}
<?php
namespace Check\Service;

use Application\ConfigInterface;
use Application\IbmiToolkitInterface;
use Application\InvalidArgumentException;
use ServiceBus\Command\CommandHandlerInterface;

/**
 * Class VoidCheckCommandHandler
 * @package Sabel\Checks\Application
 */
class VoidCheckCommandHandler implements CommandHandlerInterface
{
    /** @var ConfigInterface */
    private $config;

    /** @var IbmiToolkitInterface */
    private $ibmiToolkit;

    /**
     * VoidCheckCommandHandler constructor.
     * @param ConfigInterface $config
     * @param IbmiToolkitInterface $ibmiToolkit
     */
    public function __construct(ConfigInterface $config, IbmiToolkitInterface $ibmiToolkit)
    {
        $this->config = $config;
        $this->ibmiToolkit = $ibmiToolkit;
    }

    /**
     * @param VoidCheckCommand $command
     * @return array|bool
     * @throws \Exception
     */
    public function handle($command)
    {
        if (!$command instanceof VoidCheckCommand) {
            throw new InvalidArgumentException('First argument must be an instance of ' .
                VoidCheckCommand::class . '. An instance of ' . gettype($command) . ' was provided instead.');
        }
        $checkNumber = $command->checkNumber;
        $param[] = $this->ibmiToolkit->AddParameterPackedDecimal('both', 7, 0, 'chkno', 'chkno', $checkNumber->getNumber());
        $result = $this->ibmiToolkit->pgmCall("SC5015", $this->config->get('programCallLibrary'), $param, null, null);

        if (!$result) {
            throw new \Exception('An error occurred while attempting to void this check.');
        }

        return $result;
    }
}
<?php
class Toolkit
{
    public function pgmCall($params)
    {
        // do stuff...
    }
    
    public function CLCommand($params)
    {
        // do stuff...
    }
    
    public static function AddParameterPackDec($io, $length ,$scale , $comment, $varName = '', $value='', $dimension=0)
    {
        return new PackedDecParam($io, $length ,$scale , $comment, $varName, $value, $dimension);
    }
}
<?php
namespace Application;

/**
 * Interface IbmiToolkitInterface
 * @package Application
 */
interface IbmiToolkitInterface
{
    /**
     * @param string $pgmName
     * @param string $lib
     * @param string null $inputParam
     * @param string null $returnParam
     * @param array null $options
     * @return array|bool
     */
    public function pgmCall($pgmName, $lib, $inputParam = null, $returnParam = null, $options = null);

    /**
     * CLCommand
     *
     * @param array $command
     * @param string $exec
     * @return array|bool
     */
    public function CLCommand($command, $exec = '');

    /**
     * @param $io
     * @param $length
     * @param $scale
     * @param $comment
     * @param string $varName
     * @param string $value
     * @param int $dimension
     * @return mixed
     */
    public function AddParameterPackedDecimal($io, $length ,$scale , $comment, $varName = '', $value='', $dimension=0);
}
<?php
namespace Application;

use ToolkitApi\Toolkit;

/**
 * Class IbmiToolkitAdapter
 * @package Application
 */
class IbmiToolkitAdapter extends Toolkit implements IbmiToolkitInterface
{
    /**
     * @param string $pgmName
     * @param string $lib
     * @param null $inputParam
     * @param null $returnParam
     * @param null $options
     * @return array|bool
     */
    public function pgmCall($pgmName, $lib, $inputParam = null, $returnParam = null, $options = null)
    {
        return static::pgmCall($pgmName, $lib, $inputParam = null, $returnParam = null, $options = null);
    }

    /**
     * @param array $command
     * @param string $exec
     * @return array|bool
     */
    public function CLCommand($command, $exec = '')
    {
        return static::CLCommand($command, $exec);
    }

    /**
     * @param $io
     * @param $length
     * @param $scale
     * @param $comment
     * @param string $varName
     * @param string $value
     * @param int $dimension
     * @return \ToolkitApi\PackedDecParam
     */
    public function AddParameterPackedDecimal($io, $length ,$scale , $comment, $varName = '', $value='', $dimension=0)
    {
        return static::AddParameterPackDec($io, $length ,$scale , $comment, $varName = '', $value='', $dimension=0);
    }
}