[Laravel]Fakerのチートシート

Laravel Laravel

Fakerでテストデータ生成でざっくりと使い方や簡単なメソッドを紹介しましたが、一覧化してみました。

Faker\Providor\Base

MethodTypeDescriptionExample
randomDigit()intランダムな数字(0-9)を取得5
randomDigitNot($except)intランダムな数字(0-9)で$except以外を取得7
randomDigitNotNull()intランダムな数字(1-9)を取得9
randomNumber($nbDigits = null, $strict = false)int$strictfalseの場合は0〜桁数($nbDigis)、trueの場合は桁数のみのランダムな数値を取得。桁数がnullの場合は1と同義。79907610
randomFloat($nbMaxDecimals = null, $min = 0, $max = null)float最小値($min)から最大値($max)までの間で、指定した小数点以下の桁数($nbMaxDecimals)の数値を取得48.8932
numberBetween($min = 1000, $max = 9000)int範囲内のランダムな数値を取得79907610
randomLetter()stringa-zのランダムな英小文字を取得"b"
randomElements($array = array ('a','b','c'), $count = 1)array指定した配列の中から$count数分だけ抽出して配列で取得array("c")
randomElement($array = array ("a","b","c"))string指定した配列の中から1つだけ取得"b"
shuffle("hello, world")string指定した文字列をシャッフルして取得"rlo,h eoldlw"
shuffle(array(1, 2, 3))array指定した配列をシャッフルして取得array(2, 1, 3)
numerify('Hello ###')string"Hello 609"
lexify('Hello ???')string"Hello wgt"
bothify('Hello ##??')string"Hello 42jz"
asciify('Hello ***')string"Hello R6+"
regexify('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}')string"sm0@y8k96a.ej"

Faker\Provider\Lorem

MethodTypeDescriptionExample
word()string"aut"
words($nb = 3, $asText = false)arrayarray('porro', 'sed', 'magni')
sentence($nbWords = 6, $variableNbWords = true)stringSit vitae voluptas sint non voluptates.
sentences($nb = 3, $asText = false)arrayarray("Optio quos qui illo error.", "省略", "省略")
paragraph($nbSentences = 3, $variableNbSentences = true)string
paragraphs($nb = 3, $asText = false)array
text($maxNbChars = 200)string

Faker\Provider\DateTime

MethodTypeDescriptionExample
unixTime($max = 'now')int0〜$maxまでのランダムなタイムスタンプを取得1061306726
dateTime($max = 'now', $timezone = null)DateTimeタイムスタンプが0以上のランダムなDateTimeオブジェクトを取得
dateTimeAD($max = 'now', $timezone = null)DateTime西暦1年1月1日から現在までのランダムなDateTimeオブジェクトを取得
iso8601($max = 'now')string
date($format = 'Y-m-d', $max = 'now')string
time($format = 'H:i:s', $max = 'now')string
dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null)DateTime
dateTimeInInterval($date = '-30 years', $interval = '+5 days', $timezone = null)DateTime
dateTimeThisCentury($max = 'now', $timezone = null)DateTime現在の世紀内のランダムなDateTimeオブジェクトを取得
dateTimeThisDecade($max = 'now', $timezone = null)DateTime現在の10年内のランダムなDateTimeオブジェクトを取得
dateTimeThisYear($max = 'now', $timezone = null)DateTime現在の年内のランダムなDateTimeオブジェクトを取得
dateTimeThisMonth($max = 'now', $timezone = null)DateTime現在の月内のランダムなDateTimeオブジェクトを取得
amPm($max = 'now')stringam
dayOfMonth($max = 'now')string22
dayOfWeek($max = 'now')stringTuesday
month($max = 'now')string7
monthName($max = 'now')stringSeptember
year($max = 'now')string1673
century()stringXVII
timezone()stringEurope/Paris

Faker\Provider\Internet

MethodTypeDescriptionExample
email()stringjdoe@acme.biz
safeEmail()stringjdoe@example.com
freeEmail()stringjdoe@gmail.com
companyEmail()stringjdoe@dawson.com
freeEmailDomain()stringgmail.com
safeEmailDomain()stringexample.org
userName()stringjdoe
password($minLength = 6, $maxLength = 20)stringfY4èHdZv68
domainName()stringtiramisu.com
domainWord()stringfaber
tld()stringcom
url()stringhttp://www.runolfsdottir.com/
slug($nbWords = 6, $variableNbWords = true)stringaut-repellat-commodi-vel-itaque-nihil-id-saepe-nostrum
ipv4()string237.149.115.38
ipv6()string35cd:186d:3e23:2986:ef9f:5b41:42a4:e6f1
localIpv4()string10.1.1.17
macAddress()string32:F1:39:2F:D6:18

Faker\Provider\UserAgent

MethodTypeDescriptionExample
userAgent()
chrome()
firefox()
safari()
opera()
internetExplorer()

Faker\Provider\Payment

MethodTypeDescriptionExample
creditCardType()stringMasterCard
creditCardNumber($type = null, $formatted = false, $separator = '-')string4485480221084675
creditCardExpirationDate($valid = true)DateTime
tionDateString($valid = true, $expirationDateFormat = null)string04/13
creditCardDetails($valid = true)array
iban($countryCode = null, $prefix = '', $length = null)string
swiftBicNumber()string

Faker\Provider\Color

MethodTypeDescriptionExample
hexColor()string#fa3cc2
safeHexColor()string#ff0044
rgbColorAsArray()arrayarray(0,255,122)
rgbColor()string0,255,122
rgbCssColor()stringrgb(0,255,122)
rgbaCssColor()stringrgba(0,255,122,0.8)
safeColorName()stringblue
colorName()stringNavajoWhite
hslColor()string340,50,20
hslColorAsArray()arrayarray(340, 50, 20)

Faker\Provider\File

MethodTypeDescriptionExample
mimeType()stringvideo/avi
fileExtension()stringavi
file($sourceDirectory = '/tmp', $targetDirectory = '/tmp', $fullPath = true)string

Faker\Provider\Image

MethodTypeDescriptionExample
imageUrl($width = 640, $height = 480, $category = null, $randomize = true, $word = null, $gray = false)
image($dir = null, $width = 640, $height = 480, $category = null, $fullPath = true, $randomize = true, $word = null, $gray = false)

Faker\Provider\Uuid

MethodTypeDescriptionExample
uuid()string7e57d004-2b97-0e7a-b45f-5387367791cd

Faker\Provider\Barcode

MethodTypeDescriptionExample
ean13()string
ean8()string
isbn13()string
isbn10()string

Faker\Provider\Miscellaneous

MethodTypeDescriptionExample
boolean($chanceOfGettingTrue = 50)booltrue
md5()stringcfcd208495d565ef66e7dff9f98764da
sha1()stringb5d86317c2a144cd04d0d7c03b2b02666fafadf2
sha256()string85086017559ccc40638fcde2fecaf295e0de7ca51b7517b6aebeaaf75b4d4654
locale()stringja_JP
countryCode()stringJP
languageCode()stringja
currencyCode()stringJPY
emojistring😁

Faker\Provider\Biased

MethodTypeDescriptionExample
biasedNumberBetween($min = 0, $max = 100, $function = 'sqrt')int

Faker\Provider\HtmlLorem

MethodTypeDescriptionExample
randomHtml($maxDepth = 4, $maxWidth = 4)string

Follow me!

コメント

PAGE TOP
タイトルとURLをコピーしました