Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Tests to improve stability and avoid code regressions #71

Closed
virtualtam opened this issue Dec 8, 2014 · 16 comments
Closed

Add Unit Tests to improve stability and avoid code regressions #71

virtualtam opened this issue Dec 8, 2014 · 16 comments
Labels
cleanup code cleanup and refactoring enhancement

Comments

@virtualtam
Copy link
Member

The core of Shaarli is a big chunk of PHP: index.php.
It contains all the methods, which doesn't make editing and debugging convenient (lack of modularity). A good way to avoid bugs and regressions would be to implement unit testing, starting with sensitive features:

  • data storage (read/write),
  • authentication,
  • import / export.

This could be done with a unit test framework like PHPUnit; see the awesome Kanboard project for an example implementation.

Some readings about the pros of unit testing everything:

@nodiscc
Copy link
Member

nodiscc commented Dec 8, 2014

Unit tests would be great to have.

The core of Shaarli is a big chunk of PHP: index.php.

We could start by moving classes to a shaarli.class.php and include() it.

@virtualtam
Copy link
Member Author

By experience, it's safer to first cover features with unit tests, then you can see what is broken / hidden when refactoring (in other words, "blind" refactoring, along with being a tedious task, is definitely not recommended...).

I think there is an historical & practical reason for Shaarli to be made of a single file, but, as it is now comprised of several resource (style, images, templates) subdirs, it would greatly benefit of having a more modular shape (a better readability makes spotting bugs and maintaining code way easier).

@e2jk
Copy link

e2jk commented Dec 8, 2014

I really like the idea of creating unit tests.

The original goal of @sebsauvage was indeed to have everything contained in
one single php file. That stopped being the case 3 years ago with:

  • version 0.0.12beta [0] introducing jQuery files
  • version 0.0.15beta [1] creating a separate CSS file
  • version 0.0.28beta [2] adding the images folder
  • The big one, version 0.0.32beta [3] using the templating engine

As far as I'm concerned, we're past the "fits in a single file" stage, and
this isn't mentioned anymore either on Github or Seb's wiki page.

I agree with creating the unit tests first, and then refactoring into
multiple files (those new PHP files should be put in a separate folder)

[0]
https://github.com/shaarli/Shaarli/tree/44a9d860e191878058072ab872be3a8e3339d21b
[1]
https://github.com/shaarli/Shaarli/tree/eae4f48bf0b4137ee4764d06678ba6e6a9c74978
[2]
https://github.com/shaarli/Shaarli/tree/6d946e78bfca94ac89494e84db3746473867c5ff
[3]
https://github.com/shaarli/Shaarli/tree/3433e5e8a84e3952502b79296f945e6dde2a7d75

@nodiscc nodiscc added the cleanup code cleanup and refactoring label Jan 9, 2015
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Feb 25, 2015
Relates to shaarli#71
Relates to shaarli#95

Additions:
- Makefile for easy usage,
- Composer file to define dev & test dependencies.

Features:
- PHP Copy/Paste Detect: detect duplicate code;
- PHP Code Sniffer: static analysis, syntax checking,
- PHP Mess Detector: static analysis, syntax checking.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Feb 25, 2015
Relates to shaarli#71
Relates to shaarli#95

Additions:
- Makefile for easy usage,
- Composer file to define dev & test dependencies.

Features:
- PHP Copy/Paste Detect: detect duplicate code;
- PHP Code Sniffer: static analysis, syntax checking,
- PHP Mess Detector: static analysis, syntax checking.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Feb 25, 2015
Relates to shaarli#71
Relates to shaarli#95

Additions:
- Makefile for easy usage,
- Composer file to define dev & test dependencies.

Features:
- PHP Copy/Paste Detect: detect duplicate code;
- PHP Code Sniffer: static analysis, syntax checking,
- PHP Mess Detector: static analysis, syntax checking.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Feb 26, 2015
Relates to shaarli#71
Relates to shaarli#95

