public function testdashboardActionAjaxLayout()
 {
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $pool = new Pool($container, 'title', 'logo.png');
     $pool->setTemplates(array('ajax' => 'ajax.html'));
     $templating = $this->getMock('Symfony\\Bundle\\FrameworkBundle\\Templating\\EngineInterface');
     $request = new Request();
     $request->headers->set('X-Requested-With', 'XMLHttpRequest');
     $requestStack = null;
     if (Kernel::MINOR_VERSION > 3) {
         $requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
         $requestStack->push($request);
     }
     $values = array('sonata.admin.pool' => $pool, 'templating' => $templating, 'request' => $request, 'request_stack' => $requestStack);
     $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use($values) {
         return $values[$id];
     }));
     $container->expects($this->any())->method('getParameter')->will($this->returnCallback(function ($name) {
         if ($name == 'sonata.admin.configuration.dashboard_blocks') {
             return array();
         }
     }));
     $controller = new CoreController();
     $controller->setContainer($container);
     $response = $controller->dashboardAction();
     $this->isInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     $this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $this->request = new Request();
     $this->pool = new Pool($this->container, 'title', 'logo.png');
     $this->pool->setAdminServiceIds(array('foo.admin'));
     $this->request->attributes->set('_sonata_admin', 'foo.admin');
     $this->admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $this->parameters = array();
     $this->template = '';
     // php 5.3 BC
     $params =& $this->parameters;
     $template =& $this->template;
     $templating = $this->getMock('Symfony\\Bundle\\FrameworkBundle\\Templating\\DelegatingEngine', array(), array($this->container, array()));
     $templating->expects($this->any())->method('renderResponse')->will($this->returnCallback(function ($view, array $parameters = array(), Response $response = null) use(&$params, &$template) {
         $template = $view;
         if (null === $response) {
             $response = new Response();
         }
         $params = $parameters;
         return $response;
     }));
     $this->session = new Session(new MockArraySessionStorage());
     // php 5.3 BC
     $pool = $this->pool;
     $request = $this->request;
     $admin = $this->admin;
     $session = $this->session;
     $twig = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
     $twigRenderer = $this->getMock('Symfony\\Bridge\\Twig\\Form\\TwigRendererInterface');
     $formExtension = new FormExtension($twigRenderer);
     $twig->expects($this->any())->method('getExtension')->will($this->returnCallback(function ($name) use($formExtension) {
         switch ($name) {
             case 'form':
                 return $formExtension;
         }
         return;
     }));
     $exporter = $this->getMock('Sonata\\AdminBundle\\Export\\Exporter');
     $exporter->expects($this->any())->method('getResponse')->will($this->returnValue(new StreamedResponse()));
     $this->auditManager = $this->getMockBuilder('Sonata\\AdminBundle\\Model\\AuditManager')->disableOriginalConstructor()->getMock();
     $this->adminObjectAclManipulator = $this->getMockBuilder('Sonata\\AdminBundle\\Util\\AdminObjectAclManipulator')->disableOriginalConstructor()->getMock();
     // php 5.3 BC
     $auditManager = $this->auditManager;
     $adminObjectAclManipulator = $this->adminObjectAclManipulator;
     $this->csrfProvider = $this->getMockBuilder('Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface')->getMock();
     $this->csrfProvider->expects($this->any())->method('generateCsrfToken')->will($this->returnCallback(function ($intention) {
         return 'csrf-token-123_' . $intention;
     }));
     $this->csrfProvider->expects($this->any())->method('isCsrfTokenValid')->will($this->returnCallback(function ($intention, $token) {
         if ($token == 'csrf-token-123_' . $intention) {
             return true;
         }
         return false;
     }));
     // php 5.3 BC
     $csrfProvider = $this->csrfProvider;
     $this->logger = $this->getMock('Psr\\Log\\LoggerInterface');
     $logger = $this->logger;
     // php 5.3 BC
     $requestStack = null;
     if (Kernel::MINOR_VERSION > 3) {
         $requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
         $requestStack->push($request);
     }
     $this->container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use($pool, $request, $admin, $templating, $twig, $session, $exporter, $auditManager, $adminObjectAclManipulator, $requestStack, $csrfProvider, $logger) {
         switch ($id) {
             case 'sonata.admin.pool':
                 return $pool;
             case 'request_stack':
                 return $requestStack;
             case 'request':
                 return $request;
             case 'foo.admin':
                 return $admin;
             case 'templating':
                 return $templating;
             case 'twig':
                 return $twig;
             case 'session':
                 return $session;
             case 'sonata.admin.exporter':
                 return $exporter;
             case 'sonata.admin.audit.manager':
                 return $auditManager;
             case 'sonata.admin.object.manipulator.acl.admin':
                 return $adminObjectAclManipulator;
             case 'form.csrf_provider':
                 return $csrfProvider;
             case 'logger':
                 return $logger;
         }
         return;
     }));
     // php 5.3
     $tthis = $this;
     $this->container->expects($this->any())->method('has')->will($this->returnCallback(function ($id) use($tthis) {
         if ($id == 'form.csrf_provider' && $tthis->getCsrfProvider() !== null) {
             return true;
         }
         if ($id == 'logger') {
             return true;
         }
         return false;
     }));
     $this->admin->expects($this->any())->method('getTemplate')->will($this->returnCallback(function ($name) {
         switch ($name) {
             case 'ajax':
                 return 'SonataAdminBundle::ajax_layout.html.twig';
             case 'layout':
                 return 'SonataAdminBundle::standard_layout.html.twig';
             case 'show':
                 return 'SonataAdminBundle:CRUD:show.html.twig';
             case 'show_compare':
                 return 'SonataAdminBundle:CRUD:show_compare.html.twig';
             case 'edit':
                 return 'SonataAdminBundle:CRUD:edit.html.twig';
             case 'dashboard':
                 return 'SonataAdminBundle:Core:dashboard.html.twig';
             case 'search':
                 return 'SonataAdminBundle:Core:search.html.twig';
             case 'list':
                 return 'SonataAdminBundle:CRUD:list.html.twig';
             case 'preview':
                 return 'SonataAdminBundle:CRUD:preview.html.twig';
             case 'history':
                 return 'SonataAdminBundle:CRUD:history.html.twig';
             case 'acl':
                 return 'SonataAdminBundle:CRUD:acl.html.twig';
             case 'delete':
                 return 'SonataAdminBundle:CRUD:delete.html.twig';
             case 'batch':
                 return 'SonataAdminBundle:CRUD:list__batch.html.twig';
             case 'batch_confirmation':
                 return 'SonataAdminBundle:CRUD:batch_confirmation.html.twig';
         }
         return;
     }));
     $this->admin->expects($this->any())->method('getIdParameter')->will($this->returnValue('id'));
     $this->admin->expects($this->any())->method('generateUrl')->will($this->returnCallback(function ($name, array $parameters = array(), $absolute = false) {
         $result = $name;
         if (!empty($parameters)) {
             $result .= '?' . http_build_query($parameters);
         }
         return $result;
     }));
     $this->admin->expects($this->any())->method('generateObjectUrl')->will($this->returnCallback(function ($name, $object, array $parameters = array(), $absolute = false) {
         $result = get_class($object) . '_' . $name;
         if (!empty($parameters)) {
             $result .= '?' . http_build_query($parameters);
         }
         return $result;
     }));
     $this->controller = new CRUDController();
     $this->controller->setContainer($this->container);
     // Make some methods public to test them
     $testedMethods = array('renderJson', 'isXmlHttpRequest', 'configure', 'getBaseTemplate', 'redirectTo', 'addFlash');
     foreach ($testedMethods as $testedMethod) {
         $method = new \ReflectionMethod('Sonata\\AdminBundle\\Controller\\CRUDController', $testedMethod);
         $method->setAccessible(true);
         $this->protectedTestedMethods[$testedMethod] = $method;
     }
 }
