Skip to content

Instantly share code, notes, and snippets.

@Magomogo
Last active December 11, 2015 15:28
Show Gist options
  • Save Magomogo/4620632 to your computer and use it in GitHub Desktop.
Save Magomogo/4620632 to your computer and use it in GitHub Desktop.
Unit test for PutCommand
<?php
namespace STSShop\Admin\Command\Affiliate;
use Symfony\Component\HttpFoundation\Request;
use STSShop\Admin\Affiliate\Properties as AffiliateProperties;
use Mockery as m;
class PutTest extends \PHPUnit_Framework_TestCase
{
public function testUpdatesAffiliateModel()
{
$container = m::mock('Magomogo\\Model\\PropertyContainer\\ContainerInterface');
$container->shouldReceive('saveProperties')
->with(m::on(function($arg){
return $arg->id == 88;
}))
->andReturn(new AffiliateProperties())
->once();
$command = self::putCommand(self::jsonRequest(88), $container);
$command->execute();
}
public function testInvalidRequestCauses400Error()
{
$command = self::putCommand(null, null, m::mock(array('validateValue' => array('errors'))));
$response = $command->execute();
$this->assertEquals(400, $response->getStatusCode());
}
public function testAbsentIdMustSaveNewObject()
{
$container = m::mock('Magomogo\\Model\\PropertyContainer\\ContainerInterface');
$container->shouldReceive('saveProperties')
->andReturn(new AffiliateProperties())
->once();
$command = self::putCommand(self::jsonRequest(null), $container);
$this->assertEquals(201, $command->execute()->getStatusCode());
}
private static function jsonRequest($id = null, $data = array())
{
$json = json_encode($data);
return new Request(array('id' => $id), array(), array(), array(), array(), array(), $json);
}
private static function putCommand($request = null, $container = null, $validator = null)
{
return new Put(
$request ?: self::jsonRequest(),
$container ?: m::mock(
'Magomogo\\Model\\PropertyContainer\\ContainerInterface',
array('saveProperties' => new AffiliateProperties(), 'loadProperties' => new AffiliateProperties())
),
$validator ?: m::mock(array('validateValue' => array()))
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment