Instantly share code, notes, and snippets.

Embed
What would you like to do?
/vendor/*
composer.lock
# ignore certain files
*.pyc
*.swp
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
*.7z
# ignore compiled files
*.com
*.class
*.dll
*.exe
*.app
*.o
*.so
# system files
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
# ignore Sass cache
.sass-cache
{
"name": "chrisguitarguy/int-tests-with-mocks",
"description": "http://christopherdavis.me/blog/integration-testing-with-mocks.html",
"license": "MIT",
"require": {
"symfony/event-dispatcher": "~2.6",
"symfony/http-kernel": "~2.6",
"symfony/http-foundation": "~2.6",
"phpunit/phpunit": "~4.5"
},
"autoload": {
"files": [
"Post.php",
"PostRepository.php",
"CurrentPostListener.php"
]
}
}
<?php
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
final class CurrentPostListener implements EventSubscriberInterface
{
const ID_KEY = 'post_id';
const OBJECT_KEY = 'post';
/**
* @var PostRepository
*/
private $posts;
public function __construct(PostRepository $posts)
{
$this->posts = $posts;
}
public static function getSubscribedEvents()
{
// we want to fire this AFTER routig in Symfony, which is priority 32
return [
KernelEvents::REQUEST => ['onRequest', 10],
];
}
public function onRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($request->attributes->has(self::OBJECT_KEY) || !$request->attributes->get(self::ID_KEY)) {
return;
}
$id = $request->attributes->get(self::ID_KEY);
$post = $this->posts->get($id);
if (!$post) {
throw new NotFoundHttpException(sprintf('Post %s Not Found', $id));
}
$request->attributes->set(self::OBJECT_KEY, $post);
}
}
<?php
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class CurrentPostListenerTest extends \PHPUnit_Framework_TestCase
{
const POSTID = 123;
private $posts, $listener, $dispatcher, $request;
public function testRequestAlreadySetObjectKeyDoesNotFetchPostFromDatabase()
{
$this->willNotFetchPost();
$post = $this->createPost();
$this->request->attributes->set(CurrentPostListener::OBJECT_KEY, $post);
$this->dispatcher->dispatch(KernelEvents::REQUEST, $this->createEvent());
}
public function testNoPostIdKeyInRequestDoesNotFetchPostFromDatabase()
{
$this->willNotFetchPost();
$this->assertFalse(
$this->request->attributes->has(CurrentPostListener::ID_KEY),
'request should not have ID key'
);
$this->dispatcher->dispatch(KernelEvents::REQUEST, $this->createEvent());
}
public function testListenerThrowsNotFoundWhenNoPostIsFoundForAGivenId()
{
$this->setExpectedException(NotFoundHttpException::class);
$this->posts->expects($this->once())
->method('get')
->with(self::POSTID)
->willReturn(null);
$this->request->attributes->set(CurrentPostListener::ID_KEY, self::POSTID);
$this->dispatcher->dispatch(KernelEvents::REQUEST, $this->createEvent());
}
public function testListenerFetchesPostAndPutsPostInRequestWhenFound()
{
$post = $this->createPost();
$this->posts->expects($this->once())
->method('get')
->with(self::POSTID)
->willReturn($post);
$this->request->attributes->set(CurrentPostListener::ID_KEY, self::POSTID);
$this->dispatcher->dispatch(KernelEvents::REQUEST, $this->createEvent());
$this->assertSame($post, $this->request->attributes->get(CurrentPostListener::OBJECT_KEY));
}
protected function setUp()
{
$this->posts = $this->getMock(PostRepository::class);
$this->listener = new CurrentPostListener($this->posts);
$this->dispatcher = new EventDispatcher();
$this->dispatcher->addSubscriber($this->listener);
$this->request = Request::create('/');
}
private function willNotFetchPost()
{
$this->posts->expects($this->never())
->method('get');
}
private function createPost()
{
return new Post(self::POSTID, 'test', 'test');
}
private function createEvent()
{
return new GetResponseEvent(
$this->getMock(HttpKernelInterface::class),
$this->request,
HttpKernelInterface::MASTER_REQUEST
);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="main">
<file>CurrentPostListenerTest.php</file>
</testsuite>
</testsuites>
</phpunit>
<?php
final class Post
{
private $id;
private $title;
private $content;
public function __construct($id, $title, $content)
{
$this->id = $id;
$this->title = $title;
$this->content = $content;
}
public function getId()
{
return $this->slug;
}
public function getTitle()
{
return $this->title;
}
public function getContent()
{
return $this->content;
}
}
<?php
interface PostRepository
{
/**
* Get a single post by its ID
*
* @param int $id
* @return Post|null The post, if found, null otherwise.
*/
public function get($id);
// Probably some other methods here, like `all`, etc.
// we don't need to worry about them for this tutorial
}
@yogendra9891

This comment has been minimized.

Copy link

yogendra9891 commented Sep 10, 2015

$user_id = getting from request
$app_id = getting from request
$em = $this->_getEntityManager(); //entity manager object
$app_data = $em->getRepository('SixthContinentConnectBundle:Application')
->getAppInformation($app_id);
if (!$app_data) {
$resp_data = new Resp(Msg::getMessage(1118)->getCode(), Msg::getMessage(1118)->getMessage(), $result_data); //INVALID_APP
return Utility::createJSONResponse($resp_data);
}
$result_data = array(
'id'=>$app_data->getId(),
'app_id'=>$app_data->getApplicationId(),
'app_name'=>$app_data->getApplicationName(),
'user_id'=>$app_data->getUserId(),
'app_url'=>$app_data->getApplicationUrl()
);
$resp_data = new Resp(Msg::getMessage(101)->getCode(), Msg::getMessage(101)->getMessage(), $result_data);//SUCCESS
return Utility::createJSONResponse($resp_data);
This is my code in controller and can I mock this. please help me.

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment