Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract fixtures into separate classes #234

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}