laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

custom extensions when testing #1158

Open agoalofalife opened 6 years ago

agoalofalife commented 6 years ago

Method setUpTraits return uses array. https://github.com/laravel/framework/blob/805f67b1f36c1aa747138c81067cb6d56ef21ed5/src/Illuminate/Foundation/Testing/TestCase.php#L71

I want to create a (custom)trait for the extension. It would be convenient to do so..

$this->uses = $this->setUpTraits();
// custom trait
trait Custom
{
public function customMethod()
{
 // ....
}
}
abstract class TestCase extends BaseTestCase
{
 protected function setUp()
 {
   parent::setUp();

   if (isset($uses[Custom::class])) {
            $this->customMethod();
        }
 }
}

In the current version you need to override the method setUp or to violate the principle DRY @taylorotwell What do you think about this?

mfn commented 6 years ago

It would be convenient to do so..

You just override setupTraits and get the return value from this method and do your own magic. That is the reason the array is actually returned.

Example (from my apps Testcase):

    protected function setUpTraits(): array
    {
        $uses = parent::setUpTraits();

        if (isset($uses[ElasticSearchTrait::class])) {
            $this->elasticsearchSetUp();
        }
…

Or did I misunderstand you?