k4rl1's avatar
Level 1

Integration test instead of unit test

Hi,

let's assume I have a custom collection object. How can I create a unit test (with no dependencies) for the remove method? I have to call the add method first to be able to remove that item afterwards and therefore the remove method has a dependency to the add method. In most cases this custom collection class will have a protected property which includes all added collection items. Therefore I can't mock the add method because then I have no collection items to remove.

class Item 
{
    private $identifier;

    public function __construct($identifier)
    {
        $this->identifier = $identifier;
    }

    public function getIdentifier() { return $this->identifier; }
}

class customCollection
{
    protected $items = [];  
    public function add($item) {
        $this->items[$item->getIdentifier()] = $item;
    }
    public function remove($item) {
        unset($this->items[$item->getIdentifier()]);
    }
}

I could pass an array of collection items to the constructor and use this as the initial collection items of this custom collection object but this could be a problem if the add method possibly modify multiple object properties. So how would you solve this problem? Thanks for feedback!

Karl

0 likes
0 replies

Please or to participate in this conversation.