bigbite / wp-cypress

WordPress end to end testing with Cypress.io.
MIT License
86 stars 19 forks source link

Easy command to run Seeders #47

Closed jasonagnew closed 3 years ago

jasonagnew commented 3 years ago

Is your feature request related to a problem? Please describe. It's not always clear to users of WP Cypress how to easily test Seeders while developing them. I would typically

yarn wp-cypress wp "seed {SeederName}"

The other issue is having teardown for Seeder would be useful, so its possible to keep re-running it.

Describe the solution you'd like Provide a direct command

yarn wp-cypress seed {SeederName}

Provide teardown/down() method on seeders that would be run before the Seeder is called with this command.

Describe alternatives you've considered While working on seeder it is easy enough to create your own down method and have it called before your Seeder and remove it once done.

Additional context Although not a big issue it's more about giving less familiar people more guidance.

ampersarnie commented 3 years ago

@jasonagnew There has been a couple of things that I've found I wanted to add to this recently and this issue is one. I'm working on an addition that would help with this. Executing a teardown before seeding is the not correct place for that action, as it's supposed to be used as the last execution as part of the clean up routine, I've got something which should work for your needs though.

So I have an early version of a tear_down() method that can be added to seeders as well as a cy.tearDown() function to call that within the *.spec.js tests. I'm also extending the seeding command to have the tear-down flag to run the seeder tear down.

I'll get a PR in as soon as I'm finished testing and cleaning up.

Some examples:

Command

yarn wp-cypress wp "seed CategoriesSeeder --tear-down"

Spec

describe("Categories.", () => {
  before(() => {
    cy.seed("CategoriesSeeder");
  });

  it("I can add a new category.", () => {
    cy.visitAdmin();
    cy.visit("/wp-admin/edit-tags.php?taxonomy=category");
    // ...
  });

  after(() => {
    cy.tearDown("CategoriesSeeder");
  });
});

Seeder

<?php

use WP_Cypress\Seeder\Seeder;
use WP_Cypress\Fixtures;

class CategoriesSeeder extends Seeder {
    const CATEGORIES = [
        'Test',
        'New Test',
    ];

    public function run() {
        foreach( self::CATEGORIES as $category ) {
            wp_insert_term( $category, 'category' );
        }
    }

    public function tear_down() {
        foreach( self::CATEGORIES as $category ) {
            $term = get_term_by( 'name', $category, 'category' );
            wp_delete_term( $term->term_id, 'category' );
        }
    }
}