dor2p0520 2013-07-09 17:14
浏览 73

如何使用PHPUnit测试集合类

I have a class similar to this (some logic removed for brevity):

class FooCollection {
    protected $_foos;

    public function __construct() {
        $this->_foos = new SplObjectStorage();
    }

    public function addFoo(FooInterface $foo) {
        $this->_foos->attach($foo);
    }

    public function removeFoo(FooInterface $foo) {
        $this->_foos->detach($foo);
    }
}

I'd like to test the addFoo() and removeFoo() methods using PHPUnit, and I was wondering what the best strategy would be to do this? As far as I can tell, I have only a few options:

  1. Add a method hasFoo(FooInterface $foo) and check this after the add.
  2. Add a method getFoos() that directly returns the SplObjectStorage instance and check if $foo is in it after the add.
  3. Try to removeFoo($foo) after addFoo($foo) and check for an exception.
  4. Make $_foos a public property and check it directly after the add (bad, bad, bad...).

Options #1 and #2 are changing the public interface solely for testing purposes, and I'm not sure how I feel about that. They seem like pretty generic, useful methods to have anyway on the surface, but in my particular case, I never have a need to check for the existance of a particular Foo instance in the collection, nor retrieve all of the instances, so it really would just be bloat. Also, it seems like if I'm testing multiple parts of the interface in one test, I'm not really testing a "unit," but that's more or less just a philosophical hangup.

Option #3 seems awkward to me.

Option #4 is a really bad idea, and I shouldn't have even listed it, as I wouldn't do it even if it were suggested here.

  • 写回答

2条回答 默认 最新

  • duanchuiwen6694 2013-07-09 17:29
    关注

    You didn't post a public accessor for getting the collection, but I am sure you have one, otherwise adding/removing foos to an array which is publically inaccessible makes no sense. So you could try something like (phpunit 3.6, php 5.4):

    public function setUp()
    {
        $this->NumbersCollection = new NumbersCollection;
    }
    
    public function tearDown()
    {
        unset($this->NumbersCollection);
    }
    
    public function testNumbersCollection()
    {
        $this->NumbersCollection->addNumber(1);
        $this->NumbersCollection->addNumber(2);
    
        $this->assertSame(3, $this->NumbersCollection->sum());
        $this->assertSame(2, $this->NumbersCollection->product());
    
        $this->NumbersCollection->removeNumber(1);
    
        $this->NumbersCollection->addNumber(7);
    
        $this->assertSame(9, $this->NumbersCollection->sum());
        $this->assertSame(14, $this->NumbersCollection->product());
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用