zendframework / zend-test

Test component from Zend Framework
BSD 3-Clause "New" or "Revised" License
18 stars 38 forks source link

[Feature Request] Include testing for Zend Expressive App #34

Closed gabbydgab closed 6 years ago

gabbydgab commented 8 years ago

I'm learning to develop an app with TDD + Zend Expressive (inspired by TDD in Laravel). I would like to test features on my app while having a separated test for the implementation details, without using/refreshing a browser, with the following test:

use ZendExpressiveTest\PHPUnit\AbstractHtmlControllerTestCase;

class MyFeatureTest extends AbstractHtmlControllerTestCase
{
     public function setUp()
     {
        $this->setupApplication(include '/my/application/container.config.php');
        parent::setUp();
     }

     public function testMyFeatureCanBeAccessed()
     {
        $this->dispatch('/feature', "GET");
        $this->assertResponseStatusCode(200);
        $this->assertResponseFormat('HTML');
        $this->assertMiddlewareClass('AwesomeFeatureMiddleware');
     }

     public function testMyRestFeatureCanBeAccessed()
     {
        $this->dispatch('/api/rest-feature', "POST", ['key' => 'value']);
        $this->assertResponseStatusCode(201);
        $this->assertResponseFormat('Redirect');
        $this->assertRedirectTo('/feature');
     }
}

/**
* Separate tests for the implementation details
*/
use PHPUnit\Framework\TestCase;

class AwesomeFeatureMiddleware extends TestCase
{
     public function testAwesomeFeatureMiddleware()
     {
        // my unit test here
     }
}

class AwesomeRestFeatureMiddleware extends TestCase
{
     public function testAwesomeRestFeatureMiddleware()
     {
        // my unit test here
     }
}

Similar with Zend-MVC app, it should have the following testing capabilities:

  1. Ability to dispatch request: dispatch($route, $method, $params)
  2. Response Assertions
    • HTML
    • JSON
    • callable $next (with ability to check request attributes and response code)
    • Redirect
  3. CSS Selector Assertions
  4. XPath Assertions

Hoping to have this one. I would love to start this but needs guidance - don't know if it will be integrated as separate library due to expressive dependencies.

Thoughts?

planktonfun commented 7 years ago

Was also looking for this feature

You can use this in the meantime: https://github.com/zendframework/zend-expressive/blob/master/test/IntegrationTest.php

froschdesign commented 7 years ago

Another example: https://github.com/zendframework/zf3-web/blob/master/test/AppTest/WebTest.php

paulocastroo commented 7 years ago

Made one much simpler, this works like CURL but without the server having to run

<?php

namespace Test;

use PHPUnit\Framework\TestCase;
use Zend\Diactoros\ServerRequest;
use \Zend\Expressive\Application;

class MiddlewareTest extends \PHPUnit\Framework\TestCase
{
    /**
     * Initialize required configs and classes
     */
    public function setUp()
    {
        $this->container = require 'config/container.php';
        $this->app = $container->get(\Zend\Expressive\Application::class);
    }

    /**
     * Test middleware from the top
     */
    public function testRoutingWithMultipleMethodsSamePath()
    {
        $app = $this->app;

        $request  = new ServerRequest([], [], '/foo/bar', 'GET');
        $result   = $app->process($request, $app->getDefaultDelegate());

        $this->assertEquals(
            '{success: true}',
            (string) $result->getBody()
        );
    }
}
Ocramius commented 7 years ago

Generally against this, as it is at the edge of e2e testing, but without the benefit of it, and with all its pitfalls. An http-client designed for tests would be better, and yes, it doesn't need to actually run the tests over TCP.

Wouldn't include in expressive, but would rather make it a separate PHPUnit utility/package designed for PSR-7/PSR-15

On 25 Apr 2017 2:03 a.m., "paulocastroo" notifications@github.com wrote:

Made one, its like curl BUT you don't have to run the server to test it

<?php

namespace Test;

use PHPUnit\Framework\TestCase; use Zend\Diactoros\ServerRequest; use \Zend\Expressive\Application;

class MiddlewareTest extends \PHPUnit\Framework\TestCase { /**

  • Initialize required configs and classes */ public function setUp() { $this->container = require 'config/container.php'; $this->app = $container->get(\Zend\Expressive\Application::class); }

    /**

    • @see https://github.com/zendframework/zend-expressive/issues/40 */ public function testRoutingWithMultipleMethodsSamePath() { $app = $this->app;

      $request = new ServerRequest([], [], '/bar/foo/url', 'GET'); $result = $app->process($request, $app->getDefaultDelegate());

      $this->assertEquals( '{success: true}', (string) $result->getBody() ); } }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-test/issues/34#issuecomment-296854409, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJakCF6UJY-ilDLJAJpeGSp846F8aEKks5rzThAgaJpZM4J0cXf .

oshdev commented 6 years ago

I was actually looking to work on this myself. It's true that it's close to E2E testing, but I still see it's value. I would mock 3rd parties and potentially persistence layer if I see fit; and call it application test. It would be testing all middleware layers in my app - starting from creating a PSR-7 Request and calling the app with with it (similar to the above linked ZF3 WebApp test) and returning response to perform desired assertions on it.

Not sure if it's viable to try to create a general package for testing PSR-7 and 15 micro frameworks as they're using the same components, but in various ways.

Another thing, I would not include this here, as per description - Zend Test is for testing ZF MVC, not Expressive app. (I came across this as I was looking for it myself).

weierophinney commented 6 years ago

We will not be shoe-horning Expressive-compatible testing features into zend-test.

If somebody would like to make a general-purpose PHPUnit utility package for testing PSR-7/PSR-15 packages, we can direct folks to that, or potentially bring it under the zendframework organization, but this repository is definitely not the location for such features.

froschdesign commented 6 years ago

For PSR-7: https://github.com/martin-helmich/phpunit-psr7-assert