GeneratorTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Faker\Test;
  3. use Faker\Generator;
  4. use PHPUnit\Framework\TestCase;
  5. class GeneratorTest extends TestCase
  6. {
  7. public function testAddProviderGivesPriorityToNewlyAddedProvider()
  8. {
  9. $generator = new Generator;
  10. $generator->addProvider(new FooProvider());
  11. $generator->addProvider(new BarProvider());
  12. $this->assertEquals('barfoo', $generator->format('fooFormatter'));
  13. }
  14. public function testGetFormatterReturnsCallable()
  15. {
  16. $generator = new Generator;
  17. $provider = new FooProvider();
  18. $generator->addProvider($provider);
  19. $this->assertInternalType('callable', $generator->getFormatter('fooFormatter'));
  20. }
  21. public function testGetFormatterReturnsCorrectFormatter()
  22. {
  23. $generator = new Generator;
  24. $provider = new FooProvider();
  25. $generator->addProvider($provider);
  26. $expected = array($provider, 'fooFormatter');
  27. $this->assertEquals($expected, $generator->getFormatter('fooFormatter'));
  28. }
  29. /**
  30. * @expectedException InvalidArgumentException
  31. */
  32. public function testGetFormatterThrowsExceptionOnIncorrectProvider()
  33. {
  34. $generator = new Generator;
  35. $generator->getFormatter('fooFormatter');
  36. }
  37. /**
  38. * @expectedException InvalidArgumentException
  39. */
  40. public function testGetFormatterThrowsExceptionOnIncorrectFormatter()
  41. {
  42. $generator = new Generator;
  43. $provider = new FooProvider();
  44. $generator->addProvider($provider);
  45. $generator->getFormatter('barFormatter');
  46. }
  47. public function testFormatCallsFormatterOnProvider()
  48. {
  49. $generator = new Generator;
  50. $provider = new FooProvider();
  51. $generator->addProvider($provider);
  52. $this->assertEquals('foobar', $generator->format('fooFormatter'));
  53. }
  54. public function testFormatTransfersArgumentsToFormatter()
  55. {
  56. $generator = new Generator;
  57. $provider = new FooProvider();
  58. $generator->addProvider($provider);
  59. $this->assertEquals('bazfoo', $generator->format('fooFormatterWithArguments', array('foo')));
  60. }
  61. public function testParseReturnsSameStringWhenItContainsNoCurlyBraces()
  62. {
  63. $generator = new Generator();
  64. $this->assertEquals('fooBar#?', $generator->parse('fooBar#?'));
  65. }
  66. public function testParseReturnsStringWithTokensReplacedByFormatters()
  67. {
  68. $generator = new Generator();
  69. $provider = new FooProvider();
  70. $generator->addProvider($provider);
  71. $this->assertEquals('This is foobar a text with foobar', $generator->parse('This is {{fooFormatter}} a text with {{ fooFormatter }}'));
  72. }
  73. public function testMagicGetCallsFormat()
  74. {
  75. $generator = new Generator;
  76. $provider = new FooProvider();
  77. $generator->addProvider($provider);
  78. $this->assertEquals('foobar', $generator->fooFormatter);
  79. }
  80. public function testMagicCallCallsFormat()
  81. {
  82. $generator = new Generator;
  83. $provider = new FooProvider();
  84. $generator->addProvider($provider);
  85. $this->assertEquals('foobar', $generator->fooFormatter());
  86. }
  87. public function testMagicCallCallsFormatWithArguments()
  88. {
  89. $generator = new Generator;
  90. $provider = new FooProvider();
  91. $generator->addProvider($provider);
  92. $this->assertEquals('bazfoo', $generator->fooFormatterWithArguments('foo'));
  93. }
  94. public function testSeed()
  95. {
  96. $generator = new Generator;
  97. $generator->seed(0);
  98. $mtRandWithSeedZero = mt_rand();
  99. $generator->seed(0);
  100. $this->assertEquals($mtRandWithSeedZero, mt_rand(), 'seed(0) should be deterministic.');
  101. $generator->seed();
  102. $mtRandWithoutSeed = mt_rand();
  103. $this->assertNotEquals($mtRandWithSeedZero, $mtRandWithoutSeed, 'seed() should be different than seed(0)');
  104. $generator->seed();
  105. $this->assertNotEquals($mtRandWithoutSeed, mt_rand(), 'seed() should not be deterministic.');
  106. $generator->seed('10');
  107. $this->assertTrue(true, 'seeding with a non int value doesn\'t throw an exception');
  108. }
  109. }
  110. class FooProvider
  111. {
  112. public function fooFormatter()
  113. {
  114. return 'foobar';
  115. }
  116. public function fooFormatterWithArguments($value = '')
  117. {
  118. return 'baz' . $value;
  119. }
  120. }
  121. class BarProvider
  122. {
  123. public function fooFormatter()
  124. {
  125. return 'barfoo';
  126. }
  127. }