Parcourir la source

Update test structure for Symfony 2.8 and 3.0

Michael Babker il y a 9 ans
Parent
commit
6bca17d31f

+ 11 - 3
Tests/Command/ExplainAdminCommandTest.php

@@ -121,10 +121,18 @@ class ExplainAdminCommandTest extends \PHPUnit_Framework_TestCase
                 return $adminParent;
             }));
 
-        $this->validatorFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
+        // Prefer Symfony 2.x interfaces
+        if (interface_exists('Symfony\Component\Validator\MetadataFactoryInterface')) {
+            $this->validatorFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
 
-        $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
-        $validator->expects($this->any())->method('getMetadataFactory')->will($this->returnValue($this->validatorFactory));
+            $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
+            $validator->expects($this->any())->method('getMetadataFactory')->will($this->returnValue($this->validatorFactory));
+        } else {
+            $this->validatorFactory = $this->getMock('Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface');
+
+            $validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
+            $validator->expects($this->any())->method('getMetadataFor')->will($this->returnValue($this->validatorFactory));
+        }
 
         // php 5.3 BC
         $admin = $this->admin;

+ 53 - 19
Tests/Controller/CRUDControllerTest.php

@@ -32,6 +32,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Component\HttpKernel\Kernel;
 use Symfony\Component\HttpKernel\KernelInterface;
 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
+use Symfony\Component\Security\Csrf\CsrfToken;
 
 /**
  * Test for CRUDController.
@@ -197,26 +198,50 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $auditManager = $this->auditManager;
         $adminObjectAclManipulator = $this->adminObjectAclManipulator;
 
-        $this->csrfProvider = $this->getMockBuilder(
-            'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'
-        )
-            ->getMock();
+        // Prefer Symfony 2.x interfaces
+        if (interface_exists('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface')) {
+            $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('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;
+                    }
 
-        $this->csrfProvider->expects($this->any())
-            ->method('isCsrfTokenValid')
-            ->will($this->returnCallback(function ($intention, $token) {
-                if ($token == 'csrf-token-123_'.$intention) {
-                    return true;
-                }
+                    return false;
+                }));
+        } else {
+            $this->csrfProvider = $this->getMockBuilder(
+                'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'
+            )
+                ->getMock();
+
+            $this->csrfProvider->expects($this->any())
+                ->method('getToken')
+                ->will($this->returnCallback(function ($intention) {
+                    return new CsrfToken($intention, 'csrf-token-123_'.$intention);
+                }));
 
-                return false;
-            }));
+            $this->csrfProvider->expects($this->any())
+                ->method('isTokenValid')
+                ->will($this->returnCallback(function ($intention, $token) {
+                    if ((string) $token == 'csrf-token-123_'.$intention) {
+                        return true;
+                    }
+
+                    return false;
+                }));
+        }
 
         // php 5.3 BC
         $csrfProvider = $this->csrfProvider;
@@ -225,7 +250,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $logger       = $this->logger; // php 5.3 BC
 
         $requestStack = null;
-        if (Kernel::MINOR_VERSION > 3) {
+        if ((Kernel::MAJOR_VERSION == 2 && Kernel::MINOR_VERSION > 3) || Kernel::MAJOR_VERSION >= 3) {
             $requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
             $requestStack->push($request);
         }
@@ -272,6 +297,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
                     case 'sonata.admin.object.manipulator.acl.admin':
                         return $adminObjectAclManipulator;
                     case 'form.csrf_provider':
+                    case 'security.csrf.token_manager':
                         return $csrfProvider;
                     case 'logger':
                         return $logger;
@@ -288,7 +314,11 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $this->container->expects($this->any())
             ->method('has')
             ->will($this->returnCallback(function ($id) use ($tthis) {
-                if ($id == 'form.csrf_provider' && $tthis->getCsrfProvider() !== null) {
+                if ($id == 'form.csrf_provider' && Kernel::MAJOR_VERSION == 2 && $tthis->getCsrfProvider() !== null) {
+                    return true;
+                }
+
+                if ($id == 'security.csrf.token_manager' && Kernel::MAJOR_VERSION >= 3) {
                     return true;
                 }
 
@@ -300,6 +330,10 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
                     return true;
                 }
 
+                if ($id == 'templating') {
+                    return true;
+                }
+
                 return false;
             }));
 

+ 14 - 0
Tests/Controller/CoreControllerTest.php

@@ -51,6 +51,13 @@ class CoreControllerTest extends \PHPUnit_Framework_TestCase
                 return array();
             }
         }));
+        $container->expects($this->any())->method('has')->will($this->returnCallback(function ($id) {
+            if ($id == 'templating') {
+                return true;
+            }
+
+            return false;
+        }));
 
         $controller = new CoreController();
         $controller->setContainer($container);
@@ -94,6 +101,13 @@ class CoreControllerTest extends \PHPUnit_Framework_TestCase
                 return array();
             }
         }));
+        $container->expects($this->any())->method('has')->will($this->returnCallback(function ($id) {
+            if ($id == 'templating') {
+                return true;
+            }
+
+            return false;
+        }));
 
         $controller = new CoreController();
         $controller->setContainer($container);