nothingworksinc / ticketbeast

Back to the lesson videos:
https://course.testdrivenlaravel.com/lessons
552 stars 11 forks source link

Keeping Things Synchronized with Contract Tests - Extracting a Contract Test #63

Open vpratfr opened 7 years ago

vpratfr commented 7 years ago

You mention 2 options: base class or traits.

According to me, another very valid one would be to create a common test class which would run each methods with a parameter. Then use a provider to switch payment gateways.

class PaymentGatewayContractTest extends TestCase {

    protected function paymentGatewayProvider() {
        return [ new FakePaymentGateway(), new StripePaymentGateway(...) ];
    }

    /**
     * @test
     * @provider paymentGatewayProvider
    function charges_with_a_valid_token($paymentGateway) {
        // Code which will be executed sequentially for each gateway
    }

    // ... More shared tests
}

What are your thoughts about this option?

adamwathan commented 7 years ago

That's an interesting option too; I haven't tried it that way! I think it would work well though, I'll have to give it a shot. Thanks!

vpratfr commented 7 years ago

Usually I use it to cover edge cases in test methods to pass in different sets of parameters. But when testing contracts, it makes it very easy to add implementations to the contract test.

One drawback though is that by using it there, you cannot have any additional provider if you want to make the test input vary.