bigbite / wp-cypress

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

Adds seeder clean routines #70

Closed con322 closed 3 years ago

con322 commented 3 years ago

Note: This seems like it's a duplicate of #61 but is just the sole code to be merged from develop->master, so is being treated as its own PR. — @ampersarnie

Description

Fixes #47 - Adds functionality to use a seeder clean-up routine in tests and the CLI. This allows for an optional method to be added to seeders as clean() - this should be used for any clean/tidy routines to remove any data created by the a seeder. I've also added a wp-cypress seed command to shorten the manual usage which also takes advantage of the clean routines (See usage examples below).

Usage Examples

Command

yarn wp-cypress seed CategoriesSeeder
yarn wp-cypress seed CategoriesSeeder --clean
yarn wp-cypress seed CategoriesSeeder --clean-first

Spec

describe("Categories.", () => {
  before(() => {
    cy.cleanThenSeed("CategoriesSeeder"); // Run the cleaner then seed.
  });

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

  after(() => {
    cy.seedClean("CategoriesSeeder"); // Only clean, no seeding.
  });
});

Seeder

<?php

use WP_Cypress\Seeder\Seeder;

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

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

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

Change Log

Types of changes (if applicable):

Checklist (if applicable):