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

Create a base test class that extends TripalTestCase #135

Open
almasaeed2010 opened this issue Jan 15, 2019 · 7 comments
Open

Create a base test class that extends TripalTestCase #135

almasaeed2010 opened this issue Jan 15, 2019 · 7 comments

Comments

@almasaeed2010
Copy link
Contributor

By creating a TestCase class in the tests folder that extends the TripalTestCase, we can allow users to add custom methods that all tests can inherit. Here is an example:

class TestCase extends TripalTestCase {

  /**
   * Create a new user.
   *
   * @param string $role
   *     The role (one of 'authenticated user' or 'administrator').
   *
   * @return \stdClass
   *     The created user object.
   */
  public function createUser($role = 'authenticated user') {
    $faker = Factory::create();
    UsersTableSeeder::$email = $faker->email;
    UsersTableSeeder::$role = $role;
    return (new UsersTableSeeder())->up();
  }
}

This class allows all tests to create custom and random user accounts.

The tests themselves can then utilize this by simply extending TestCase instead of TripalTestCase:

class AdminFormsTest extends TestCase {

  // Uncomment to auto start and rollback db transactions per test method.
  use DBTransaction;

  /** @test */
  public function testThatAdminDashboardFormIsAccessibleToAdmins() {
    $this->actingAs(1);

    $urls = [
      'tripal_hq/admin',
      'tripal_hq/admin/pending',
      'tripal_hq/admin/approved',
      'tripal_hq/admin/all',
    ];

    foreach ($urls as $url) {
      $this->get($url)->assertStatus(200);
    }
  }
}
@bradfordcondon
Copy link
Member

bradfordcondon commented Jan 16, 2019

i think i understand this.

the TestCase class is a stub that gets installed and bootstrapped. So when new Tests are created, you extend TestCase instead of TripalTestCase so you can have methods available in all your tests.

Is that accurate? Seems like a good idea.

The naming seems off. (test case vs tripal test case)

what does make test do by default, extend TestCase or TripalTestCase?

@almasaeed2010
Copy link
Contributor Author

I think an example will clarify. See this PR: statonlab/tripal_hq#94
It introduces a new class in tests/TestCase.php. This new class extends TripalTestCase. If you take a look in that class, you see new method called createUser() which is a custom method for tripal_hq specifically to create random test users. Then, all of the actual test classes such as AdminFormsTest.php extend TestCase rather than extending TripalTestCase directly. The result is that all of the tests that are specific to tripal_hq share any methods in TestCase.php (like createUser()).

For completeness, the TestCase.php class is auto loaded in composer.json using psr-4 standards.

Does that help at all?

@bradfordcondon
Copy link
Member

bradfordcondon commented Jan 16, 2019

Your explanation is in-line with what I was thinking above.

My two concerns still stand.

  • Naming seems weird. I can extend testCase or TripalTestCase, with testCase . allowing me to customize shared helper methods. Is there some more intuitive naming we can use?

  • what does make test do? create a TestCase or a TripalTestCase? Should we just do away with TripalTestCase and advise users to use TestCase instead?

@almasaeed2010
Copy link
Contributor Author

  • You'll always extend TestCase
  • init will create the empty TestCase class
  • make:test will create a test that extends TestCase

Therefore, there will be no more mentions of TripalTestCase except in the new TestCase class. In other words, the user shouldn't care about anything but TestCase.

@bradfordcondon
Copy link
Member

ok thanks for clarifying that point, sounds good to me. We should probably make a note in the docs about old tests extending tripalTestCase instead of TestCase.

@almasaeed2010
Copy link
Contributor Author

Sure!

We have to make this a 2.0 feature otherwise the make:test will be breaking change.

@almasaeed2010
Copy link
Contributor Author

OR we can make make:test check if TestCase doesn't exist and create it (+ add a note in the docs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants