testing code that uses apc

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

testing code that uses apc

Post by koen.h »

Is there a way to mock apc?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: testing code that uses apc

Post by php_east »

koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: testing code that uses apc

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: testing code that uses apc

Post 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 ?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: testing code that uses apc

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: testing code that uses apc

Post by Christopher »

Wrap the APC functions in a class and then mock the class. It could use a file or a database.
(#10850)
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: testing code that uses apc

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: testing code that uses apc

Post by php_east »

koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: testing code that uses apc

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: testing code that uses apc

Post 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.
Post Reply