Git Product home page Git Product logo

faker's Introduction

Faker

Fake post data with wp-cli. Made for WordPress.

Build Status

Relies on nelmio/alice and fzaninotto/Faker.

Extended with user capabilities by 5queezer/faker.

Install

wp package install trendwerk/faker

Requires wp-cli >= 2.0.

Usage

wp faker fake <files>...

Options

Parameter Default Required Description
<file> null Yes Location to an Alice YAML file

Delete data

wp faker delete

Deletes all fake data.

Support

The YAML file supports:

Fields Description
WP_Post All properties from wp_insert_post
WP_User All properties from wp_insert_user
meta Post meta
terms Terms for taxonomies, see Terms
acf Advanced Custom Fields fields, see ACF

YAML / Faker

Your YAML file(s) could look like any the examples below.

For more understanding of the internals:

Basic

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_content: <paragraphs(4, true)>
    post_title: '<sentence()>'

Generates 100 posts with a title and content.

Post Type

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_title: '<sentence()>'
    post_type: 'testimonials'

Generates 100 posts from the post type testimonials with a title.

Meta

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_title: '<sentence()>'
    post_type: 'testimonials'
    meta:
      name: '<name()>'
      address: '<address()>'

Generates 100 testimonials with a title and a custom field called name and one called address.

Terms

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_content: <paragraphs(3, true)>
    post_title: '<sentence()>'
    terms:
      category: <terms('category', 1)>
      post_tag: <terms('post_tag', 7)>

Generates 100 posts with a title, content, 1 random category and 7 random tags.

Using <terms> is not required. You could also provide an array of integers yourself or use randomElements.

Options

Parameter Default Required
taxonomy null Yes
amount 1 No

ACF

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_content: <paragraphs(3, true)>
    post_title: '<sentence()>'
    acf:
      name: '<name()>'
      address: '<address()>'

Generates 100 posts with a title, content, and two filled ACF fields: name and address.

Duplicate field names

In ACF, it is possible to have multiple fields with the same name. This could cause formatting conflicts when faking data with this library. If you have two fields with the same name, using the unique field key is recommended:

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_content: <paragraphs(3, true)>
    post_title: '<sentence()>'
    acf:
      field_56cf2f782e9b1: '<name()>' # Name
      address: '<address()>'

Users

Trendwerk\Faker\Entity\User:
  user{1..10}:
    user_login: '<username()>'
    user_pass: '<username()>'
    first_name: '<firstName()>'
    last_name: '<lastName()>'
    display_name: '<firstName()> <lastName()>'
    user_email: '<email()>'
    role: 'author'

Generates 10 users with the role of an author.

Attachments

Currently the only type of supported attachments are images.

Images

Trendwerk\Faker\Entity\Image:
  image{1..3}:
    data: '<image()>'

Generates 3 image attachments. Images are provided by Faker, which in turn are provided by LoremPixel.

Post + (Featured) Image + User

# image.yml
Trendwerk\Faker\Entity\Image:
  image{1..3}:
    data: '<image()>'
# user.yml
Trendwerk\Faker\Entity\User:
  user{1..10}:
    user_login: '<username()>'
    user_pass: '<username()>'
    user_email: '<email()>'
    role: 'author'
# post.yml
Trendwerk\Faker\Post:
  post{1..1}:
    post_content: <paragraphs(3, true)>
    post_title: '<sentence()>'
    post_author: '@user*->id'
    meta:
      _thumbnail_id: '@image*->id'

You can now supply all three files to wp faker fake:

wp faker fake image.yml user.yml post.yml

Make sure you load the file that contains the referenced objects first.

faker's People

Contributors

5queezer avatar danielbachhuber avatar haroldangenent avatar vinkla avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

faker's Issues

Can't generate WooCommerce products

# image.yml
Trendwerk\Faker\Entity\Image:
  image{1..5}:
    data: '<image()>'
Trendwerk\Faker\Entity\Post:
  post{1..30}:
    post_content: <paragraphs(4, true)>
    post_title: '<words()>'
    post_type: 'product'
    terms:
      category: <terms('category', 3)>
    meta:
      _thumbnail_id: '@image*->id'
      _price: '<randomDigit()>'
      _visibility: 'visible'

The output is:

Warning: trim() expects parameter 1 to be string, array given in /var/www/public/wp-includes/class-wp-hook.php on line 298
Success: Generated 35 new posts.

I'm not sure what I could have done wrong. Also, is it possible to re-use a field? Like use the set value of _price on another meta field _regular_price. Thanks!

Error upon installation

Error: trendwerk/faker dev-master requires nelmio/alice ^2.2 -> satisfiable by nelmio/alice[2.2.0, 2.2.1, 2.2.2, 2.x-dev].

How is Faker installed?

How does this package install the fzaninotto/faker package? I want to use the latest version of thefzaninotto/faker package. Isn't it installed with Composer?

ACF Issue

The ACF no longer works for me in the latest version of ACF (5.6.0). Not sure how to debug this. Does it work for you in the latest beta of ACF?

Could not determine how to assign acf to a Trendwerk\Faker\Entity\User

Can't use acf on user fixtures.

UnexpectedValueException: Could not determine how to assign acf to a Trendwerk\Faker\Entity\User object in file /Users/user/.wp-cli/packages/vendor/nelmio/alice/src/Nelmio/Alice/Instances/Populator/Populator.php on line 104
Stack trace:
  1. UnexpectedValueException->() /Users/user/.wp-cli/packages/vendor/nelmio/alice/src/Nelmio/Alice/Instances/Populator/Populator.php:104
  2. Nelmio\Alice\Instances\Populator\Populator->populate() /Users/user/.wp-cli/packages/vendor/nelmio/alice/src/Nelmio/Alice/Fixtures/Loader.php:309
  3. Nelmio\Alice\Fixtures\Loader->populateObjects() /Users/user/.wp-cli/packages/vendor/nelmio/alice/src/Nelmio/Alice/Fixtures/Loader.php:147
  4. Nelmio\Alice\Fixtures\Loader->load() /Users/user/.wp-cli/packages/vendor/trendwerk/faker/src/Faker.php:32
  5. Trendwerk\Faker\Faker->getObjectCount() /Users/user/.wp-cli/packages/vendor/trendwerk/faker/src/Command.php:23
  6. Trendwerk\Faker\Command->fake() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php:98
  7. call_user_func() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php:98
  8. WP_CLI\Dispatcher\CommandFactory->WP_CLI\Dispatcher\{closure}() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php:489
  9. call_user_func() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php:489
 10. WP_CLI\Dispatcher\Subcommand->invoke() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:380
 11. WP_CLI\Runner->run_command() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:403
 12. WP_CLI\Runner->run_command_and_exit() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php:1174
 13. WP_CLI\Runner->start() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php:23
 14. WP_CLI\Bootstrap\LaunchRunner->process() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php:74
 15. WP_CLI\bootstrap() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php:27
 16. include() phar:///usr/local/Cellar/wp-cli/2.4.0/bin/wp/php/boot-phar.php:11
 17. include() /usr/local/Cellar/wp-cli/2.4.0/bin/wp:4

WP-cli: WP-CLI 2.5.0-alpha-9061b3e
PHP: 7.4.15

Please consider joining forces with bordoni/fakerpress

Hi, you are reinventing fakerpress, but:

  • fakerpress has more features (e.g. import images, set terms, delete fake data)
  • fakerpress has no wp-cli implementation (if I am not missing anything)

So this actually looks like a perfect match - you could concentrate on the wp-cli implementation and I am sure everybody is happy about more features and multiple eyes looking at code.

Please unite! Thanks!

[Feature Request] Submit to Packagist

Since WP-CLI version 2.0 it is now possible to register commands simply by requiring a package which registers a command. Basically installing wp-cli/wp-cli and trendwerk/faker as development dependencies makes it possible to run wp faker in the current project:

"require-dev": {
    "trendwerk/faker": "^0.1.1",
    "wp-cli/wp-cli": "^2.0"
}

Though, this package isn't submitted to Packagist (yet). Currently one has to add the repository URL to the repositories object in composer.json file.

Would it be possible if you registered it on Packagist?

How to get different data each time?

I am trying to generate random posts every few minutes using a cron job but for the given file I always get the same post title and content:

Trendwerk\Faker\Entity\Post:
  post{1..1}:
    post_content: <paragraphs(4, true)>
    post_title: '<sentence()>'

this is how my command looks like:

wp faker fake post.yml

Is it possible to randomize the generated content each time the command is executed?

Thanks in advance for your help!

Generate fake attachments

Anyway to create a post thumbnail with a certain image here. Either using the faker library or even just a special image in the theme or something?

Consider adding functional tests to this project

Functional tests are an integral ingredient of highly-quality, maintainable commands. WP-CLI tries to make it as easy as possible to add functional tests to your package with its wp scaffold package-tests command:

https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-tests

I'd encourage you to consider adding functional tests to your package :) By starting your functional tests early on, it also makes it much easier to maintain your project over time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.