GithubHelp home page GithubHelp logo

cakephp-test-helper's Introduction

CakePHP TestHelper plugin

CI Coverage Status Latest Stable Version Minimum PHP Version License Total Downloads Coding Standards

Browser based addons for your test driven development.

Note: This branch is for CakePHP 5.0+. See version map for details.

Motivation

After 2.x=>3.x, the "web-tester" has been removed. It was, for certain cases, however, quite useful. This aims to bring back a part of it.

The CLI also doesn't allow a good overview. Even with auto-complete, you have to type almost everything out. With a browser backend generating tests or running them is just a simple mouse click.

You have an overview of your classes and the test classes to it. If there is one missing, you can easily "bake" it from this web backend. It internally uses Bake plugin as well as your preferred theme.

Further useful addons

  • URL array generation from string URLs (respects routing, so it is basically also a reverse lookup)
  • Fixture validation tool (compares actual DB with the schema files: fields and attributes, constraints and indexes)
  • GUI for fixture comparison and generation of missing ones per mouse click.

Installation

You can install this plugin into your CakePHP application using composer:

composer require --dev dereuromark/cakephp-test-helper

Note: This is not meant for production, so make sure you use the --dev flag and install it as development-only tool.

Setup

Don't forget to load it under your bootstrap function in Application.php:

if (Configure::read('debug')) {
    $this->addPlugin('TestHelper');
}

This will also load the routes.

non-dev mode

In certain apps it can be useful to have some of the helper functionality available also for staging and prod. Here you must make sure then to not load the routes, though:

$this->addPlugin('TestHelper', ['routes' => Configure::read('debug')]);

And here you must use composer require without --dev flag then.

Usage

Navigate to /test-helper backend and select the app or plugin you want to check. You can then with a single click

  • check what classes do not yet have a test case
  • generate a test case for them (or copy and paste a generated code into CLI)
  • run test case
  • check coverage on a tested class, as overall and in detail

Supported class types:

  • Controllers
  • Models (Tables/Entities)
  • Components
  • Behavior
  • Helpers
  • Commands
  • Tasks
  • Cells
  • CommandHelpers
  • Forms
  • Mailers

Feel free to help out improving and completing this test helper plugin.

Limitations

Executing the tests and coverage from the web backend usually can not work for long-running tests due to the timeout issues. Make sure you raise the apache/nginx settings here if you want to use this functionality here.

The focus is on providing an overview and quickly generating the desired classes with a single mouse click.

cakephp-test-helper's People

Contributors

dereuromark avatar highstrike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cakephp-test-helper's Issues

Missing CSRF Token Cookie

Version: 0.2.3
CakePHP: 3.8.9

I just tested the plugin in an application which has the CSRF protectoin activated by default.
Submitting anything under /test-helper leads to a Missing CSRF token cookie exception.

Either this can be worked around or a warning should be added to the documentation

Importing Model in Fixture leads to Invalid Comparison Result

Version: 0.2.3
CakePHP: 3.8.9

I compared the fixture(s) of a plugin in an application that imports the model.

The fixture:

<?php
namespace PartnerQuiz\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
 * Partner Quiz Submissions Fixtures
 */
class PartnerQuizSubmissionsFixture extends TestFixture
{
    /**
     * Import from model
     *
     * @var array
     */
    public $import = ['model' => 'PartnerQuizSubmissions'];

    /**
     * Init method
     *
     * @return void
     */
    public function init()
    {
        $this->records = [
            [
                'id' => 1,
                'salutation' => 'L',
                'last_name' => 'Müller',
                'first_name' => 'Markus',
                'company_name' => 'Müller GmbH',
                'date_of_birth' => '1953-10-01 00:00:00',
                'email' => '[email protected]',
                'quiz_answer' => 12345,
                'participation_conditions_accepted' => 1,
                'newsletter_subscribed' => 1,
                'created' => '2019-11-28 15:05:24',
                'modified' => '2019-11-28 15:05:24',
            ],
        ];
        parent::init();
    }
}

The comparison result:

>php bin/cake.php fixture_check -v -p PartnerQuiz
1 fixtures found, processing:

Comparing `PartnerQuiz\Test\Fixture\PartnerQuizSubmissionsFixture` with table `partner_quiz_submissions`
Live DB has fields that are not in PartnerQuiz\Test\Fixture\PartnerQuizSubmissionsFixture
 * company_name
 * created
 * date_of_birth
 * email
 * first_name
 * id
 * last_name
 * modified
 * newsletter_subscribed
 * participation_conditions_accepted
 * quiz_answer
 * salutation