Esempio n. 3
0
require_once __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing;
use Symfony\Component\HttpKernel;
use Symfony\Component\EventDispatcher\EventDispatcher;
function render_template(Request $request)
{
    extract($request->attributes->all(), EXTR_SKIP);
    ob_start();
    include sprintf(__DIR__ . '/../src/pages/%s.php', $_route);
    return new Response(ob_get_clean());
}
$request = Request::createFromGlobals();
$requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
$requestStack->push($request);
$routes = (include __DIR__ . '/../src/routing.php');
$context = new Routing\RequestContext();
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);
$resolver = new HttpKernel\Controller\ControllerResolver();
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new HttpKernel\EventListener\RouterListener($matcher, $requestStack));
$errorHandler = function (\Symfony\Component\Debug\Exception\FlattenException $exception) {
    $msg = 'Something went wrong! (' . $exception->getMessage() . ')';
    return new Response($msg, $exception->getStatusCode());
};
$listener = new HttpKernel\EventListener\ExceptionListener('Itav\\Invoice\\Controller\\ErrorController::exceptionAction');
$dispatcher->addSubscriber($listener);
$dispatcher->addSubscriber(new HttpKernel\EventListener\ResponseListener('UTF-8'));
$dispatcher->addSubscriber(new Itav\StringResponseListener());
 /**
  * Prepares the environment before running a test.
  */
 protected function setUp()
 {
     parent::setUp();
     $this->categoryAdminController = new CategoryAdminController();
     $this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $this->categoryManager = $this->getMockBuilder('Sonata\\ClassificationBundle\\Entity\\CategoryManager')->disableOriginalConstructor()->getMock();
     $categoryManager = $this->categoryManager;
     $this->contextManager = $this->getMockBuilder('Sonata\\ClassificationBundle\\Entity\\ContextManager')->disableOriginalConstructor()->getMock();
     $contextManager = $this->contextManager;
     $this->view = $this->getMock('Symfony\\Component\\Form\\FormView');
     $this->form = $this->getMockBuilder('Symfony\\Component\\Form\\Form')->disableOriginalConstructor()->getMock();
     $this->form->expects($this->any())->method('createView')->will($this->returnValue($this->view));
     $this->dataGrid = $this->getMock('\\Sonata\\AdminBundle\\Datagrid\\DatagridInterface');
     $this->dataGrid->expects($this->any())->method('getForm')->will($this->returnValue($this->form));
     $this->request = new Request();
     $this->pool = new Pool($this->container, 'title', 'logo.png');
     $this->pool->setAdminServiceIds(array('foo.admin'));
     $this->request->attributes->set('_sonata_admin', 'foo.admin');
     $this->admin = $this->getMockBuilder('Sonata\\ClassificationBundle\\Admin\\CategoryAdmin')->disableOriginalConstructor()->getMock();
     $this->admin->expects($this->any())->method('getPersistentParameter')->will($this->returnValue('persistentParameter'));
     $this->admin->expects($this->any())->method('getDataGrid')->will($this->returnValue($this->dataGrid));
     $params = array();
     $template = '';
     $templating = $this->getMock('Symfony\\Bundle\\FrameworkBundle\\Templating\\DelegatingEngine', array(), array($this->container, array()));
     $templating->expects($this->any())->method('renderResponse')->will($this->returnCallback(function ($view, array $parameters = array(), Response $response = null) use(&$params, &$template) {
         $template = $view;
         if (null === $response) {
             $response = new Response();
         }
         $params = $parameters;
         return $response;
     }));
     $twig = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
     $twigRenderer = $this->getMock('Symfony\\Bridge\\Twig\\Form\\TwigRendererInterface');
     $formExtension = new FormExtension($twigRenderer);
     $twig->expects($this->any())->method('getExtension')->will($this->returnCallback(function ($name) use($formExtension) {
         switch ($name) {
             case 'form':
                 return $formExtension;
         }
         return;
     }));
     $this->csrfProvider = $this->getMock('Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface');
     $this->csrfProvider->expects($this->any())->method('generateCsrfToken')->will($this->returnCallback(function ($intention) {
         return 'csrf-token-123_' . $intention;
     }));
     $this->csrfProvider->expects($this->any())->method('isCsrfTokenValid')->will($this->returnCallback(function ($intention, $token) {
         if ($token == 'csrf-token-123_' . $intention) {
             return true;
         }
         return false;
     }));
     // php 5.3 BC
     $pool = $this->pool;
     $admin = $this->admin;
     $request = $this->request;
     $csrfProvider = $this->csrfProvider;
     $requestStack = null;
     if (Kernel::MINOR_VERSION > 3) {
         $requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
         $requestStack->push($request);
     }
     $this->container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use($pool, $admin, $request, $requestStack, $templating, $twig, $csrfProvider, $categoryManager, $contextManager) {
         switch ($id) {
             case 'sonata.admin.pool':
                 return $pool;
             case 'foo.admin':
                 return $admin;
             case 'request':
                 return $request;
             case 'request_stack':
                 return $requestStack;
             case 'templating':
                 return $templating;
             case 'twig':
                 return $twig;
             case 'form.csrf_provider':
                 return $csrfProvider;
             case 'sonata.classification.manager.category':
                 return $categoryManager;
             case 'sonata.classification.manager.context':
                 return $contextManager;
         }
         return;
     }));
     $this->categoryAdminController->setContainer($this->container);
 }
