PHP + HSQLDB for unit testing - php

Is there a way to run in-memory database such as hsqldb?
I need it for unit testing. In Java there's no problem with it. But, unfortunately, there is a problem with it in PHP. So, is there a way?

You should be able to use this Perl module to access HSQLDB via PHP
https://metacpan.org/pod/DBD::JDBC

Ideally for unit testing you should be mocking your data access in order to test your components in isolation - so you would use mocks and stubs to remove the dependency on the database in your tests.

Related

Can I run phpspec tests (that use the network) without running a apache server

Beginner question here but is it possible to run a PHPSpec test suite that contains tests that make REST calls without actually running a server.
Yes, you can by mocking the service that makes the calls BUT TDD/BDD (and so PHPSpec) are not meant to be used for those kind of test that are integration tests
For those kind of test I would recommend to use something like Behat

Symfony2 functional tests takes hours to finish using PHPUnit

I am currently creating a project using symfony2, i am using PHPUnit vendor to functional test my code.
I am facing a problem that the functional test time increases as the projects gets bigger.
Note: i am using Fixtures to be loaded for every functional test function.
My question here? what is the best practice to implement functional test that run in a reasonable time.
on another hand, Is there any bundle or tool that i can use to speedup this process?
Your question is complex and it is very hard to say exactly problem of delay.
First of all you can divide your fixtures that possible to load before your test suit and other part that can be loaded via setUp method.
2nd advice that can really speedup you tests process is multi-threads run. I use "fastest" lib that allow me to speed up my tests in about 3-4 times (on i7) in compare with one thread run.
https://github.com/liuggio/fastest
find tests/ -name "*Test.php" | ./bin/fastest "bin/phpunit -c app {};"
Just some addition about arguments for unit/functional test. As I see the topic is about "functional" tests and if the tool is PhpUnit, it doesn't mean that tests must be "unit", because it is possible to make functional tests with it too, but I believe that it is not the best choice of the tool for functional tests.
I will recommend you take a look to other a great tool for functional tests like Behat (http://behat.org). It will allow you to not only work with functional tests more comfortable, but use BDD(Behavior Driven Development) approach for your development process
Just small example how we use it for functional testing of shopping cart http://www.youtube.com/watch?v=XIUmDGaaZWM

What is the difference between behat, mink and selenium in php

I am new to testing. All i knew was we PHPunit for testing various functions within class and then i know selenium for browser testing.
I know we can write php to link with selenium web driver to do headless testing of browser.
I am not able to get how does behat and mink come in there. Are these seperate from selenium and they are the alternatives of selenium.
Can i do aweb application tetsing without beaht, mink and only with selenium and php
PHPUnit and Behat are similar, both being testing frameworks. They allow you to test your code, by using different approaches:
PHPUnit tests are based on code you write to check how your classes behave under the required circunstances. A lot of people use this type of framework to practice TDD, but you can of course write tests after the code, or for code written a long time ago.
Behat tests are written in a human readable way, and they are supposed to allow everyone involved in the project to read them. This type of testing is called BDD. You can write tests that explain in (nearly) plain english how your system is supposed to behave.
IMO PHPUnit is more general and is the preferred way of writing most tests. I use Behat for testing my systems general behavior, and PHPUnit for unit testing every class and method independently of others.
On the other side, Mink is a library that allows you to browse programatically, using PHP, and access the contents. It can be used to control in a unified way a lot of browsing systems like Selenium, Zombie, etc. each of them based on different technologies.
You can use Mink outside Behat, but they are usually used together because this way you can write tests that show how a website behaves: Given I enter my credentials in the login form, And press the submit button, I should see my profile page...
And yes, you can use PHPUnit and Selenium together as explained in the docs...

PHPUnit: Notification for untested methods

Does PHPUnit have functionality (or an external manner) to reflect on the target object, and either fail or markTestIncomplete()'ish on methods which it does not have a test for?
To be clear; this would be in a situation where a test suite exists. A developer adds a new method, but neglects to write a test for it. Currently, it does not affect the unit testing process what-so-ever. I am looking for a way to change this.
Thanks in advance.
PHPUnit can log code coverage data into a file with serialized data, which you can analyze later. This way you can find about untested methods:
$ phpunit --coverage-php coveragedata.ser .
PHPUnit 3.7 itself has no way to fail when classes/methods are untested or when code coverage is below a treshold.

Unit Testing and External Resources

I'm kind of new to unit testing, but I've recently seen how it can be quite useful. I've seen that most unit tests are self running. In fact most unit testing frameworks provide a way of running several tests at once (such as unit testing a whole system).
I wonder though; How do you deal with external resources in self running unit tests? I like the idea of testing a whole system and seeing which classes failed, but a class may, for example, create thumbnails from an uploaded image. How would that test be self running when it relies on an upload image? Would I keep a directory of images, and "pretend" to upload one of them in the test?
Any thoughts on the matter would be greatly appreciated.
If you are planning on testing external resources then it would be integration testing. In pure unit testing -> to test external resources, you would have to mock the external resource. So in this case you create a IDirectory interface and then use say a FakeDirectory class and then use FakeDirectory to "Upload" the image. And when you are actually using the application you would pass an actual directory.
In integration testing, you could have a setup class which would do all the work to set up and then you would test.
I have come across this same situation while unit testing my PHP classes. There are functions which can be tested without using any other resources (unit testing), but many functions perform file read/write operations or require database access (integration testing). In order to test these functions, I've combined unit testing with integration testing. In my setUp and tearDown testing classes, it may load a database schema or fetch test data from a local test_data/ directory required by the class functions.
If you need to test what happens with user input, you indeed need some sample data at hand. A directory with images, text files, PDFs or whatever else is needed, should be there along your unit tests. Or you can generate random data programmatically in your tests.
Yes, ideally a class that creates a thumbnail can use a placeholder image that you provide as a resource in your unit test directory. You should be able to test the class in isolation, with as little dependency on the rest of your application as possible. That's kind of what people mean when they recommend to design your code to be "testable."
Mock external dependencies. I have no real experience of mocking in php but I have seen enough resources online just googling for mock and php that it's being done

Categories