The following constraints mismatch:
 * Constraint "primary" {"type":"primary","columns":["id"],"length":[]} is missing in fixture, but in live DB.
 * Constraint "UX_email" {"type":"unique","columns":["email"],"length":[]} is missing in fixture, but in live DB.


Copy-paste the following for fixture updating:
bin/cake bake fixture partner_quiz_submissions -f -p PartnerQuiz
Differences detected, check your fixtures and DB.

The mismatch found doesn't make sense, as it imports from the model:

The following constraints mismatch:
 * Constraint "primary" {"type":"primary","columns":["id"],"length":[]} is missing in fixture, but in live DB.
 * Constraint "UX_email" {"type":"unique","columns":["email"],"length":[]} is missing in fixture, but in live DB.

In such a case I would expect no diff.

RE-Bake?

Should we also have a re-bake option?
If you changed the code, and you cannot just normally rebake, we could dry-run it and show a diff of what changes.
And you could partially apply those changes (added validation rules, form fields, ....)?

This could be a bit more complicated however to accomplish.

This plugin is missing some stuff

You must include the assets for the "backend" interface found in /test-helper

This plugin repository is currently lacking:

  • it's own layout.ctp.
  • assets like jquery, font awesome and the modal lib that are being used in the templates.
  • the Flash component should also be loaded by default as it's being used in the controllers.
  • the Flash design elements (Element/Flash/success.ctp and Element/Flash/error.ctp) maybe look at my component and include that in the project as well because it will load the elements from the plugin (once included).
  • the Tools.Format helper should also be loaded by default as it's being used in the views.
  • the TestHelper helper should also be loaded by default

Extra stuff needed to make this plugin work

currently my project's AppController.php looks like this:

class AppController extends Controller
{
    public function initialize() {
        parent::initialize();

        // dereuromark's testhelper
        if($this->request->getParam('plugin') == 'TestHelper') {
            $this->loadComponent('Flash');
        }
    }
}

and my project's AppView.php looks like this:

class AppView extends View
{
    public function initialize() {
        parent::initialize();

        // dereuromark's testhelper
        if($this->request->getParam('plugin') == 'TestHelper') {
            $this->loadHelper('Test'); //// this is my helper that will overwrite the layout in my project
            $this->loadHelper('Tools.Format');
        }
    }
}

Fixture Check to Migration file

with -d (direction) we can also generate migration files from the diff directly.

E.g. my result of

Comparing `App\Test\Fixture\AttendeesFixture` with table `attendees`
Live DB has fields that are not in App\Test\Fixture\AttendeesFixture
 * created
 * modified
 * status

The following field attributes mismatch:
 * Field attribute `from:null` differs from live DB! (`true` vs `false` live)
 * Field attribute `to:null` differs from live DB! (`true` vs `false` live)

Comparing `App\Test\Fixture\EventsFixture` with table `events`
The following field attributes mismatch:
 * Field attribute `from:type` differs from live DB! (`'datetime'` vs `'date'` live)
 * Field attribute `from:null` differs from live DB! (`true` vs `false` live)
 * Field attribute `name:default` is missing from the live DB!
 * Field attribute `to:type` differs from live DB! (`'datetime'` vs `'date'` live)
 * Field attribute `to:null` differs from live DB! (`true` vs `false` live)

Comparing `App\Test\Fixture\UsersFixture` with table `users`
Live DB has fields that are not in App\Test\Fixture\UsersFixture
 * email_confirmed
 * irc_nick

Could become a Migrations file when the option -m ClassName is passed and the migrations plugin is loaded/available.

Vendor plugins don't work

trying to generate tests for my plugin (which is in vendor/ doesn't work)

i clicked the "+" icon which redirected into 404 http://dev.local/test-helper/test-cases/component/Unimatrix%2FFrontend

image preview

image

fixture_check Command doesn't check plugins by default

Version: 0.2.3
CakePHP: 3.8.9

When executing the fixture_check command on an applicaton with no fixtures in the app directly but some in one of the several plugins, it leads to no fixtures found

>php bin/cake.php fixture_check -v
0 fixtures found, processing:


All fine :)

As a total noob of this plugin I would have exptected that this command goes through all of my plugins by default. At least, I would have expected that the plugin tells me which pats of the app it checks. This way, it's clear.

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.