[TYPO3-dev] Unit tests and method visibility

Oliver Klee typo3-german-02 at oliverklee.de
Sun Jul 5 23:03:37 CEST 2009


Hi,

Francois Suter schrieb:
> I'm trying to design my first unit tests (wow!) and I'm faced with a
> problem. I want to test the result of a method which should logically be
> "protected". But since the test is outside the scope of the class that
> method belongs to, I get a fatal error trying to call it.
> 
> Is there a way to design unit tests for protected (or private) methods?

Testing private methods is discouraged because they are, well, private
and should not be part of the interface visible to the outside.

For protected methods, you can do this (in two separate files):

class tx_foo_Bar {
  /**
   * Roasts a duck.
   *
   * @return tx_foo_Duck a well-roasted duck
   */
  protected function roastADuck() {
    ...
  }
}

and then in tests/fixtures/:

class tx_foo_TestingBar extends tx_foo_Bar {
  /**
   * Roasts a duck.
   *
   * @return tx_foo_Duck a well-roasted duck
   */
  public function roastADuck() {
    return parent::roastADuck();
  }
}

Then in the Bar test case, you use an instance of TestingBar.


Oliver




More information about the TYPO3-dev mailing list