Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize values annotation into provider handling #4

Merged
merged 2 commits into from
Jan 21, 2023
Merged

Conversation

thekid
Copy link
Member

@thekid thekid commented Jan 18, 2023

This pull request introduces a generalized Provider annotation, allowing for various implementations

Value-driven tests

#[Values] becomes one of these implementations. Its usage does not change, the following example from our README continues to work!

use test\{Assert, Test, Values};

class CalculatorTest {

  #[Test, Values([[0, 0], [1, 1], [-1, 1]])]
  public function addition($a, $b) {
    Assert::equals($a + $b, (new Calculator())->add($a, $b));
  }
}

Example provider

Implementations must specify a values() method:

use test\Provider;
use lang\reflection\Type;

class StartServer implements Provider {
  private $connection;

  /** Starts a new server */
  public function __construct(string $bind, ?int $port= null) {
    $port??= rand(1024, 65535);
    $this->connection= "Socket({$bind}:{$port})"; // TODO: Actual implementation ;)
  }

  /**
   * Returns values
   *
   * @param  Type $type
   * @param  ?object $instance
   * @return iterable
   */
  public function values($type, $instance= null) {
    return [$this->connection];
  }
}

Used on test case

Provider values are passed as method argument, just like #[Values], the previous only implementation.

use test\{Assert, Test};

class ServerTest {

  #[Test, StartServer('0.0.0.0', 8080)]
  public function connect($connection) {
    Assert::equals('Socket(0.0.0.0:8080)', $connection);
  }
}

Used on test group

Provider values are passed to the constructor and the connection can be used for all test cases. Note: The provider's values() method is invoked with $instance= null!

use test\{Assert, Test};

#[StartServer('0.0.0.0', 8080)]
class ServerTest {

  public function __construct(private $connection) { }

  #[Test]
  public function connect() {
    Assert::equals('Socket(0.0.0.0:8080)', $this->connection);
  }
}

This opens the door for having providers such as `#[Files]` or `#[Server]`
which also just pass arguments to a method / the type instantiation
@thekid thekid added the enhancement New feature or request label Jan 21, 2023
@thekid thekid merged commit 1beaed7 into main Jan 21, 2023
@thekid thekid deleted the feature/provider branch January 21, 2023 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant