开发者

Unit testing singleton pattern method __clone() in php

开发者 https://www.devze.com 2023-02-14 10:41 出处:网络
I\'m currently working on unit testing of a custom class I made, which is based on the singleton design pattern. Based on the cod开发者_开发知识库e coverage report I have 95.45% of it covered. I am us

I'm currently working on unit testing of a custom class I made, which is based on the singleton design pattern. Based on the cod开发者_开发知识库e coverage report I have 95.45% of it covered. I am using PHPUnit to do the unit testing and I have been through this article by Sebastian Bergmann.

The only problem I am left with is testing against class cloning throught the magic method __clone(). I have set that method as private to avoid instantiation

private final function __clone()
{}

What would be the best way to write a test to make sure that the singleton isn't "clonable". (The same test could eventually be used to test the __constructor())

Not really a question but is it just me or the tests runs awfully slow on a windows box compared to a *nix box?


Keep in mind that code coverage is not a measure of how correct your program is, nor does 100% coverage mean you've executed every code path. For example, the ternary operator

a ? b : c

and compound boolean expressions

if (a < 1 || b > 6)

are counted as single statements even though you may execute only a portion of them due to short-circuiting. Also, omitting the braces around single-statement if, while, etc. blocks turns the whole thing into a single statement.

The following will appear as a single statement in the code coverage report so you can't tell if you've executed both cases (true and false).

if (...)
    foo();

I feel that

private final function __clone() { }

is too simple to fail. Testing that the method throws an exception (using reflection no less which your clients won't do) is testing the PHP interpreter--out of scope in my book.

[For the record, I too get a little OC when it comes to reaching 100% code coverage, but keeping the above facts in mind helps to alleviate it so I can move on to writing better code.]


Call clone or constructor and check if excpetion has been thrown.

0

精彩评论

暂无评论...
验证码 换一张
取 消