Additions:
- Makefile for easy usage,
- Composer file to define dev & test dependencies.

Features:
- PHP Copy/Paste Detect: detect duplicate code;
- PHP Code Sniffer: static analysis, syntax checking,
- PHP Mess Detector: static analysis, syntax checking.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Feb 26, 2015
Relates to shaarli#71
Relates to shaarli#95

Additions:
- Makefile for easy usage,
- Composer file to define dev & test dependencies.

Features:
- PHP Copy/Paste Detect: detect duplicate code;
- PHP Code Sniffer: static analysis, syntax checking,
- PHP Mess Detector: static analysis, syntax checking.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Feb 26, 2015
Relates to shaarli#71
Relates to shaarli#95

Additions:
- Makefile for easy usage,
- Composer file to define dev & test dependencies.

Features:
- PHP Copy/Paste Detect: detect duplicate code;
- PHP Code Sniffer: static analysis, syntax checking,
- PHP Mess Detector: static analysis, syntax checking.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Mar 5, 2015
Relates to shaarli#71
Relates to shaarli#95

Additions:
- Makefile for easy usage,
- Composer file to declare dev & test dependencies.

Features:
- PHP Copy/Paste Detect: detect duplicate code;
- PHP Code Sniffer: static analysis, syntax checking,
- PHP Mess Detector: static analysis, syntax checking.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Mar 5, 2015
Relates to shaarli#71
Relates to shaarli#95

Additions:
- Makefile for easy usage,
- Composer file to declare dev & test dependencies.

Features:
- PHP Copy/Paste Detect: detect duplicate code;
- PHP Code Sniffer: static analysis, syntax checking,
- PHP Mess Detector: static analysis, syntax checking.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
@nodiscc
Copy link
Member

nodiscc commented Mar 9, 2015

Related #130 (comment):

A patch for a simple, initial test would be accepted.

@nodiscc
Copy link
Member

nodiscc commented Mar 10, 2015

#130 (comment)

Here's an efficient workflow, to bring tests to a software:

identify core / sensitive features,
cover them with tests, as relevant as possible:
nominal usage: what is a method supposed to do?,
nominal error handling: what is likely to break often?,
borderline cases: erroneous values, invalid arguments, etc.
this will likely highlight issues & flaws in the original code,
fix'em all!

Having a good coverage before doing any refactoring makes the process way easier: any new issue or behavior alteration will be instantly spotted!

Then, refactoring may happen as follows:

  • think of a cool, elegant new design,
  • update test code to fit the expected design
    • all tests will break!
  • update / refactor the code until all tests are passing
    • again, issues may be spotted, covered and fixed.

Tests specify what the software is expected to perform, and how it is used, thus their importance ;-)
[...]
What unit and functional tests have brought:

  • instant spotting of any regression,
  • hyper-stable codebase,
  • bugs we're facing are mostly minor, and can be fixed very quickly,
  • easy refactoring,
  • developer serenity ;-)

[...]
Oh, and the nice things about testing:

  • writing them is not that hard, just time-consuming at first,
  • it makes contributing simpler:
  • for the contributor: test tell you if your contribution is good/stable enough for submission,
  • for reviewers: you can see what has been brought by the PR, how it works,
  • it won't prevent people from contributing; quite the opposite, actually: it shows you care about stability.

@nodiscc
Copy link
Member

nodiscc commented Mar 11, 2015

A few things to check for:

@virtualtam
Copy link
Member Author

Some test-able points:

  • the easy one:
    • default class constructors;
  • the functional one:
    • database interactions;
    • reading, writing, transactions;
    • parallel access (if any; the Python sqlite3 module allows multi-threaded connections, for instance);
  • the security one:
    • session handling;
    • authentication;
    • password bruteforce / IP blacklist;
    • XSS.

And the way more advanced stuff (for the sole sake of awareness):

  • template rendering;
  • user interaction / browser automation: SeleniumHQ.

