tags:

views:

53

answers:

1

I'm using PHPUnit 3.4.9, but I'm having some problems with the @depends annotation. It works like in the examples, but breaks when the producer reliers on a provider. I don't know if this is meant to work or not, but my code is basically in the form:

<?php
    class StackTest extends PHPUnit_Framework_TestCase {
      /**
       * @dataProvider provider
       */
      public function testEmpty($data) {
        $stack = array();
        $this->assertTrue(empty($stack));

        return $stack;
      }

      /**
       * @depends testEmpty
       */
      public function testPush(array $stack) {
        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertFalse(empty($stack));

        return $stack;
      }

      /**
       * @depends testPush
       */
      public function testPop(array $stack) {
        $this->assertEquals('foo', array_pop($stack));
        $this->assertTrue(empty($stack));
      }

      public function provider () {
        return array(
           // Some testing data here...
        );
      }
    }

The code above is just an example, but shows what my code's structure is like. When ran, it skips the consumer tests, acting as though the producer had failed. I'm expecting that for every set of testing data in the provider, the producer will be run with that data, and all of its consumer correspondingly run.

A: 

Since the question is already 2 days old i give it a shot:

It doesn't seem to work the way you want it to.

@depends just looks if a test with the name provided has run and gets the result. It doesn't even know or care about the @annotations of said test.

I'd guess (haven't dug deep enough into the phpunit source to be 100% sure) tests with @depends are run as "group of tests" internally and not as a single one so there is not test named "testEmpty" and the depends fails.

So to provide a workaround the only thing i can think of right now is to call those "sub tests" directly.

<?php
class StackTest extends PHPUnit_Framework_TestCase {
  /**
   * @dataProvider provider
   */
  public function testEmpty($data) {
    $stack = array();
    $this->assertTrue(empty($stack));
    $this->nextTestOrJustAllInOneTestcaseSaidly($stack);
    return $stack;
  }

  protected function nextTestOrJustAllInOneTestcaseSaidly($data) { ... }

Hope that helps or at least motivates someone else to answer ;)

edorian
Thanks for answering. A little while after posting I worked around it just by combining all the tests into one test, but I was mostly curious if this was expected behavior or not. I'll take yours as the answer either way.
alecRN