PHPUnit - using assertions in external object - php

I'm working on a tool exporting tests as PHP/PHPUnit and I'm facing a small problem. To explain briefly, the test scripts only contain calls to an actionwords object, which contains all the logic of the test (in order to factors various steps from different test scenario).
An example might be clearer:
require_once('Actionwords.php');
class CoffeeMachineHiptestPublisherSampleTest extends PHPUnit_Framework_TestCase {
public $actionwords = new Actionwords();
public function simpleUse() {
$this->actionwords->iStartTheCoffeeMachine();
$this->actionwords->iTakeACoffee();
$this->actionwords->coffeeShouldBeServed();
}
}
Here in the coffeeShouldBeServed method, I need to run an assertion, but that's not possible as the Actionwords class does not extend PHPUnit_Framework_TestCase (and I'm not sure it should, it's not a test case, just a set of helpers).
Currently the solution I found is to pass the tests object to the action words and use the reference for the assertions, something like that.
class CoffeeMachineHiptestPublisherSampleTest extends PHPUnit_Framework_TestCase {
public $actionwords;
public function setUp() {
$this->actionwords = new Actionwords($this);
}
}
class Actionwords {
var $tests;
function __construct($tests) {
$this->tests = $tests;
}
public function coffeeShouldBeServed() {
$this->tests->assertTrue($this->sut->coffeeServed);
}
}
It works fine but I don't find it really elegant. I'm not a PHP developer so there might be some better solutions that feel more "php-ish".
Thanks in advance,
Vincent

The assertion methods are static so you can use PHPUnit_Framework_Assert::assertEquals(), for instance.

Related

How to run exactly the same phpunit tests against different implementations of SUT?

I can't wrap my head around this testing issue.
I wrote a test for testing repository which uses In-memory implementation like:
class RepositoryTest extends TestCase {
function setUp() {
// set implementation in the container
container()->set(Repository::class, InMemoryRepository::class);
}
function test_it_can_save() {...}
function test_it_can_delete() {...}
function test_it_can_query() {...}
}
Then I added another implementation for this repository. Let's say it is SQLRepository.
I need to run exactly the same set of tests against my new implementation.
I'd like to just set another context for the same tests.
How do I do that?
Ok, I think I know what to do.
I just need to extend my initial test and reload setUp method like this:
class SQLRepositoryTest extends RepositoryTest {
function setUp() {
// set another implementation in the container
container()->set(Repository::class, SQLRepository::class);
}
}

PHPUnit Mocks vs PHP parameter constrains

I'm not sure if it's the proper place for such question since it's rather theoretical than the specific code sample but I'll ask anyway.
So, at some point PHP introduced type constrains in function definition (except basic types of course), i.e.
class A {
public $value;
}
function foo($someInt, A $a) {...}
What make me wondering is if PHPUnit mocks can be used in such situation:
class functionTest extends PHPUnit_Framework_TestCase {
public function testFoo() {
$mockA = $this->getMockBuilder('A')->getMock();
$this->assertEquals('some result', foo(1, $mockA));
}
}
Would such call be accepted when the test runs (ofc. I skipped includes and stuff to keep it simple).
And the more interesting question: if yes, then how is it implemented?
Yes it will be working, PHPUnit will mock your object. This mocked object will dynamically extends the base Object you want to mock.

Phpunit does not execute effective parent test case?

I'm currently testing a wrapper to an API with PHPUnit (CLI).
Due to the nature of the tests, I can pretty much use the same code for testing two different use cases. The only difference is in the values I send to the API as parameters.
So, I decided to write a class DefaultTest, where I test the API using default values used by the API and a second CustomTest(Case) where I test my parameter container with differing values. CustomTest inherites from DefaultTest, as all the functions used for validating the returned data are equivalent in both cases.
Here's some code for your understanding:
class DefaultTest extends PHPUnit_Framework_TestCase {
public function testAPIMethod()
{
$this->checkParameterContainer();
$this->validateResults();
}
public function checkParameterContainer()
{
/* Set up default parameter container */
}
public function validateResults()
{
/* Validate the results */
}
}
class CustomTest extends DefaultTest {
public function checkParameterContainer()
{
/* Set up custom parameter container */
}
public function validateResults()
{
parent::validateResult();
}
}
PHPUnit takes the child class, executes the testAPIMethod, leading to CustomTest::checkParameterContainer() and DefaultTest::validateResults() being executed.
But DefaultTest's testAPIMethod is never executed, as DefaultTest::checkParameterContainer() is never called.
Both classes are fully valid TestCases and DefaultTest is executed normally when not specialized.
So, my question for you guys: Why is that? Do I miss something here? Is this by design?
In case, somebody needs it: PHPUnit uses reflection to find test methods on each class, but will not search in parent classes. So the following will be needed in the child test class:
public function testAPIMethod()
{
parent::testAPIMethod();
}

PHP extending a class

