monomelodies / gentry
PHP unit test generation tools
Requires
- php: >=7
- jonnyw/php-phantomjs: 4.*
- monomelodies/dabble: ^1.5
- monomelodies/kingconf: ^1.0
- psr/cache: ^1.0
- simoneast/simple-ansi-colors: ^1.0
- zeptech/annotations: ^1.0.0
- dev-master
- 0.9.0
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.7
- 0.7.6
- 0.7.5
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.1
- 0.4.0
- 0.3.9
- 0.3.8
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-develop
- dev-testing-framework
This package is auto-updated.
Last update: 2022-02-01 13:14:40 UTC
README
A testing framework for PHP7+
Good programmers are lazy, but unfortunately that means that stuff like writing tests (boooooring) is often skipped. Please don't; it's important and oh so handy once you have them in place.
Gentry was designed with three goals in mind:
- To make writing tests so easy even the worst slacker will bother;
- To alleviate writing boilerplate code by generating skeletons for you.
- Speed. You want to run tests before you push, so if they're slow that's annoying.
Prerequisites
- Make sure you have PHP7. Seriously, Gentry uses some new features so it'll fail miserably on anything older.
- Turn on assertions and configure them to throw
AssertionError
on failure. See this section in the manual; both values should be set to1
.
Installation
Composer (recommended)
composer require --dev monomelodies/gentry
You can now run vendor/bin/gentry
.
Manual
Download or clone the repo. There's an executable in the root.
Configuration
Create a Gentry.json
file in the root of your project. It uses the following
options:
{ "src": "/path/to/src", "tests": "/path/to/tests", "bootstrap": "/path/to/bootstrap.php", "namespace": "Foo", "ignore": "some.*?regex" }
string|array src
string tests
Both src
and tests
can be either absolute, or relative to the root - hence
"/path/to/root/src"
could be simplified to just "src"
.
Directories are recursed. If Gentry detects that tests
is inside src
, it
skips it for you (but seriously, don't do that).
Gentry supports multiple src
directories, but only one tests
directory. The
simple reasoning is that it's not uncommon to place scripts in a bin
directory
outside of src
, but still have them tested.
string|array bootstrap
The path(s) to file(s) ("bootstrapper(s)") every piece of code in your
application needs. This is usually something that would reside in an index.php
entry point or similar file. These files are otherwise ignored by Gentry when
analysing your code and should do stuff like initialise an autoloader.
You can also pass an array of files instead of a string. They will be prepended in order.
includePath
is parsed before bootstrap
, so if you use them in conjunction
you could use relative paths here. Otherwise, they will be relative to
get_cwd()
.
Caution: if
bootstrap
ped files reside insidesrc
, they won't be ignored. Gentry usesrequire_once
of course, but if these files contain testable features it will try and do something sensible with them.
This isn't necessarily a bad thing; you could actually write tests that test the mock objects you use in other tests :)
string ignore
A regular expression of classnames to ignore in the "src"
path. Useful for
automatically ignoring classtypes that are hard to test, e.g. controllers. You
could also utilise this if your tests and sourcecode are mixed (but seriously,
don't do that).
Usage
Now run Gentry from the command line and see what happens:
vendor/bin/gentry
It'll complain that it can't do anything yet. Which makes sense, we haven't written any tests yet!
Verbose mode
If you'd like more info, run Gentry with the -v
flag:
vendor/bin/gentry -v
In the default mode, only important messages are displayed. But verbose mode might be handy when something's going wrong for you, or if you simply want feedback about stuff like incomplete tests.
Detecting the environment
For a lot of testing, you'll need to detect whether or not to use a mock object
(e.g. for database connections), or "the real thang". The simplest way is to
check getenv("GENTRY")
where needed. Gentry's executable sets that for you, so
it's a sure-fire way of knowing you're in testing mode. Unless you're using that
same environment variable yourself somewhere. But that would be silly.
Generating missing tests
Run Gentry with the -g
flag to generate skeletons for missing tests for you:
vendor/bin/gentry -g
More on generating tests in the corresponding section of the manual.