Skip to content

Commit

Permalink
Enhancement: Extract fixtures into separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 26, 2020
1 parent 3d69b4d commit 86ebdd0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
},
"autoload-dev": {
"psr-4": {
"Faker\\Test\\": "test/Faker/"
"Faker\\Test\\": "test/Faker/",
"Faker\\Test\\Fixture\\": "test/Fixture/"
}
},
"conflict": {
Expand Down
51 changes: 15 additions & 36 deletions test/Faker/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ public function testMimeType(): void

public function testAddProviderGivesPriorityToNewlyAddedProvider()
{
$this->faker->addProvider(new FooProvider());
$this->faker->addProvider(new BarProvider());
$this->faker->addProvider(new Fixture\Provider\FooProvider());
$this->faker->addProvider(new Fixture\Provider\BarProvider());
self::assertEquals('barfoo', $this->faker->format('fooFormatter'));
}

public function testGetProvidersReturnsCorrectProviders()
{
$this->faker->addProvider(new FooProvider());
$this->faker->addProvider(new BarProvider());
self::assertInstanceOf(FooProvider::class, $this->faker->getProviders()[1]);
self::assertInstanceOf(BarProvider::class, $this->faker->getProviders()[0]);
$this->faker->addProvider(new Fixture\Provider\FooProvider());
$this->faker->addProvider(new Fixture\Provider\BarProvider());
self::assertInstanceOf(Fixture\Provider\FooProvider::class, $this->faker->getProviders()[1]);
self::assertInstanceOf(Fixture\Provider\BarProvider::class, $this->faker->getProviders()[0]);
self::assertCount(2, $this->faker->getProviders());
}

public function testGetFormatterReturnsCallable()
{
$this->faker->addProvider(new FooProvider());
$this->faker->addProvider(new Fixture\Provider\FooProvider());
self::assertIsCallable($this->faker->getFormatter('fooFormatter'));
}

public function testGetFormatterReturnsCorrectFormatter()
{
$provider = new FooProvider();
$provider = new Fixture\Provider\FooProvider();
$this->faker->addProvider($provider);
$expected = [$provider, 'fooFormatter'];
self::assertEquals($expected, $this->faker->getFormatter('fooFormatter'));
Expand All @@ -93,20 +93,20 @@ public function testGetFormatterThrowsExceptionOnIncorrectProvider()
public function testGetFormatterThrowsExceptionOnIncorrectFormatter()
{
$this->expectException(\InvalidArgumentException::class);
$this->faker->addProvider(new FooProvider());
$this->faker->addProvider(new Fixture\Provider\FooProvider());
$this->faker->getFormatter('barFormatter');
}

public function testFormatCallsFormatterOnProvider()
{
$this->faker->addProvider(new FooProvider());
$this->faker->addProvider(new Fixture\Provider\FooProvider());
self::assertEquals('foobar', $this->faker->format('fooFormatter'));
}

public function testFormatTransfersArgumentsToFormatter()
{
$this->faker = new Generator();
$provider = new FooProvider();
$provider = new Fixture\Provider\FooProvider();
$this->faker->addProvider($provider);
self::assertEquals('bazfoo', $this->faker->format('fooFormatterWithArguments', ['foo']));
}
Expand All @@ -118,7 +118,7 @@ public function testParseReturnsSameStringWhenItContainsNoCurlyBraces()

public function testParseReturnsStringWithTokensReplacedByFormatters()
{
$this->faker->addProvider(new FooProvider());
$this->faker->addProvider(new Fixture\Provider\FooProvider());
self::assertEquals('This is foobar a text with foobar', $this->faker->parse('This is {{fooFormatter}} a text with {{ fooFormatter }}'));
}

Expand All @@ -127,19 +127,19 @@ public function testParseReturnsStringWithTokensReplacedByFormatters()
*/
public function testMagicGetCallsFormat()
{
$this->faker->addProvider(new FooProvider());
$this->faker->addProvider(new Fixture\Provider\FooProvider());
self::assertEquals('foobar', $this->faker->fooFormatter);
}

public function testMagicCallCallsFormat()
{
$this->faker->addProvider(new FooProvider());
$this->faker->addProvider(new Fixture\Provider\FooProvider());
self::assertEquals('foobar', $this->faker->fooFormatter());
}

public function testMagicCallCallsFormatWithArguments()
{
$this->faker->addProvider(new FooProvider());
$this->faker->addProvider(new Fixture\Provider\FooProvider());
self::assertEquals('bazfoo', $this->faker->fooFormatterWithArguments('foo'));
}

Expand Down Expand Up @@ -285,24 +285,3 @@ public function word(): string
$uniqueGenerator->word();
}
}

final class FooProvider
{
public function fooFormatter()
{
return 'foobar';
}

public function fooFormatterWithArguments($value = '')
{
return 'baz' . $value;
}
}

final class BarProvider
{
public function fooFormatter()
{
return 'barfoo';
}
}
11 changes: 11 additions & 0 deletions test/Fixture/Provider/BarProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Faker\Test\Fixture\Provider;

final class BarProvider
{
public function fooFormatter()
{
return 'barfoo';
}
}
16 changes: 16 additions & 0 deletions test/Fixture/Provider/FooProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Faker\Test\Fixture\Provider;

final class FooProvider
{
public function fooFormatter()
{
return 'foobar';
}

public function fooFormatterWithArguments($value = '')
{
return 'baz' . $value;
}
}

0 comments on commit 86ebdd0

Please sign in to comment.