Skip to content

v0.6.0

Compare
Choose a tag to compare
@liamdefty liamdefty released this 08 Jul 08:24
· 197 commits to master since this release
3fe0504

Change Log

Changed

  • Add the ability to be authenticated with different users #17
  • Add ability to have cypress in a non default location by passing seedsPath as a config option #36
  • Improvements to the seeder #34
    • Add the ability to be able to call seeders within seeders, in a specific order.
    • Add the ability for wp cypress to call it's own seeders that aren't user generated.
  • Various improvements under the hood to php code quality.

Breaking Changes

  • Default Seeder: Previously named, Init, the Default Seeder will be the seeder that is always ran on reset. It has been renamed to make it easier to understand and to give it more context.

  • Fixtures: The most notable changes in this release come in the seeder with the introduction of fixtures. Fixtures replace the generator function instantiated with the seeder which is now deprecated. Fixtures are a structured way to create data when seeding. There are some default fixtures for a post, comment and user and will create more in the future and upcoming releases. However, you can create your own fixtures too. Before to create a post using the generate function it looked something like:

<?php

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

class DefaultSeeder extends Seeder {
	public function run() {
		$this->generate->posts( [], 3 );
	}
}

Instead, using Fixtures, it looks like:

<?php

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

class DefaultSeeder extends Seeder {
	public function run() {
		$post = new Fixtures\Post( [] );

                $post->create( 3 );
	}
}

To create your own fixture simply create a file in your seeds directory with the name of the fixture. For example ExampleCommentFixture.php. Then extend the Fixture class and create two methods defaults and generate. Defaults the default properties to be generated and the generate function performs the required logic that is ran on create to create your data in the database:

<?php

use  WP_Cypress\Fixtures\Fixture;
use  WP_Cypress\Utils;

class ExampleCommentFixture extends Fixture {
	public function defaults() {
		return [
			'comment_post_ID'      => 1,
			'comment_author'       => 'admin',
			'comment_author_email' => '[email protected]',
			'comment_content'      => 'This is an example comment fixture',
			'user_id'              => 1,
			'comment_date'         => Utils\now(),
		];
	}

	public function generate() {
		$id = (int) wp_insert_comment( array_merge( $this->defaults(), $this->properties ) );
	}
}

This will be autoloaded and can be executed in any seeder:

<?php

use WP_Cypress\Seeder\Seeder;

class ExampleSeeder extends Seeder {
	public function run() {
		( new Fixtures\Post([
			'import_id'  => 10,
			'post_title' => 'Post with Custom Comments',
		]) )->create();

		( new ExampleCommentFixture( [ 'comment_post_ID' => 10 ] ) )->create( 10 );
	}
}