Because this is the way you ensure your code is awesome, and provably works!
If you you have some code you think could be reused in lots of projects, create a repository in Github and share it with the world!
I set up a project on github called delboy1978uk/blank. It’s literally a blank PHP project waiting to be written.
However it already has configuration for Travis for automated testing, and it also hooks up with Scrutinizer, which will analyse the code coverage report Travis sends it, giving you reports on how many of your class methods have been tested, and the quality of your code (less complex methods = good!).
You can clone the repository, rename a folder and a couple of files, and search and replace my blank class name refs, then you are good to go! Just composer install and you can run tests by running:
vendor/bin/codecept run unit --coverage-html
The coverage option generates an html report you can browse in the tests/output folder.
If you try it straight away you’ll see there is one test in tests/unit/del/BlankTest.php. Tests usually follow this pattern. If it says you pass a string as an argument, and return some object, then test it! The simplest example is a getter/setter. If you run setsomething(‘hello’); then getSomething() should be ‘hello’. Most tests say something like $this->assertTrue() or $this->assertInstanceOf() or $this->assertEquals(). You get the idea. Check the code coverage reports to ensure the tests cover all eventualities. But it’s great seeing your code working, with tested PROOF that it works.
So to summarise:
- Create your new repository on Github, activate your new repository in Travis CI , and activate your new repository in Scrutinizer
- Clone https://github.com/delboy1978uk/blank somewhere and delete the .git from it
- Clone https://github.com/you/your-project somewhere-else
- Copy everything from somewhere to somewhere-else (including hidden files)
- Tweak the composrr.json, rename the class file and test file, search and replace refs
- Commit & Push 😀
- Register your project on Packagist!
All that should be doable in 5 minutes, so you can instantly get set up ready to code the real stuff! Try writing your assumptions first in the test file!