Jeśli jesteś właścicielem tej strony, możesz wyłączyć reklamę poniżej zmieniając pakiet na PRO lub VIP w panelu naszego hostingu już od 4zł!

Archive for Wrzesień, 2012

Access entity manager in Zend Framework 2 unit tests

Access entity manager in Zend Framework 2 unit tests

In order to access the entity manager in your unit tests you will have to make your PHPUnit bootstrap file a little bit more complex. You will need to access the entity manager if you are using Doctrine module together with Zend Framework 2.

Here is what I have done:

  1. <?php
  2.  
  3. use Zend\ServiceManager\ServiceManager,
  4.     Zend\Mvc\Service\ServiceManagerConfig;
  5.  
  6. class Bootstrap
  7. {
  8.     static public $config;
  9.     static public $sm;
  10.     static public $em;
  11.  
  12.     static public function go()
  13.     {
  14.         chdir(dirname(__DIR__));
  15.         include __DIR__ . '/../init_autoloader.php';
  16.         self::$config = include 'config/application.config.php';
  17.         Zend\Mvc\Application::init(self::$config);
  18.         self::$sm = self::getServiceManager(self::$config);
  19.         self::$em = self::getEntityManager(self::$sm);
  20.     }
  21.  
  22.     static public function getServiceManager($config)
  23.     {
  24.         $serviceManager = new ServiceManager(new ServiceManagerConfig);
  25.         $serviceManager->setService('ApplicationConfig', $config);
  26.         $serviceManager->get('ModuleManager')->loadModules();
  27.         return $serviceManager;
  28.     }
  29.  
  30.     static public function getEntityManager($serviceManager)
  31.     {
  32.         return $serviceManager->get('doctrine.entitymanager.orm_default');
  33.     }
  34. }
  35.  
  36. Bootstrap::go();

And then in your unit tests you can access the entity manager simply by:

  1. Bootstrap:$em

That was quite easy actually.

Source: http://blog.richardknop.com/2012/09/access-entity-manager-in-zend-framework-2-unit-tests/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

How to unit test redirecting in Zend Framework 2

How to unit test redirecting in Zend Framework 2

I’ve been working with Zend Framework 2 last couple of weeks and so far I like it better than the legacy Zend Framework. Proper namespaces and separate bootstrap for each module are nice features. I am still learning all the new features so I will be documenting some helpful ideas.

One problem I had today was I needed to write a unit test for a controller action which under certain condition redirects to a different action using the redirect helper:

  1. $this->redirect->roToute('routeName', array('controller' => 'index', 'action' = 'index'));

Unit testing a controller action was not a problem, I figured that out a week or two ago. In order to write an assertion for redirection, you will need to create a new instance of Zend\Mvc\Router\SimpleRouteStack and add routes you are testing for to it. Then attach the router to Zend\Mvc\MvcEvent.

So include few classes from Zend library plus the controller you want to test:

  1. use MyModule\Controller\IndexController,
  2.     Zend\Http\Request,
  3.     Zend\Http\Response,
  4.     Zend\Mvc\MvcEvent,
  5.     Zend\Mvc\Router\RouteMatch,
  6.     Zend\Mvc\Router\SimpleRouteStack,
  7.     Zend\Mvc\Router\Http\Segment,

Then put this inside your PHPUnit test case’s setUp method:

  1. public function setUp()
  2. {
  3.     $this->_controller = new IndexController;
  4.     $this->_request = new Request;
  5.     $this->_response = new Response;
  6.  
  7.     $this->_event = new MvcEvent();
  8.  
  9.     $routeStack = new SimpleRouteStack;
  10.     $route = new Segment('/mymodule/[:controller/[:action/]]');
  11.     $routeStack->addRoute('mymodule', $route);
  12.     $this->_event->setRouter($routeStack);
  13.  
  14.     $routeMatch = new RouteMatch(array('controller' => 'index', 'action' => 'index'));
  15.     $routeMatch->setMatchedRouteName('admin');
  16.     $this->_event->setRouteMatch($routeMatch);
  17.  
  18.     $this->_controller->setEvent($this->_event);
  19. }

After you have done all that, there are two ways how to assert a redirection has actually happened.

First, you can test for a 302 HTTP status:

  1. public function testIndexActionRedirectsByHttpStatus()
  2. {
  3.     $this->_controller->dispatch($this->_request, $this->_response);
  4.     $this->assertEquals(302, $this->_response->getStatusCode());
  5. }