Esempio n. 5
0
 */
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\Helper\SlotsHelper;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper;
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app->register(new Silex\Provider\DoctrineServiceProvider(), array('db.options' => $app['config']['db']));
$app->register(new Silex\Provider\SessionServiceProvider());
/* Представление */
$app['templating.engine.php'] = $app->share(function () use($app) {
    $phpEngine = new PhpEngine(new TemplateNameParser(), new FilesystemLoader(__DIR__ . '/../templates/%name%'));
    /* Хелперы представления */
    $phpEngine->set(new SlotsHelper());
    $inlineFragmentRenderer = new \Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer($app);
    $requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
    $requestStack->push($app['request']);
    $fragmentHandler = new \Symfony\Component\HttpKernel\Fragment\FragmentHandler(array($inlineFragmentRenderer), $app['debug'], $requestStack);
    $phpEngine->set(new ActionsHelper($fragmentHandler));
    return $phpEngine;
});
/* Модели */
$app['models.products'] = $app->share(function () use($app) {
    return new Gksoft\Models\Catalog($app['db']);
});
$app['models.users'] = $app->share(function () use($app) {
    return new Gksoft\Models\Users($app['db']);
});
/* Контроллеры */
$app['controllers.index'] = $app->share(function () use($app) {
    return new Gksoft\Controllers\IndexController($app['templating.engine.php'], $app['models.products'], $app, $app['request']);