1
\$\begingroup\$

I am trying to test my first functional tests. I would like to know if my functional test is well done, or if the structure of it is not optimal.

Any advice will be useful to me. As I said, I just started writing functional tests, so I have major doubts about my test method which regroups all of a sudden or should it separate?

<?php

namespace Api\Tests\Post;

require_once __DIR__.'/../../../vendor/autoload.php';

use Api\Util\Error;
use Silex\Application;
use Silex\WebTestCase;

use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\HttpFoundation\Response;

class UserTest extends WebTestCase
{
/**
 * Basic, application-wide functional test inspired by Symfony best practices.
 * Simply checks that all application URLs load successfully.
 * During test execution, this method is called for each URL returned by the provideUrls method.
 *
 * @dataProvider provideUrls
 * @param $url
 */
public function testPageIsSuccessful($url)
{
    $password   = '%sZ69%$%C5sRz4_5Od$46#5';

    /**
     * Create user with just field required
     */
    $client     = $this->createClient();

    $randomName = rand(1000, 999999);

    $client->request(
        'POST',
        $url,
        array(),
        array(),
        Error::$header,
        '{
            "email":"test+'.$randomName.'@Test.com",
            "password":"'.$password.'",
            "gender":"m",
            "firstName":"Test",
            "lastName":"'.$randomName.'"
        }'
    );

    $object = json_decode($client->getResponse()->getContent(), false);

    $this->assertNotNull($object);
    $this->assertTrue(is_object($object));
    $this->assertEquals($randomName, $object->lastName);
    $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode(), $client->getResponse()->getContent());


    /**
     * Create user with all field
     */
    $client     = $this->createClient();

    $randomName = rand(1000, 999999);

    $client->request(
        'POST',
        $url,
        array(),
        array(),
        Error::$header,
        '{
            "email":"test2+'.$randomName.'@Test.com",
            "password":"'.$password.'",
            "gender":"m",
            "firstName":"Test",
            "lastName":"'.$randomName.'",
            "image":null,
            "address":null,
            "enabled":1,
            "locked":0,
            "role":["ROLE_ANDROID"],
            "content":"Une description complète sur cette personne",
            "birthday":"1991-06-12",
            "facebookId":null,
            "avatar":null,
            "phone":"0601020304",
            "profession":"Robot",
            "iHostWith":null
        }'
    );

    $object = json_decode($client->getResponse()->getContent(), false);

    $this->assertNotNull($object);
    $this->assertTrue(is_object($object));
    $this->assertEquals($randomName, $object->lastName);
    $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode(), $client->getResponse()->getContent());



    /**
     * Create user with birthday not good
     */
    $client     = $this->createClient();

    $randomName = rand(1000, 999999);

    $client->request(
        'POST',
        $url,
        array(),
        array(),
        Error::$header,
        '{
            "email":"test3+'.$randomName.'@Test.com",
            "password":"'.$password.'",
            "gender":"m",
            "firstName":"Test",
            "lastName":"'.$randomName.'",
            "image":null,
            "address":null,
            "enabled":1,
            "locked":0,
            "role":["ROLE_ANDROID"],
            "content":"Une description complète sur cette personne",
            "birthday":"2001-06-15",
            "facebookId":null,
            "avatar":null,
            "phone":"0601020304",
            "profession":"Robot",
            "iHostWith":null
        }'
    );

    $object = json_decode($client->getResponse()->getContent(), false);

    $this->assertEquals(Error::USER_AGE_MIN_REQUIRED, $object->code);
    $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode(), $client->getResponse()->getContent());


    /**
     * Create user with birthday good
     */
    $client     = $this->createClient();

    $randomName = rand(1000, 999999);

    $client->request(
        'POST',
        $url,
        array(),
        array(),
        Error::$header,
        '{
            "email":"test3+'.$randomName.'@Test.com",
            "password":"'.$password.'",
            "gender":"m",
            "firstName":"Test",
            "lastName":"'.$randomName.'",
            "image":null,
            "address":null,
            "enabled":1,
            "locked":0,
            "role":["ROLE_ANDROID"],
            "content":"Une description complète sur cette personne",
            "birthday":"1991-06-12",
            "facebookId":null,
            "avatar":null,
            "phone":"0601020304",
            "profession":"Robot",
            "iHostWith":null
        }'
    );

    $object = json_decode($client->getResponse()->getContent(), false);

    $this->assertNotNull($object);
    $this->assertTrue(is_object($object));
    $this->assertEquals($randomName, $object->lastName);
    $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode(), $client->getResponse()->getContent());


    /**
     * Create user with password with min letters
     */
    $client     = $this->createClient();

    $randomName = rand(1000, 999999);

    $client->request(
        'POST',
        $url,
        array(),
        array(),
        Error::$header,
        '{
            "email":"test3+'.$randomName.'@Test.com",
            "password":"12345",
            "gender":"m",
            "firstName":"Test",
            "lastName":"'.$randomName.'",
            "image":null,
            "address":null,
            "enabled":1,
            "locked":0,
            "role":["ROLE_ANDROID"],
            "content":"Une description complète sur cette personne",
            "birthday":"1991-06-12",
            "facebookId":null,
            "avatar":null,
            "phone":"0601020304",
            "profession":"Robot",
            "iHostWith":null
        }'
    );

    $object = json_decode($client->getResponse()->getContent(), false);

    $this->assertEquals(Error::USER_PASSWORD_TOO_SHORT, $object->code);
    $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode(), $client->getResponse()->getContent());
}

/**
 * {@inheritDoc}
 */
public function createApplication()
{
    $app = new Application();

    require __DIR__.'/../../../app/config/dev.php';
    require __DIR__.'/../../../app/app.php';
    require __DIR__.'/../../../app/routes.php';

    // Generate raw exceptions instead of HTML pages if errors occur
    ExceptionHandler::register(false);
    // Simulate sessions for testing
    $app['session.test']            = true;
    // Enable anonymous access to admin zone
    $app['security.access_rules']   = array();

    return $app;
}

/**
 * Provides all valid application URLs.
 *
 * @return array The list of all valid application URLs.
 */
public function provideUrls()
{
    return [
        array('/users/')
    ];
}
}
\$\endgroup\$
1
  • \$\begingroup\$ I'm having trouble understanding this part: "which regroups all of a sudden or should it separate?" What did you mean by that? \$\endgroup\$
    – Mast
    Nov 30, 2016 at 10:07

1 Answer 1

1
\$\begingroup\$

You should follow the Single Responsibility Principle (SRP).

  • Each class has to solve exact one issue. Same applies for methods/functions
    • In your case your Test class creates an application and does unit tests
    • Further you have one test method which tests multiply cases.

So, rather create multiply test methods such as:

  • testRegisterUserUsingInvalidBirthdate
  • testRegisterUserWithCompleteData

You include an autoloader in the test class. require_once __DIR__.'/../../../vendor/autoload.php'; From UnitTests I am aware of a bootstrap file. If such a class exists/can be registered for functional tests as well you should move it to a respective file.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.