phptestingsilex

Silex: How and why to use testing


The documentation on testing is not really clear to me. It says you can use testing to check whether my pages and my system works correctly.

In my opinion, that would mean it could check whether all rights are used properly or something like that. But I don't understand why I'd put so much time in coding the testing part.

I hope someon could explain and convince me why I should use testing, and as a second part: HOW I should use it!

So, as an example, this could be my basic index.php file:

<?php

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

$app = new Silex\Application();
$app['debug'] = true;

$routes = $app['controllers_factory'];

$routes->match('/', function () use ($app) {
    return 'Hello';
});

$app->mount('/', $routes);
$app->run();

Could you give me an example or adjust my route and give some testing with it. And please don't use the examples from the documentation: they don't make sense to me and that's the whole reason why I'm asking it here.

EDIT

So, both your comments were helpfull. I think I'm starting to understand why it might be usefull. Now, all I need is an example to fully understand it. Let's say I've got this route:

$routes->match('/user/login', function (Request $request) use ($app) {

    $form = $app['form.factory']->createBuilder(FormType::class)
        ->add('username', TextType::class, array(
            'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 3,'max' => 3))),
            'label' => 'Username',
            'required' => 'required',
            'attr' => array('class' => 'input-field', 'autocomplete' => 'off', 'placeholder' => 'Docent afkorting'),
            'label_attr' => array('class' => 'label'),
            'error_bubbling' => true
        ))
        ->add('password', PasswordType::class, array(
            'constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 5))),
            'label' => 'Password',
            'required' => 'required',
            'attr' => array('class' => 'input-field', 'autocomplete' => 'off', 'placeholder' => 'Wachtwoord'),
            'label_attr' => array('class' => 'label'),
            'error_bubbling' => true
        ))
        ->add('submit', SubmitType::class, [
            'label' => 'Login',
            'attr' => array('class' => 'submit'),
        ])
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $data = $form->getData();
        $user = new User();

        if($user->login($data['username'],$data['password'])){
            return $app->redirect($app['url_generator']->generate('home'));
        }
        else{
            return $app['twig']->render('form.twig', [
                'content' => 'Incorrect username or password<br><br>',
                'form' => $form->createView(),
            ]);
        }
    }

    return $app['twig']->render('form.twig', [
        'form' => $form->createView()
    ]);
})->bind('user.login');

What would I have to do to have a proper test?


Solution

  • If you are not familiar with software tests, you may be wondering why you would need this. Every time you make a change to your application, you have to test it. This means going through all the pages and making sure they are still working. Functional tests save you a lot of time, because they enable you to test your application in usually under a second by running a single command.

    Source: https://silex.symfony.com/doc/2.0/testing.html#why