GithubHelp home page GithubHelp logo

Comments (4)

nXqd avatar nXqd commented on July 3, 2024

@scaraveos can you provide me a good template to write test for restful service in zf2. Thanks a lot :)

from zf2-restful-module-skeleton.

scaraveos avatar scaraveos commented on July 3, 2024

@nXqd I suggest that you code your application logic in library package/classes. In my opinion the controller should be doing something very simple as instantiating a library object, feeding it with data, calling a method to fulfill a specific request and finally returning the method result.

If you do that, you do not need to test the controller but the library which is much more straight forward and you keep your code tidy.

If you still want to test your controller you need to instantiate the controller and call the right action. Ie:

$event = new \Zend\Mvc\MvcEvent();
$event->setRouteMatch(new \Zend\Mvc\Router\RouteMatch(array()));

$controller = new \Main\Controller\TestController();
$controller->setEvent($event);

$routeMatch = $controller->getEvent()->getRouteMatch();
$routeMatch->setMatchedRouteName('restful');

$result = $controller->getList();

..... assert $result data ....

from zf2-restful-module-skeleton.

nXqd avatar nXqd commented on July 3, 2024

I use angularJS as front end and zf2 as restful webservice only. So I think it's still best to test restful resource. Since I'm quite new to zf2 so I'm open for any suggestions.

In your example, it seems to take care the result but I want to test the request and response status as well. I come up with a small unit test but it doesn't seem to work. Can you help me :
http://stackoverflow.com/questions/14210463/restful-controller-in-zendframework-2-how-does-its-parameter-work

from zf2-restful-module-skeleton.

scaraveos avatar scaraveos commented on July 3, 2024

I gave a brief explanation (at the above link) of how ZF2 internally calls the controller methods. I am closing this issue as it is not related with this module but rather it's a general ZF2 question.

from zf2-restful-module-skeleton.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.