I have a question about extending a class in PHP.
The examples I see on the php site just have 1 line of code in the method...is it the same if the method has tons of code??
If this is the base class:
class BaseClass {
public function WithWayTooMuchCode {
// like 100 lines of code here
}
}
Then do I have to copy all the code over if I want to use the same method, but change only 1 or 2 things?
class MyOwnClass extends BaseClass {
public function WithWayTooMuchCode {
// like 100 lines of code here
// do I have to copy all of the other code and then add my code??
}
}
This seems a little not DRY to me...
Yes, unless those 1 or 2 things happen to be at the beginning or the end. You can call the parent function via
parent::WithWayTooMuchCode();
Which you can place anywhere in the child/overridden method.
If it doesn't feel DRY, consider splitting the function into smaller methods.
do I have to copy all the code over if I want to use the same method,
but change only 1 or 2 things?
No, you don't have to copy all of the code, assuming you're adding to the function and not removing pieces of it.
so it follows:
class BaseClass {
public function WithWayTooMuchCode {
// like 100 lines of code here
}
}
class MyOwnClass extends BaseClass {
public function WithWayTooMuchCode {
parent::WithWayTooMuchCode();
//additionally, do something else
}
}
$moc = new MyOwnClass();
$moc->WithWayTooMuchCode();
You can use the parent::WithWayTooMuchCode() wich will execute the parent method and after you can add your code. It will look like this :
class MyOwnClass extends BaseClass {
public function WithWayTooMuchCode {
parent::WithWayTooMuchCode()
// do I have to copy all of the other code and then add my code??
}
}
You can write down a separate function for that things that you want to do separately in parent class and then Call then in you way.
In other words, separate out the things you need to do separately ans create a function for them. And call them separately in child class. The final function in child class will call parent class function as well those separate functions.
You have a few choices. Assuming;
class BaseClass {
public function WithWayTooMuchCode {
// like 100 lines of code here
}
}
You can do
class MyOwnClass extends BaseClass {
public function AnotherFunction() {
// put other code here
}
}
This allows you to do MyOwnClass->AnotherFunction() and MyOwnClass->WithWayTooMuchCode()
or you could do
class MyOwnClass extends BaseClass {
public function WithWayTooMuchCode() {
// put new code here
}
}
which will allow you to run MyOwnClass->WithWayTooMuchCode() and will ONLY run the "new code", not the "100 lines".
Finally you could do
class MyOwnClass extends BaseClass {
public function WithWayTooMuchCode() {
parent::WithWayTooMuchCode();
// Do more processing
}
}
which will allow you to run MyOwnClass->WithWayTooMuchCode() will run the "100 lines of code" AND the new code. You can put the parent before/after/during your new code, so you can tailor it as required
In addition to the other answers I should point out that this problem can be addressed by events. They are a way to determine points in a class where you can add your own functionality from outside. If you have control over the codebase and the time/inclination you might want to consider implementing this functionality. Unfortunately PHP doesn't support them directly in the way that, say, C# does so you'd have to do the work.
If you only have this issue in a single place I doubt you should bother, but if it's becoming a pattern you might want to consider this approach.

php OOP function declarations

I'm a big fan of OOP in php, but i feel like defining class methods gets disorganized so fast. I have a pretty good background in OOP in C++, and i am pretty comfortable with how it is handled there, and am curious if there are ways to do it similarly in php.
To be more specific, here is what i mean. I like how in C++ you can define a class header (myclass.h) and then define the actual details of the functions in the implementation file (myclass.cc). Ive found that this can easily be replicated using interfaces in php, but i havent found a good solution for the following:
I like to organize my code in C++ in different files based on how they are accessed, so for example, public methods that can be called outside of the class would be in 1 place, and private methods would be organized somewhere else - this is personal preference.
Ive tried to define class methods in php like:
private function MyPHPClass::myFunction(){ }
when the definition isnt directly inside the class block( { } ), but i havent had any success doing this.
Ive been through all of the pages on php.net, but couldnt find anything like this. Im assuming that there is no support for something like this, but thought i would ask anyway.
thanks
You can't do this. The class declarations are Java-like.
You have to put everything in one file or, at minimum, have some helper classes -- be they only static methods to which you forward or calls or with you deferring some implementation to encapsulated objects. You can also use the __call and __callstatic magic methods to reduce the size of your stubs, but that only works for public methods and I would recommend that you avoid magic methods.
EDI2: As RobertPitt pointed in a comment, you should consider alternative strategies that divide the functionality between several classes. It has the added advantage that it can make your code more decoupled. If you need, you can still unify the functionality of the several classes behind a façade.
EDIT: By using magic methods, I mean something like this:
class MyClassHelper {
public function method_one(array $args) {
...
}
public function method_two(array $args) {
...
}
}
class MyClass {
/**
* #var MyClassHelper
*/
private $helper;
private static $ALLOWED_METHODS = array("method_one" => NULL,
"method_two" => NULL);
public function __call($name, $arguments) {
$name = strtolower($name);
if (array_key_exists($name, self::$ALLOWED_METHODS) {
$helper->$name($arguments);
}
else
throw new Exception(...);
}
}
I should recommend that you avoid this, there are many pitfalls to this (handling of references, no code completion).
Im not really a C++ / C# Programmer but interfaces in php i can give you an exampe to see if this helps.
Interface
interface IDatabase
{
public function connect($dns = '');
public function disconnect($flushCache = false); //Do not use braces, Code free in interfaces
}
Database abstract base class
abstract class Database
{
//Some driver logic here to laod mysql,oracle etc
}
MySql Driver
class DBDriver_MySQl extends Database implements IDatabase
{
public function connect($dns = '')
{
//Connection logic.
}
public function disconnect($flushDns)
{
//Disconnect Login
}
}
Hope this is what your looking for.

Categories