Second, you can check for the Location header:

  1. public function testIndexActionRedirectsToCorrectUri()
  2. {
  3.     $this->_controller->dispatch($this->_request, $this->_response);
  4.     $this->assertEquals('/mymodule/mycontroller/myaction/', $this->_event->getResponse()->getHeaders()->get('Location')->getUri());
  5. }

Ideally use both assertions to be sure.

Source: http://blog.richardknop.com/2012/09/how-to-unit-test-redirecting-in-zend-framework-2/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Social media buttons the easy way

Social media buttons the easy wayIt’s great to have access to such a great repository of WordPress plugins, but sometimes a great plugin is just “too much” for a smaller task of function. Like social media buttons, they have in fact a very simple function and most buttons require only a few rows of code. If a complex plugin doesn’t [...]


Source: http://feedproxy.google.com/~r/WebDevelopmentBlog/~3/J0TBF8uJmZc/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Social media buttons the easy way

Social media buttons the easy wayIt’s great to have access to such a great repository of WordPress plugins, but sometimes a great plugin is just “too much” for a smaller task of function. Like social media buttons, they have in fact a very simple function and most buttons require only a few rows of code. If a complex plugin doesn’t [...]


Source: http://feedproxy.google.com/~r/WebDevelopmentBlog/~3/J0TBF8uJmZc/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Social media buttons the easy way

Social media buttons the easy wayIt’s great to have access to such a great repository of WordPress plugins, but sometimes a great plugin is just “too much” for a smaller task of function. Like social media buttons, they have in fact a very simple function and most buttons require only a few rows of code. If a complex plugin doesn’t [...]


Source: http://feedproxy.google.com/~r/WebDevelopmentBlog/~3/J0TBF8uJmZc/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Social media buttons the easy way

Social media buttons the easy wayIt’s great to have access to such a great repository of WordPress plugins, but sometimes a great plugin is just “too much” for a smaller task of function. Like social media buttons, they have in fact a very simple function and most buttons require only a few rows of code. If a complex plugin doesn’t [...]


Source: http://feedproxy.google.com/~r/WebDevelopmentBlog/~3/J0TBF8uJmZc/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Building a Custom WordPress Website from Scratch

Building a Custom WordPress Website from ScratchWe all know WordPress has grown to a mature content management system these days. There are several great WordPress themes you can use to build a professional corporate website. You are not limited to creating a magazine or blog site if you choose WordPress as your CMS. A few days ago I launched my company’s website at finalwebsites.nl [...]


Source: http://feedproxy.google.com/~r/WebDevelopmentBlog/~3/8y1xg66Ui2g/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Building a Custom WordPress Website from Scratch

Building a Custom WordPress Website from ScratchWe all know WordPress has grown to a mature content management system these days. There are several great WordPress themes you can use to build a professional corporate website. You are not limited to creating a magazine or blog site if you choose WordPress as your CMS. A few days ago I launched my company’s website at finalwebsites.nl [...]


Source: http://feedproxy.google.com/~r/WebDevelopmentBlog/~3/8y1xg66Ui2g/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Reddit.com: Avoid static methods at all costs? (testability)

Reddit.com: Avoid static methods at all costs? (testability)

On Reddit.com there’s a recent post questioning the (recently) common saying that PHP developers should avoid static methods when concerned about testability:

I get it: testing is important, and building your codebase in a manner that is easy to test should be a priority. However, sometimes I feel like I have to compromise on the elegance of my code in order to maintain testability. Cases where perhaps a static method makes sense, but end up having to perform some coding acrobatics in order to avoid it. Is this a common challenge, something many developers face and must balance between? Or am I misguided in how frequently static methods can be the most elegant solution (before taking testability into consideration)?

Answers point out a few things – that sometimes, state doesn’t matter and static is okay or that they can be used if the instance they return is always exactly the same, never altered.

Source: http://www.phpdeveloper.org/news/18523

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Reddit.com: Avoid static methods at all costs? (testability)

Reddit.com: Avoid static methods at all costs? (testability)

On Reddit.com there’s a recent post questioning the (recently) common saying that PHP developers should avoid static methods when concerned about testability:

I get it: testing is important, and building your codebase in a manner that is easy to test should be a priority. However, sometimes I feel like I have to compromise on the elegance of my code in order to maintain testability. Cases where perhaps a static method makes sense, but end up having to perform some coding acrobatics in order to avoid it. Is this a common challenge, something many developers face and must balance between? Or am I misguided in how frequently static methods can be the most elegant solution (before taking testability into consideration)?

Answers point out a few things – that sometimes, state doesn’t matter and static is okay or that they can be used if the instance they return is always exactly the same, never altered.

Source: http://www.phpdeveloper.org/news/18523

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>