You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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