As a starter, I suggest writing tests for the linkdb class, which will provide hints on how to:

  • write a test,
  • test a class and its methods,
  • check DB interactions,
  • run tests ;-)

if it's OK, I'll start working on a PR to experiment unit test features; as usual, any feedback will be much appreciated :)

@nodiscc
Copy link
Member

nodiscc commented Mar 11, 2015

Yep, please proceed! A well commented test script would help. It's still a bit unclear to me how/what to test.

virtualtam added a commit to virtualtam/Shaarli that referenced this issue Mar 12, 2015
Relates to shaarli#71

Coverage:
 - constructor.

Utilities:
 - Composer: PHPUnit;
 - Makefile: test target.

TODO:
 - write the actual tests;
 - factorize test config (globals, settings)
 - more Makefile options:
   - code coverage;
   - test discovery
   - output formatting.

TODO (to be discussed):
 - make LinkDB code PSR-compliant;
 - reformat code comments;
 - use PHP's autoload for Shaarli classes.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Mar 12, 2015
Relates to shaarli#71

Coverage:
 - constructor.

Utilities:
 - Composer: PHPUnit;
 - Makefile: test target.

TODO:
 - write the actual tests;
 - factorize test config (globals, settings)
 - more Makefile options:
   - code coverage;
   - test discovery;
   - output formatting.

TODO (to be discussed):
 - make LinkDB code PSR-compliant;
 - reformat code comments;
 - use PHP's autoload for Shaarli classes.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
@nodiscc nodiscc modified the milestone: future Mar 13, 2015
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Mar 16, 2015
Relates to shaarli#71

Coverage:
 - constructor.

Utilities:
 - Composer: PHPUnit;
 - Makefile: test target.

TODO:
 - write the actual tests;
 - factorize test config (globals, settings)
 - more Makefile options:
   - update lint targets;
   - code coverage;
   - test discovery;
   - output formatting.

TODO (to be discussed):
 - make LinkDB code PSR-compliant;
 - reformat code comments;
 - use PHP's autoload for Shaarli classes.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Mar 16, 2015
Relates to shaarli#71

Coverage:
 - constructor.

Utilities:
 - Composer: PHPUnit;
 - Makefile: test target.

TODO:
 - write the actual tests;
 - factorize test config (globals, settings)
 - more Makefile options:
   - update lint targets;
   - code coverage;
   - test discovery;
   - output formatting.

TODO (to be discussed):
 - make LinkDB code PSR-compliant;
 - use PHP's autoload for Shaarli classes.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Mar 16, 2015
Relates to shaarli#71

Coverage:
 - constructor.

Utilities:
 - Composer: PHPUnit;
 - Makefile: test target.

TODO:
 - write the actual tests;
 - factorize test config (globals, settings)
 - more Makefile options:
   - update lint targets;
   - code coverage;
   - test discovery;
   - output formatting.

TODO (to be discussed):
 - make LinkDB code PSR-compliant;
 - use PHP's autoload for Shaarli classes.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Mar 16, 2015
Relates to shaarli#71

Coverage:
 - constructor.

Utilities:
 - Composer: add PHPUnit to dev dependencies;
 - Makefile:
    - update lint targets;
    - add test targets.

TODO:
 - write the actual tests;
 - more Makefile options:
   - code coverage;
   - test discovery;
   - output formatting.

TODO (to be discussed):
 - make LinkDB code PSR-compliant;
 - use PHP's autoload for Shaarli classes.

Signed-off-by: VirtualTam <virtualtam@flibidi.org>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Jun 4, 2015
Relates to shaarli#71

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Jun 4, 2015
Relates to shaarli#71

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Jun 4, 2015
Relates to shaarli#71

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Jun 4, 2015
Relates to shaarli#71

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Jun 10, 2015
Relates to shaarli#71

LinkDB
 - move to application/LinkDB.php
 - code cleanup
   - indentation
   - whitespaces
   - formatting
 - comment cleanup
   - add missing documentation
   - unify formatting

