I would like to get some feedback on what is one of my first PHPUnit test cases.
The subject of the tests is a simple configuration class in a PHP app. It is used to create, store and retrieve configuration settings.
The settings are stored in an array within the class. Each setting has the following properties:
key (for example
frontend:menu:bgcolor
)type (one of
string
,int
,hexcolor
,boolean
)value (mixed)
read only (boolean)
Every setting has to be created first. On creation, each setting is assigned a type, the read only flag (defaults to false), and a default value.
Class methods:
public function create($key, $value, $type = "string", $read_only = false)
public function set($key, $value)
public function get($key)
The tests I am running:
(ok) and (fail) are the expected results of the test.
Create a setting named
test
with typestring
with an initial valuexyz
(ok)Load the setting
test
and expect return valuexyz
(ok)Set the setting
test
toabc
(ok)Load the setting
test
and expect return valueabc
(ok)Load the setting
töst
(fail, key doesn't exist)Create a setting named
test_read_only
with typestring
, an initial valueMary had a little lamb
and the read-only flag set to true (ok)Set the setting named
test_read_only
toPeter had a big lamb
(fail, key is read only)Load the setting named
test_read_only
and expect return valueMary had a little lamb
(ok)Create a setting named
test_integer
with typeint
, an initial value5
and the read-only flag set to true (ok)Create a setting named
test_integer_second
with typeint
, and an initial value5.23049505906
(fail, initial value has wrong type)Create a setting named
test_weird
with typesdasfäödsf#ädfsaö
(fail, type not found)Set the setting named
test_weird
toPeter had a little lamb
(fail, key not defined)
Questions:
- Does this test case make sense to you?
- Would you consider this a complete test case?
- What would you do differently, or in addition?
Update: Suggestions that have come up so far that I will implement:
- String length checks
- Tests involving the remaining data types
boolean
andhexcolor
- Key naming tests are not necessary right now but will be in the future.