diff --git a/src/PostType.php b/src/PostType.php index e678c4d..1ceba99 100644 --- a/src/PostType.php +++ b/src/PostType.php @@ -4,7 +4,7 @@ use PostTypes\Columns; use PostTypes\Contracts\PostTypeContract; -use PostTypes\Registrars\PostTypeRegistrar; +use PostTypes\Registration\Service; abstract class PostType implements PostTypeContract { @@ -110,12 +110,12 @@ public function hooks(): void } /** - * Register the post type. + * Register the PostType. * * @return void */ public function register(): void { - (new PostTypeRegistrar($this))->register(); + Service::forPostType()->register($this); } } diff --git a/src/Registrars/PostTypeRegistrar.php b/src/Registrars/PostTypeRegistrar.php deleted file mode 100644 index 2a5703b..0000000 --- a/src/Registrars/PostTypeRegistrar.php +++ /dev/null @@ -1,261 +0,0 @@ -posttype = $posttype; - } - - /** - * Register the PostType to WordPress. - * - * @return void - */ - public function register() - { - $name = $this->posttype->name(); - - // Initialize the post type. - add_action('init', [$this, 'createColumns'], 10, 0); - add_action('init', [$this, 'initialize'], 10, 0); - - // Handle PostType filters. - add_action('restrict_manage_posts', [$this, 'modifyFilters'], 10, 1); - - // Handle PostType columns. - add_filter('manage_' . $name . '_posts_columns', [$this, 'modifyColumns'], 10, 1); - add_action('manage_' . $name . '_posts_custom_column', [$this, 'populateColumns'], 10, 2); - add_filter('manage_edit-' . $name . '_sortable_columns', [$this, 'setSortableColumns'], 10, 1); - add_action('pre_get_posts', [$this, 'sortSortableColumns'], 10, 1); - - // Register custom hooks. - $this->posttype->hooks(); - } - - /** - * Create Columns. - * - * @return void - */ - public function createColumns() - { - $this->columns = $this->posttype->columns(new Columns()); - } - - /** - * Register Post Type. - * - * @return void - */ - public function initialize() - { - // Modify the existing PostType if it exists. - if (post_type_exists($this->posttype->name())) { - add_filter('register_post_type_args', [$this, 'modifyPostType'], 10, 2); - - return; - } - - // Register the new PostType to WordPress. - register_post_type($this->posttype->name(), $this->generateOptions()); - } - - /** - * Modify the existing PostType. - * - * @param array $args - * @param string $posttype - * @return array - */ - public function modifyPostType(array $args, string $posttype) - { - if ($posttype !== $this->posttype->name()) { - return $args; - } - - return array_replace_recursive($args, $this->generateOptions()); - } - - /** - * Generate the options for the PostType. - * - * @return array - */ - public function generateOptions() - { - $defaults = [ - 'public' => true, - 'show_in_rest' => true, - 'labels' => $this->posttype->labels(), - 'taxonomies' => $this->posttype->taxonomies(), - 'supports' => $this->posttype->supports(), - 'menu_icon' => $this->posttype->icon(), - 'rewrite' => [ - 'slug' => $this->posttype->slug(), - ], - ]; - - return array_replace_recursive($defaults, $this->posttype->options()); - } - - /** - * Modify the PostType filters. - * - * @param string $posttype - * @return void - */ - public function modifyFilters($posttype) - { - if ($posttype !== $this->posttype->name()) { - return; - } - - foreach ($this->posttype->filters() as $taxonomy) { - if (!is_object_in_taxonomy($posttype, $taxonomy)) { - continue; - } - - $query_var = get_taxonomy($taxonomy)->query_var; - $selected = isset($_GET[$query_var]) ? $_GET[$query_var] : ''; - - $options = [ - 'name' => $query_var, //$taxonomy, - 'value_field' => 'slug', - 'taxonomy' => $taxonomy, - 'show_option_all' => get_taxonomy($taxonomy)->labels->all_items, - 'hierarchical' => get_taxonomy($taxonomy)->hierarchical, - 'hide_empty' => 0, - 'show_count' => 0, - 'orderby' => 'name', - 'selected' => $selected, //isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '', - ]; - - echo ''; - - wp_dropdown_categories($options); - } - } - - /** - * Modify the PostType columns. - * - * @param array $columns - * @return array - */ - public function modifyColumns(array $columns) - { - foreach ($this->columns->getColumns() as $key => $label) { - $columns[$key] = $label; - } - - if ($remove = $this->columns->getRemoved()) { - $columns = array_diff_key($columns, array_flip($remove)); - } - - if ($only = $this->columns->getOnly()) { - $columns = array_intersect_key($columns, array_flip($only)); - } - - foreach ($this->columns->getPositions() as $key => $position) { - [$direction, $reference] = $position; - - if (!isset($direction) || !isset($reference)) { - continue; - } - - $new = []; - - foreach ($columns as $k => $label) { - if ('before' === $direction && $k === $reference) { - $new[$key] = $columns[$key]; - } - - $new[$k] = $label; - - if ('after' === $direction && $k === $reference) { - $new[$key] = $columns[$key]; - } - } - - $columns = $new; - } - - return $columns; - } - - /** - * Populate the PostType columns. - * - * @param string $column - * @param int $post_id - * @return void - */ - public function populateColumns($column, $post_id) - { - $callback = $this->columns->getPopulateCallback($column); - - if ($callback) { - call_user_func_array($callback, [$post_id]); - } - } - - /** - * Set the PostTypes sortable columns. - * - * @param array $columns - * @return array - */ - public function setSortableColumns($columns) - { - $sortable = $this->columns->getSortableColumns(); - - return array_merge($columns, $sortable); - } - - /** - * Sort PostType columns. - * - * @param \WP_Query $query - * @return void - */ - public function sortSortableColumns($query) - { - if (!is_admin() || !$query->is_main_query()) { - return; - } - - $column = $query->get('orderby'); - $callback = $this->columns->getSortCallback($column); - - if ($callback) { - call_user_func_array($callback, [$query]); - } - } -} diff --git a/src/Registration/Integrations/ManagePostTypeColumns.php b/src/Registration/Integrations/ManagePostTypeColumns.php new file mode 100644 index 0000000..2764168 --- /dev/null +++ b/src/Registration/Integrations/ManagePostTypeColumns.php @@ -0,0 +1,156 @@ +posttype = $posttype; + } + + /** + * Add hooks. + * + * @return void + */ + public function register(): void + { + $name = $this->posttype->name(); + + add_action('init', [$this, 'createColumns'], 10, 0); + add_filter('manage_' . $name . '_posts_columns', [$this, 'modifyColumns'], 10, 1); + add_action('manage_' . $name . '_posts_custom_column', [$this, 'populateColumns'], 10, 2); + add_filter('manage_edit-' . $name . '_sortable_columns', [$this, 'setSortableColumns'], 10, 1); + add_action('pre_get_posts', [$this, 'sortSortableColumns'], 10, 1); + } + + /** + * Create Columns. + * + * @return void + */ + public function createColumns(): void + { + $this->columns = $this->posttype->columns(new Columns()); + } + + /** + * Modify the PostType columns. + * + * @param array $columns + * @return array + */ + public function modifyColumns(array $columns): array + { + foreach ($this->columns->getColumns() as $key => $label) { + $columns[$key] = $label; + } + + if ($remove = $this->columns->getRemoved()) { + $columns = array_diff_key($columns, array_flip($remove)); + } + + if ($only = $this->columns->getOnly()) { + $columns = array_intersect_key($columns, array_flip($only)); + } + + foreach ($this->columns->getPositions() as $key => $position) { + [$direction, $reference] = $position; + + if (!isset($direction) || !isset($reference)) { + continue; + } + + $new = []; + + foreach ($columns as $k => $label) { + if ('before' === $direction && $k === $reference) { + $new[$key] = $columns[$key]; + } + + $new[$k] = $label; + + if ('after' === $direction && $k === $reference) { + $new[$key] = $columns[$key]; + } + } + + $columns = $new; + } + + return $columns; + } + + /** + * Populate the PostType columns. + * + * @param string $column + * @param int $post_id + * @return void + */ + public function populateColumns(string $column, int $post_id): void + { + $callback = $this->columns->getPopulateCallback($column); + + if ($callback) { + call_user_func_array($callback, [$post_id]); + } + } + + /** + * Set the PostTypes sortable columns. + * + * @param array $columns + * @return array + */ + public function setSortableColumns(array $columns): array + { + $sortable = $this->columns->getSortableColumns(); + + return array_merge($columns, $sortable); + } + + /** + * Sort PostType columns. + * + * @param WP_Query $query + * @return void + */ + public function sortSortableColumns(WP_Query $query): void + { + if (!is_admin() || !$query->is_main_query()) { + return; + } + + $column = $query->get('orderby'); + $callback = $this->columns->getSortCallback($column); + + if ($callback) { + call_user_func_array($callback, [$query]); + } + } +} diff --git a/src/Registration/Integrations/ManagePostTypeFilters.php b/src/Registration/Integrations/ManagePostTypeFilters.php new file mode 100644 index 0000000..db7cde0 --- /dev/null +++ b/src/Registration/Integrations/ManagePostTypeFilters.php @@ -0,0 +1,84 @@ +posttype = $posttype; + } + + /** + * Add hooks. + * + * @return void + */ + public function register(): void + { + add_action('restrict_manage_posts', [$this, 'manageFilters'], 10, 1); + } + + /** + * Manage the post type filters. + * + * @param string $posttype + * @return void + */ + public function manageFilters(string $posttype): void + { + if ($posttype !== $this->posttype->name()) { + return; + } + + foreach ($this->posttype->filters() as $taxonomy) { + if (!is_object_in_taxonomy($posttype, $taxonomy)) { + continue; + } + + $tax = get_taxonomy($taxonomy); + + if (! $tax) { + return; + } + + $query_var = $tax->query_var; + $show_all = $tax->labels->all_items; + $filter_by_item = $tax->labels->filter_by_item; + $is_heirarchical = $tax->hierarchical; + $selected = isset($_GET[$query_var]) ? $_GET[$query_var] : ''; + + $options = [ + 'name' => $query_var, + 'value_field' => 'slug', + 'taxonomy' => $taxonomy, + 'show_option_all' => $show_all, + 'hierarchical' => $is_heirarchical, + 'hide_empty' => 0, + 'show_count' => 0, + 'orderby' => 'name', + 'selected' => $selected, + ]; + + echo ''; + + wp_dropdown_categories($options); + } + } +} diff --git a/src/Registrars/TaxonomyRegistrar.php b/src/Registration/Integrations/ManageTaxonomyColumns.php similarity index 67% rename from src/Registrars/TaxonomyRegistrar.php rename to src/Registration/Integrations/ManageTaxonomyColumns.php index c223e09..2be1750 100644 --- a/src/Registrars/TaxonomyRegistrar.php +++ b/src/Registration/Integrations/ManageTaxonomyColumns.php @@ -1,11 +1,12 @@ taxonomy->name(); - add_action('init', [$this, 'registerTaxonomy'], 9); - add_action('init', [$this, 'registerTaxonomyToPostTypes'], 10); add_action('init', [$this, 'createcolumns'], 10); - - // Handle Taxonomy columns. add_filter('manage_edit-' . $name . '_columns', [$this, 'modifyColumns'], 10, 1); add_action('manage_' . $name . '_custom_column', [$this, 'populateColumns'], 10, 3); add_filter('manage_edit-' . $name . '_sortable_columns', [$this, 'setSortableColumns'], 10, 1); add_action('parse_term_query', [$this, 'sortSortableColumns'], 10, 1); - - // Register custom hooks. - $this->taxonomy->hooks(); } /** @@ -59,61 +53,18 @@ public function register() * * @return void */ - public function createColumns() + public function createColumns(): void { $this->columns = $this->taxonomy->columns(new Columns()); } - /** - * Register the Taxonomy. - * - * @return void - */ - public function registerTaxonomy() - { - register_taxonomy($this->taxonomy->name(), [], $this->generateOptions()); - } - - /** - * Generate Taxonomy options. - * - * @return array - */ - public function generateOptions() - { - $defaults = [ - 'public' => true, - 'show_in_rest' => true, - 'hierarchical' => true, - 'show_admin_column' => true, - 'labels' => $this->taxonomy->labels(), - 'rewrite' => [ - 'slug' => $this->taxonomy->slug(), - ], - ]; - - return array_replace_recursive($defaults, $this->taxonomy->options()); - } - - /** - * Register Taxonomy to post types. - * - * @return void - */ - public function registerTaxonomyToPostTypes() - { - foreach ($this->taxonomy->posttypes() as $posttype) { - register_taxonomy_for_object_type($this->taxonomy->name(), $posttype); - } - } - /** * Modify the Taxonomy columns. * * @param array $columns * @return array */ - public function modifyColumns(array $columns) + public function modifyColumns(array $columns): array { foreach ($this->columns->getColumns() as $key => $label) { $columns[$key] = $label; @@ -162,7 +113,7 @@ public function modifyColumns(array $columns) * @param int $term_id * @return void */ - public function populateColumns($content, $column, $term_id) + public function populateColumns(string $content, string $column, int $term_id): void { $callback = $this->columns->getPopulateCallback($column); @@ -177,7 +128,7 @@ public function populateColumns($content, $column, $term_id) * @param array $columns * @return array */ - public function setSortableColumns($columns) + public function setSortableColumns(array $columns): array { return array_merge($columns, $this->columns->getSortableColumns()); } @@ -188,7 +139,7 @@ public function setSortableColumns($columns) * @param \WP_Term_Query $query * @return void */ - public function sortSortableColumns($query) + public function sortSortableColumns(WP_Term_Query $query): void { if (!is_admin() || !is_array($query->query_vars['taxonomy']) || diff --git a/src/Registration/Integrations/RegisterPostType.php b/src/Registration/Integrations/RegisterPostType.php new file mode 100644 index 0000000..f47a03d --- /dev/null +++ b/src/Registration/Integrations/RegisterPostType.php @@ -0,0 +1,80 @@ +posttype = $posttype; + } + + /** + * Add hooks. + * + * @return void + */ + public function register(): void + { + add_action('init', [$this, 'registerPostType']); + add_filter('register_post_type_args', [$this, 'setOptions'], 10, 2); + + $this->posttype->hooks(); + } + + /** + * Register to post type to WordPress. + * + * @return void + */ + public function registerPostType(): void + { + if (post_type_exists($this->posttype->name())) { + return; + } + + register_post_type($this->posttype->name(), []); + } + + /** + * Set the post type options. + * + * @param array $options + * @param string $posttype + * @return array + */ + public function setOptions(array $options, string $posttype): array + { + if ($posttype !== $this->posttype->name()) { + return $options; + } + + $defaults = [ + 'public' => true, + 'show_in_rest' => true, + 'labels' => $this->posttype->labels(), + 'taxonomies' => $this->posttype->taxonomies(), + 'supports' => $this->posttype->supports(), + 'menu_icon' => $this->posttype->icon(), + 'rewrite' => [ + 'slug' => $this->posttype->slug(), + ], + ]; + + return array_replace_recursive($options, $defaults, $this->posttype->options()); + } +} diff --git a/src/Registration/Integrations/RegisterTaxonomy.php b/src/Registration/Integrations/RegisterTaxonomy.php new file mode 100644 index 0000000..689bec1 --- /dev/null +++ b/src/Registration/Integrations/RegisterTaxonomy.php @@ -0,0 +1,81 @@ +taxonomy = $taxonomy; + } + + /** + * Register the Taxonomy to WordPress. + * + * @return void + */ + public function register(): void + { + add_action('init', [$this, 'registerTaxonomy'], 9); + add_action('init', [$this, 'registerTaxonomyToPostTypes'], 10); + + $this->taxonomy->hooks(); + } + + /** + * Register the Taxonomy. + * + * @return void + */ + public function registerTaxonomy(): void + { + register_taxonomy($this->taxonomy->name(), [], $this->generateOptions()); + } + + /** + * Generate Taxonomy options. + * + * @return array + */ + public function generateOptions(): array + { + $defaults = [ + 'public' => true, + 'show_in_rest' => true, + 'hierarchical' => true, + 'show_admin_column' => true, + 'labels' => $this->taxonomy->labels(), + 'rewrite' => [ + 'slug' => $this->taxonomy->slug(), + ], + ]; + + return array_replace_recursive($defaults, $this->taxonomy->options()); + } + + /** + * Register Taxonomy to post types. + * + * @return void + */ + public function registerTaxonomyToPostTypes(): void + { + foreach ($this->taxonomy->posttypes() as $posttype) { + register_taxonomy_for_object_type($this->taxonomy->name(), $posttype); + } + } +} diff --git a/src/Registration/Service.php b/src/Registration/Service.php new file mode 100644 index 0000000..25533b2 --- /dev/null +++ b/src/Registration/Service.php @@ -0,0 +1,71 @@ +integrations = $integrations; + } + + /** + * Register a PostType or Taxonomy. + * + * @param PostTypeContract|TaxonomyContract $definition + * @return void + */ + public function register(PostTypeContract|TaxonomyContract $definition): void + { + foreach ($this->integrations as $integration) { + (new $integration($definition))->register(); + } + } + + /** + * Create a Registration Service for PostType. + * + * @return self + */ + public static function forPostType(): self + { + return new self([ + RegisterPostType::class, + ManagePostTypeFilters::class, + ManagePostTypeColumns::class, + ]); + } + + /** + * Create a Registration Service for Taxonomy. + * + * @return self + */ + public static function forTaxonomy(): self + { + return new self([ + RegisterTaxonomy::class, + ManageTaxonomyColumns::class, + ]); + } +} diff --git a/src/Taxonomy.php b/src/Taxonomy.php index 1fdd318..aedc803 100644 --- a/src/Taxonomy.php +++ b/src/Taxonomy.php @@ -4,7 +4,7 @@ use PostTypes\Columns; use PostTypes\Contracts\TaxonomyContract; -use PostTypes\Registrars\TaxonomyRegistrar; +use PostTypes\Registration\Service; abstract class Taxonomy implements TaxonomyContract { @@ -77,12 +77,12 @@ public function hooks(): void } /** - * Register the taxonomy. + * Register the Taxonomy. * * @return void */ public function register(): void { - (new TaxonomyRegistrar($this))->register(); + Service::forTaxonomy()->register($this); } } diff --git a/tests/PostTypeTest.php b/tests/PostTypeTest.php index c0bbd4d..e374671 100644 --- a/tests/PostTypeTest.php +++ b/tests/PostTypeTest.php @@ -1,6 +1,7 @@ method('name') ->will($this->returnValue('book')); + $columns = new Columns; + $this->assertEquals('book', $stub->slug()); $this->assertEquals([], $stub->labels()); $this->assertEquals([], $stub->options()); @@ -20,6 +23,7 @@ public function test_post_type_returns_defaults() $this->assertEquals(['title', 'editor'], $stub->supports()); $this->assertEquals(null, $stub->icon()); $this->assertEquals([], $stub->filters()); + $this->assertEquals($columns, $stub->columns($columns)); $this->assertEquals(null, $stub->hooks()); } } diff --git a/tests/Registrars/PostTypeRegistrarTest.php b/tests/Registration/Integrations/ManagePostTypeColumnsTest.php similarity index 51% rename from tests/Registrars/PostTypeRegistrarTest.php rename to tests/Registration/Integrations/ManagePostTypeColumnsTest.php index 0ac3db1..1080197 100644 --- a/tests/Registrars/PostTypeRegistrarTest.php +++ b/tests/Registration/Integrations/ManagePostTypeColumnsTest.php @@ -3,74 +3,11 @@ use PHPUnit\Framework\TestCase; use PostTypes\Column; use PostTypes\Columns; +use PostTypes\Registration\Integrations\ManagePostTypeColumns; use PostTypes\PostType; -use PostTypes\Registrars\PostTypeRegistrar; -class PostTypeRegistrarTest extends TestCase +class ManagePostTypeColumnsTest extends TestCase { - public function test_can_create_registrar() - { - $stub = $this->getMockForAbstractClass(PostType::class); - - $stub->expects($this->any()) - ->method('name') - ->will($this->returnValue('book')); - - $registrar = new PostTypeRegistrar($stub); - - $this->assertInstanceOf(PostTypeRegistrar::class, $registrar); - } - - public function test_will_modify_post_type() - { - $stub = $this->getMockForAbstractClass(PostType::class); - - $stub->expects($this->any()) - ->method('name') - ->will($this->returnValue('book')); - - $registrar = new PostTypeRegistrar($stub); - - $args = [ - 'public' => false, - ]; - - $options = $registrar->modifyPostType($args, 'book'); - - $expected = [ - 'public' => true, - 'show_in_rest' => true, - 'labels' => [], - 'taxonomies' => [], - 'supports' => ['title', 'editor'], - 'menu_icon' => null, - 'rewrite' => [ - 'slug' => 'book', - ], - ]; - - $this->assertEquals($expected, $options); - } - - public function test_will_not_modify_post_type_if_name_does_not_match() - { - $stub = $this->getMockForAbstractClass(PostType::class); - - $stub->expects($this->any()) - ->method('name') - ->will($this->returnValue('book')); - - $registrar = new PostTypeRegistrar($stub); - - $args = [ - 'public' => false, - ]; - - $options = $registrar->modifyPostType($args, 'post'); - - $this->assertEquals($args, $options); - } - public function test_can_modify_columns() { $defaults = [ @@ -93,9 +30,9 @@ public function test_can_modify_columns() ->method('columns') ->will($this->returnValue($columns)); - $registrar = new PostTypeRegistrar($stub); - $registrar->createColumns(); - $output = $registrar->modifyColumns($defaults); + $integration = new ManagePostTypeColumns($stub); + $integration->createColumns(); + $output = $integration->modifyColumns($defaults); $expected = [ 'cb' => '', @@ -134,9 +71,9 @@ public function test_can_populate_column() ->method('columns') ->will($this->returnValue($columns)); - $registrar = new PostTypeRegistrar($stub); - $registrar->createColumns(); - $registrar->populateColumns('column', 1); + $integration = new ManagePostTypeColumns($stub); + $integration->createColumns(); + $integration->populateColumns('column', 1); } public function test_can_set_sortable_columns() @@ -159,9 +96,9 @@ public function test_can_set_sortable_columns() ->method('columns') ->will($this->returnValue($columns)); - $registrar = new PostTypeRegistrar($stub); - $registrar->createColumns(); - $output = $registrar->setSortableColumns($sortable); + $integration = new ManagePostTypeColumns($stub); + $integration->createColumns(); + $output = $integration->setSortableColumns($sortable); $expected = [ 'title' => 'title', diff --git a/tests/Registrars/TaxonomyRegistrarTest.php b/tests/Registration/Integrations/ManageTaxonomyColumnsTest.php similarity index 54% rename from tests/Registrars/TaxonomyRegistrarTest.php rename to tests/Registration/Integrations/ManageTaxonomyColumnsTest.php index 697b0f7..23c5298 100644 --- a/tests/Registrars/TaxonomyRegistrarTest.php +++ b/tests/Registration/Integrations/ManageTaxonomyColumnsTest.php @@ -3,63 +3,11 @@ use PHPUnit\Framework\TestCase; use PostTypes\Column; use PostTypes\Columns; -use PostTypes\Registrars\TaxonomyRegistrar; +use PostTypes\Registration\Integrations\ManageTaxonomyColumns; use PostTypes\Taxonomy; -class TaxonomyRegistrarTest extends TestCase +class ManageTaxonomyColumnsTest extends TestCase { - public function test_can_create_registrar() - { - $stub = $this->getMockForAbstractClass(Taxonomy::class); - - $stub->expects($this->any()) - ->method('name') - ->will($this->returnValue('genre')); - - $registrar = new TaxonomyRegistrar($stub); - - $this->assertInstanceOf(TaxonomyRegistrar::class, $registrar); - } - - public function test_can_generate_options_with_overrides() - { - $stub = $this->getMockBuilder(Taxonomy::class) - ->getMock(); - - $stub->expects($this->any()) - ->method('name') - ->will($this->returnValue('genre')); - - $stub->expects($this->any()) - ->method('slug') - ->will($this->returnValue('genre')); - - - $stub->expects($this->once()) - ->method('options') - ->will($this->returnValue([ - 'public' => false, - ])); - - - $registrar = new TaxonomyRegistrar($stub); - - $options = $registrar->generateOptions(); - - $expected = [ - 'public' => false, - 'show_in_rest' => true, - 'hierarchical' => true, - 'show_admin_column' => true, - 'labels' => [], - 'rewrite' => [ - 'slug' => 'genre', - ], - ]; - - $this->assertEquals($expected, $options); - } - public function test_can_modify_columns() { $defaults = [ @@ -81,9 +29,9 @@ public function test_can_modify_columns() ->method('columns') ->will($this->returnValue($columns)); - $registrar = new TaxonomyRegistrar($stub); - $registrar->createColumns(); - $output = $registrar->modifyColumns($defaults); + $integration = new ManageTaxonomyColumns($stub); + $integration->createColumns(); + $output = $integration->modifyColumns($defaults); $expected = [ 'cb' => '', @@ -121,9 +69,9 @@ public function test_can_populate_column() ->method('columns') ->will($this->returnValue($columns)); - $registrar = new TaxonomyRegistrar($stub); - $registrar->createColumns(); - $registrar->populateColumns('', 'column', 1); + $integration = new ManageTaxonomyColumns($stub); + $integration->createColumns(); + $integration->populateColumns('', 'column', 1); } public function test_can_set_sortable_columns() @@ -146,9 +94,9 @@ public function test_can_set_sortable_columns() ->method('columns') ->will($this->returnValue($columns)); - $registrar = new TaxonomyRegistrar($stub); - $registrar->createColumns(); - $output = $registrar->setSortableColumns($sortable); + $integration = new ManageTaxonomyColumns($stub); + $integration->createColumns(); + $output = $integration->setSortableColumns($sortable); $expected = [ 'title' => 'title', diff --git a/tests/Registration/Integrations/RegisterPostTypeTest.php b/tests/Registration/Integrations/RegisterPostTypeTest.php new file mode 100644 index 0000000..50ff2e9 --- /dev/null +++ b/tests/Registration/Integrations/RegisterPostTypeTest.php @@ -0,0 +1,58 @@ +getMockForAbstractClass(PostType::class); + + $stub->expects($this->any()) + ->method('name') + ->will($this->returnValue('book')); + + $inegration = new RegisterPostType($stub); + + $args = [ + 'public' => false, + ]; + + $options = $inegration->setOptions($args, 'book'); + + $expected = [ + 'public' => true, + 'show_in_rest' => true, + 'labels' => [], + 'taxonomies' => [], + 'supports' => ['title', 'editor'], + 'menu_icon' => null, + 'rewrite' => [ + 'slug' => 'book', + ], + ]; + + $this->assertEquals($expected, $options); + } + + public function test_will_not_modify_post_type_if_name_does_not_match() + { + $stub = $this->getMockForAbstractClass(PostType::class); + + $stub->expects($this->any()) + ->method('name') + ->will($this->returnValue('book')); + + $inegration = new RegisterPostType($stub); + + $args = [ + 'public' => false, + ]; + + $options = $inegration->setOptions($args, 'post'); + + $this->assertEquals($args, $options); + } +} diff --git a/tests/Registration/Integrations/RegisterTaxonomyTest.php b/tests/Registration/Integrations/RegisterTaxonomyTest.php new file mode 100644 index 0000000..626f0fe --- /dev/null +++ b/tests/Registration/Integrations/RegisterTaxonomyTest.php @@ -0,0 +1,47 @@ +getMockBuilder(Taxonomy::class) + ->getMock(); + + $stub->expects($this->any()) + ->method('name') + ->will($this->returnValue('genre')); + + $stub->expects($this->any()) + ->method('slug') + ->will($this->returnValue('genre')); + + + $stub->expects($this->once()) + ->method('options') + ->will($this->returnValue([ + 'public' => false, + ])); + + + $integration = new RegisterTaxonomy($stub); + + $options = $integration->generateOptions(); + + $expected = [ + 'public' => false, + 'show_in_rest' => true, + 'hierarchical' => true, + 'show_admin_column' => true, + 'labels' => [], + 'rewrite' => [ + 'slug' => 'genre', + ], + ]; + + $this->assertEquals($expected, $options); + } +} diff --git a/tests/Registration/ServiceTest.php b/tests/Registration/ServiceTest.php new file mode 100644 index 0000000..8d4468a --- /dev/null +++ b/tests/Registration/ServiceTest.php @@ -0,0 +1,11 @@ +method('name') ->will($this->returnValue('genre')); + $columns = new Columns; + $this->assertEquals('genre', $stub->slug()); $this->assertEquals([], $stub->labels()); $this->assertEquals([], $stub->options()); $this->assertEquals([], $stub->posttypes()); + $this->assertEquals($columns, $stub->columns($columns)); $this->assertEquals(null, $stub->hooks()); } }