Test coverage for LinkDB
 - constructor
 - public / private access
 - link-related methods

Shaarli utilities (LinkDB dependencies)
 - move startsWith() and endsWith() functions to application/Utils.php
 - add test coverage

Dev utilities
 - Composer: add PHPUnit to dev dependencies
 - Makefile:
    - update lint targets
    - add test targets
    - generate coverage reports

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
virtualtam added a commit to virtualtam/Shaarli that referenced this issue Jun 10, 2015
Relates to shaarli#71

LinkDB
 - move to application/LinkDB.php
 - code cleanup
   - indentation
   - whitespaces
   - formatting
 - comment cleanup
   - add missing documentation
   - unify formatting

Test coverage for LinkDB
 - constructor
 - public / private access
 - link-related methods

Shaarli utilities (LinkDB dependencies)
 - move startsWith() and endsWith() functions to application/Utils.php
 - add test coverage

Dev utilities
 - Composer: add PHPUnit to dev dependencies
 - Makefile:
    - update lint targets
    - add test targets
    - generate coverage reports

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
@virtualtam virtualtam modified the milestones: future, 0.7.0 Jul 30, 2015
@mro
Copy link

mro commented Sep 3, 2015

I did a demo on integration tests (running against a webserver) over there: #16 (comment)

Maybe that's interesting to add (testing not multiple source distributions, but the current github code only).

What do you guys think?

P.S.: a driver to run the tests may be mink - see http://robbiemackay.com/2013/05/03/automating-behat-and-mink-tests-with-travis-ci/

@virtualtam
Copy link
Member Author

@mro thanks for taking the first step towards functional testing ;-)

After a quick glance to phptestdemo, it seems to fairly increase the complexity of the Travis configuration, and will require a bit of tuning to get a minimal setup suitable to testing Shaarli.

Some comments & ideas:

  • Docker migrated to a container-based infrastructure some times ago (see Travis: use the container-based infrastructure #290)
  • the matrix may not be needed - Travis already runs a build on each branch + all PRs
  • Nginx + PHP-FPM tend to be lighter to deploy than Apache2 and more flexible to configure
  • though, supporting multiple web server configurations (Apache, Nginx, lighthttpd) could be a huge asset to tackle server/config-specific issues (sessions, headers, cookies, proxy...)

I have to admit I'm quite enthusiastic with experimenting functional testing, which could be introduced starting #333 - Refactor HTTP / Url functions (HTTP requests and response headers involved)

@mro
Copy link

mro commented Sep 3, 2015

@virtualtam things may not be so easy (when testing multiple shaarli versions as I want to do). See https://travis-ci.org/mro/phptestdemo/builds/78607719

The issues there - even with a extremely simple test - are:

  • vanilla shaarli doesn't work with port other than 80 but travis + docker prevents sudo (and port numbers < 1024) for now,
  • php 5.3 seems to lack web root command line switch

Both restrictions may be acceptable for this community shaarli. See https://github.com/mro/phptestdemo/tree/feature/docker .travis.ymland scripts/run-tests.sh for details.

@virtualtam
Copy link
Member Author

Regarding the port limitation, I wonder if authbind could be used within a Travis docker appliance to allow serving on :80.

Example (provided chmod/chown are available):

touch /etc/authbind/byport/80
chmod 500 /etc/authbind/byport/80
chown <MY_PROCESS_USER> /etc/authbind/byport/80

@mro
Copy link

mro commented Sep 5, 2015

ok, pushed the integration tests a bit further, see #16 (comment)

@mro
Copy link

mro commented Sep 21, 2015

added an important test (for API #16, #113, #342, #130 when adding a link and currently sadly failing) in PR #354

@virtualtam
Copy link
Member Author

Closing this generic thread as we're improving test coverage along code refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup code cleanup and refactoring enhancement
Projects
None yet
Development

No branches or pull requests

4 participants