title: Yii2 and Codeception API test with _before
body: <p>I'm using Yii2 as my app framework, I started with the advanced template. I have a frontend, backend and api entry point for my app. I have moved frontend, backend and api out of the root directory and into /modules to keep clutter down in the root.</p>
  
  <p>I am trying to create a registration test for my API registration endpoint. Before I run the test I need to ensure that the email address i'm testing registration with has not already been registered in the test database. </p>
  
  <p>I'm using the Cest _before method to see if I can find a Member model with the test email, if I find one I then delete it. </p>
  
  <p>The problem is that this is causing the test to exit without any debugging information.</p>
  
  <p>Is this how I should be doing my test setup? Should I be doing something else to clear any old test users.</p>
  
  <p>The test will work the first time I run it if I remove the _before() setup functionality.</p>
  
  <p><strong>AuthenticationCest.php</strong></p>
  
  <pre><code> namespace tests\codeception\api\api;
  
  use common\models\Member;
  use tests\codeception\api\ApiTester;
  
  class AuthenticationCest {
  
      public $email = "reg.test@member.com";
  
      //Check to see id the email is already used and delete it
      public function _before(ApiTester $I) {
          $member = Member::findOne([
              "email" =&gt; $this-&gt;email
          ]);
  
          if ($member) {
              $member-&gt;delete();
          }
      }
  
      //Test that a member can register
      public function registerTest(ApiTester $I) {
          $I-&gt;wantTo('Register a new member via the API');
  
          $I-&gt;haveHttpHeader('Content-Type', 'application/json');
  
          $I-&gt;sendPOST('token', [
              'grant_type'    =&gt; 'registration',
              'client_id'     =&gt; 'testclient',
              'client_secret' =&gt; 'testpass',
              'email'         =&gt; $this-&gt;email,
              'password'      =&gt; 'Test1234',
              'given_name'    =&gt; 'Test',
              'family_name'   =&gt; 'Member'
          ]);
  
          $I-&gt;seeResponseCodeIs(200);
          $I-&gt;seeResponseIsJson();
      }
  }
  </code></pre>
  
  <p><strong>_bootstrap.php</strong></p>
  
  <pre><code>defined('YII_DEBUG') or define('YII_DEBUG', true);
  defined('YII_ENV') or define('YII_ENV', 'test');
  
  defined('YII_APP_BASE_PATH') or define('YII_APP_BASE_PATH', dirname(dirname(dirname(__DIR__))));
  
  require_once(YII_APP_BASE_PATH . '/vendor/autoload.php');
  require_once(YII_APP_BASE_PATH . '/vendor/yiisoft/yii2/Yii.php');
  require_once(YII_APP_BASE_PATH . '/common/config/bootstrap.php');
  require_once(YII_APP_BASE_PATH . '/modules/api/config/bootstrap.php');
  
  Yii::setAlias('@tests', dirname(dirname(__DIR__)));
  </code></pre>
  
  <p>This seems to be enough to let me create and delete models for Unit test, but not the functional API tests</p>
  
  <p><strong>api.suite.yml</strong></p>
  
  <pre><code>class_name: ApiTester
  modules:
  enabled: [PhpBrowser, REST]
  config:
    PhpBrowser:
        url: http://api.yii.lan/
    REST:
        url: http://api.yii.lan/v1/
  </code></pre>
  
  <p><strong>EDIT output</strong></p>
  
  <p><code>$ ../vendor/bin/codecept run --steps --debug -vvv</code></p>
  
  <pre><code>Codeception PHP Testing Framework v2.0.13
  Powered by PHPUnit 4.6.6 by Sebastian Bergmann and contributors.
  
  [tests\codeception\common]: tests from /workspace/yii2/tests//codeception/common
  
  
  Tests\codeception\api.api Tests (3) ------------------------------------------------------------------------
  Modules: PhpBrowser, REST, Db
  ------------------------------------------------------------------------------------------------------------------------------------
  Register via the API (tests\codeception\api\api\oauth\RegistrationCest::registerTest)
  Scenario:
  </code></pre>