Page 1 of 1

testing code that uses apc

Posted: Mon Mar 30, 2009 2:43 am
by koen.h
Is there a way to mock apc?

Re: testing code that uses apc

Posted: Mon Mar 30, 2009 4:02 am
by php_east

Re: testing code that uses apc

Posted: Mon Mar 30, 2009 4:25 am
by koen.h
Do you suggest writing it to a file?

The problem is with a class like this:

Code: Select all

 
class ApcAdapter
{
    public function store($key, $var) {
        apc_store($key, $var);
    }
}
 
Actually the problem is maybe more that I don't see a way to design this class so that I can mock out the apc functions.

Re: testing code that uses apc

Posted: Mon Mar 30, 2009 4:29 am
by php_east
ah, when you say mock i thought you meant mimicking apc, if so that would be it. but i see now you meant a class to do the same as apc. how about using sessions to mock its behavior ?

Re: testing code that uses apc

Posted: Mon Mar 30, 2009 4:30 am
by php_east
koen.h wrote:Do you suggest writing it to a file?
actually, if you think about it, it is all finally in a file.

Re: testing code that uses apc

Posted: Mon Mar 30, 2009 4:34 am
by Christopher
Wrap the APC functions in a class and then mock the class. It could use a file or a database.

Re: testing code that uses apc

Posted: Mon Mar 30, 2009 4:47 am
by koen.h
arborint wrote:Wrap the APC functions in a class and then mock the class. It could use a file or a database.
Actually the current class is like an apc wrapper. I would have to create a similare one as the one under testing.

Re: testing code that uses apc

Posted: Mon Mar 30, 2009 6:43 am
by php_east

Re: testing code that uses apc

Posted: Mon Mar 30, 2009 6:48 am
by koen.h
php_east wrote:http://www.nabble.com/Since-APC-is-no-l ... 60650.html

i can't install apc at all. :(
viewtopic.php?f=31&t=97801

I can but my unit tests don't work.

Re: testing code that uses apc

Posted: Mon Mar 30, 2009 8:07 am
by Chris Corbyn
I'd do as arborint says. Create a KeyCache API, or something to that effect:

Code: Select all

interface KeyCache {
  const LIFETIME_DEFAULT = 3600;
  function put($key, $value, $lifetime = self::LIFETIME_DEFAULT);
  function has($key);
  function get($key);
  function clear($key);
}
Then make an APC implementation of this (you could also have MemCache and XCache implementation, or even File and DB..).

Now, your unit tests need not worry about APC, they only need to worry about the KeyCache interface, which is easy to mock. If the cache is provided by some sort of factory (KeyCacheProvider?) then you have the control to decouple the instantiation of a specific keycache in any code that uses it, and therefore have the control to substitute in your Mock.