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

Add new files loader #821

Merged
merged 1 commit into from
Oct 22, 2017
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ phpstan: vendor-bin/phpstan/vendor

cs: ## Run the CS Fixer
cs: vendor-bin/php-cs-fixer/vendor
rm -rf fixtures/Bridge/Symfony/Application/cache/*
$(PHP_CS_FIXER) fix


Expand Down
2 changes: 2 additions & 0 deletions fixtures/Integration/another_dummy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
stdClass:
another_dummy: {}
2 changes: 2 additions & 0 deletions fixtures/Integration/dummy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
stdClass:
dummy: {}
11 changes: 10 additions & 1 deletion fixtures/Loader/IsolatedLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

use Nelmio\Alice\DataLoaderInterface;
use Nelmio\Alice\FileLoaderInterface;
use Nelmio\Alice\FilesLoaderInterface;
use Nelmio\Alice\ObjectSet;

/**
* Make use of NativeLoader for easy usage but ensure than no state is kept between each usage, perfect for isolated
* tests.
*/
class IsolatedLoader implements FileLoaderInterface, DataLoaderInterface
class IsolatedLoader implements FilesLoaderInterface, FileLoaderInterface, DataLoaderInterface
{
/**
* @inheritdoc
Expand All @@ -31,6 +32,14 @@ public function loadData(array $data, array $parameters = [], array $objects = [
return (new NativeLoader())->loadData($data, $parameters, $objects);
}

/**
* @inheritdoc
*/
public function loadFiles(array $files, array $parameters = [], array $objects = []): ObjectSet
{
return (new NativeLoader())->loadFiles($files, $parameters, $objects);
}

/**
* @inheritdoc
*/
Expand Down
11 changes: 10 additions & 1 deletion fixtures/Loader/NonIsolatedSymfonyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

use Nelmio\Alice\DataLoaderInterface;
use Nelmio\Alice\FileLoaderInterface;
use Nelmio\Alice\FilesLoaderInterface;
use Nelmio\Alice\ObjectSet;
use Symfony\Component\DependencyInjection\ContainerInterface;

class NonIsolatedSymfonyLoader implements FileLoaderInterface, DataLoaderInterface
class NonIsolatedSymfonyLoader implements FilesLoaderInterface, FileLoaderInterface, DataLoaderInterface
{
/**
* @var ContainerInterface
Expand All @@ -38,6 +39,14 @@ public function loadData(array $data, array $parameters = [], array $objects = [
return $this->container->get('nelmio_alice.data_loader')->loadData($data, $parameters, $objects);
}

/**
* @inheritdoc
*/
public function loadFiles(array $files, array $parameters = [], array $objects = []): ObjectSet
{
return $this->container->get('nelmio_alice.files_loader')->loadFiles($files, $parameters, $objects);
}

/**
* @inheritdoc
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Bridge/Symfony/Resources/config/loader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<argument type="service" id="nelmio_alice.generator" />
</service>


<service id="nelmio_alice.file_loader" public="true"
alias="nelmio_alice.file_loader.simple" />

Expand All @@ -31,6 +32,16 @@
<argument type="service" id="nelmio_alice.data_loader" />
</service>


<service id="nelmio_alice.files_loader" public="true"
alias="nelmio_alice.files_loader.simple" />

<service id="nelmio_alice.files_loader.simple"
class="Nelmio\Alice\Loader\SimpleFilesLoader">
<argument type="service" id="nelmio_alice.file_parser" />
<argument type="service" id="nelmio_alice.data_loader" />
</service>

</services>

</container>
35 changes: 35 additions & 0 deletions src/FilesLoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Nelmio\Alice;

use Nelmio\Alice\Throwable\LoadingThrowable;

/**
* Main interface of the library.
*/
interface FilesLoaderInterface
{
/**
* Loads a collection of fixture files.
*
* @param string[] $files Files to load.
* @param array $parameters Additional parameters to inject.
* @param array $objects Additional objects to inject.
*
* @throws LoadingThrowable
*
* @return ObjectSet Contains the list of objects and parameters loaded and injected.
*/
public function loadFiles(array $files, array $parameters = [], array $objects = []): ObjectSet;
}
26 changes: 25 additions & 1 deletion src/Loader/NativeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Nelmio\Alice\Faker\Provider\AliceProvider;
use Nelmio\Alice\FileLoaderInterface;
use Nelmio\Alice\FileLocator\DefaultFileLocator;
use Nelmio\Alice\FilesLoaderInterface;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\Chainable\CollectionDenormalizerWithTemporaryFixture;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\Chainable\NullListNameDenormalizer;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\Chainable\NullRangeNameDenormalizer;
Expand Down Expand Up @@ -171,6 +172,7 @@
*
* @method DataLoaderInterface getDataLoader()
* @method FileLoaderInterface getFileLoader()
* @method FilesLoaderInterface getFilesLoader()
* @method FixtureBuilderInterface getFixtureBuilder()
* @method GeneratorInterface getGenerator()
* @method ParserInterface getParser()
Expand Down Expand Up @@ -198,7 +200,7 @@
* @method CallerInterface getCaller()
* @method CallProcessorInterface getCallProcessor()
*/
class NativeLoader implements FileLoaderInterface, DataLoaderInterface
class NativeLoader implements FilesLoaderInterface, FileLoaderInterface, DataLoaderInterface
{
use IsAServiceTrait;

Expand All @@ -219,6 +221,11 @@ class NativeLoader implements FileLoaderInterface, DataLoaderInterface
*/
private $fileLoader;

/**
* @var FilesLoaderInterface
*/
private $filesLoader;

/**
* @var DataLoaderInterface
*/
Expand All @@ -229,6 +236,15 @@ public function __construct(FakerGenerator $fakerGenerator = null)
$this->fakerGenerator = (null === $fakerGenerator) ? $this->getFakerGenerator() : $fakerGenerator;
$this->dataLoader = $this->getDataLoader();
$this->fileLoader = $this->getFileLoader();
$this->filesLoader = $this->getFilesLoader();
}

/**
* @inheritdoc
*/
public function loadFiles(array $files, array $parameters = [], array $objects = []): ObjectSet
{
return $this->filesLoader->loadFiles($files, $parameters, $objects);
}

/**
Expand Down Expand Up @@ -263,6 +279,14 @@ protected function createFileLoader(): FileLoaderInterface
);
}

protected function createFilesLoader(): FilesLoaderInterface
{
return new SimpleFilesLoader(
$this->getParser(),
$this->dataLoader
);
}

protected function createFixtureBuilder(): FixtureBuilderInterface
{
return new SimpleBuilder(
Expand Down
67 changes: 67 additions & 0 deletions src/Loader/SimpleFilesLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Nelmio\Alice\Loader;

use Nelmio\Alice\DataLoaderInterface;
use Nelmio\Alice\FilesLoaderInterface;
use Nelmio\Alice\IsAServiceTrait;
use Nelmio\Alice\ObjectSet;
use Nelmio\Alice\Parser\IncludeProcessor\IncludeDataMerger;
use Nelmio\Alice\Parser\IncludeProcessorInterface;
use Nelmio\Alice\ParserInterface;

final class SimpleFilesLoader implements FilesLoaderInterface
{
use IsAServiceTrait;

/**
* @var DataLoaderInterface
*/
private $dataLoader;

/**
* @var IncludeProcessorInterface
*/
private $dataMerger;

/**
* @var ParserInterface
*/
private $parser;

public function __construct(ParserInterface $parser, DataLoaderInterface $dataLoader)
{
$this->parser = $parser;
$this->dataMerger = new IncludeDataMerger();
$this->dataLoader = $dataLoader;
}

/**
* @inheritdoc
*/
public function loadFiles(array $files, array $parameters = [], array $objects = []): ObjectSet
{
$data = array_reduce(
array_unique($files),
function (array $data, string $file): array {
$fileData = $this->parser->parse($file);

return $this->dataMerger->mergeInclude($data, $fileData);
},
[]
);

return $this->dataLoader->loadData($data, $parameters, $objects);
}
}
2 changes: 2 additions & 0 deletions src/Parser/RuntimeCacheParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ public function parse(string $file): array
}

$data = $this->parser->parse($realPath);

if (array_key_exists('include', $data)) {
$data = $this->includeProcessor->process($this, $file, $data);
}

$this->cache[$realPath] = $data;

return $data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Nelmio\Alice\FixtureBag;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\ChainableFixtureDenormalizerInterface;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\DummySpecificationBagDenormalizer;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\FakeSpecificationBagDenormalizer;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationsDenormalizerInterface;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParser\DummyFlagParser;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParserInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Nelmio\Alice\Definition\FlagBag;
use Nelmio\Alice\FixtureBag;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FixtureBagDenormalizerInterface;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParser\FakeFlagParser;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParserInterface;
use Nelmio\Alice\FixtureInterface;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Nelmio\Alice\Definition\Fixture\FakeFixture;
use Nelmio\Alice\Definition\Flag\ElementFlag;
use Nelmio\Alice\Definition\FlagBag;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\Value\FakeValueDenormalizer;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\ValueDenormalizerInterface;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParserInterface;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Nelmio\Alice\Definition\Fixture\FakeFixture;
use Nelmio\Alice\Definition\MethodCall\SimpleMethodCall;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\Arguments\FakeArgumentsDenormalizer;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\ArgumentsDenormalizerInterface;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FlagParser\FakeFlagParser;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Nelmio\Alice\Definition\Fixture\FakeFixture;
use Nelmio\Alice\Definition\FlagBag;
use Nelmio\Alice\Definition\Property;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\Value\FakeValueDenormalizer;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\SpecificationBagDenormalizer\ValueDenormalizerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use Nelmio\Alice\Definition\Fixture\DummyFixture;
use Nelmio\Alice\FixtureBag;
use Nelmio\Alice\FixtureBuilder\BareFixtureSet;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\FakeFixtureBagDenormalizer;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Parameter\FakeParameterBagDenormalizer;
use Nelmio\Alice\FixtureBuilder\DenormalizerInterface;
use Nelmio\Alice\ParameterBag;
use PHPUnit\Framework\TestCase;
Expand Down
1 change: 0 additions & 1 deletion tests/FixtureBuilder/SimpleBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Nelmio\Alice\FixtureBuilder;

use Nelmio\Alice\FixtureBag;
use Nelmio\Alice\FixtureBuilder\Denormalizer\FakeDenormalizer;
use Nelmio\Alice\FixtureBuilderInterface;
use Nelmio\Alice\FixtureSet;
use Nelmio\Alice\ObjectBag;
Expand Down
1 change: 0 additions & 1 deletion tests/Generator/DoublePassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Nelmio\Alice\Entity\StdClassFactory;
use Nelmio\Alice\FixtureBag;
use Nelmio\Alice\FixtureSet;
use Nelmio\Alice\Generator\Resolver\FixtureSet\FakeFixtureSetResolver;
use Nelmio\Alice\GeneratorInterface;
use Nelmio\Alice\ObjectBag;
use Nelmio\Alice\ObjectSet;
Expand Down
3 changes: 0 additions & 3 deletions tests/Generator/ObjectGenerator/SimpleObjectGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
use Nelmio\Alice\Definition\Fixture\SimpleFixture;
use Nelmio\Alice\Definition\Object\SimpleObject;
use Nelmio\Alice\Definition\SpecificationBagFactory;
use Nelmio\Alice\Generator\Caller\FakeCaller;
use Nelmio\Alice\Generator\CallerInterface;
use Nelmio\Alice\Generator\GenerationContext;
use Nelmio\Alice\Generator\Hydrator\FakeHydrator;
use Nelmio\Alice\Generator\HydratorInterface;
use Nelmio\Alice\Generator\Instantiator\FakeInstantiator;
use Nelmio\Alice\Generator\InstantiatorInterface;
use Nelmio\Alice\Generator\ObjectGeneratorInterface;
use Nelmio\Alice\Generator\ResolvedFixtureSetFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use Nelmio\Alice\FixtureSet;
use Nelmio\Alice\Generator\FixtureSetResolverInterface;
use Nelmio\Alice\Generator\ResolvedFixtureSet;
use Nelmio\Alice\Generator\Resolver\FakeFixtureBagResolver;
use Nelmio\Alice\Generator\Resolver\FakeParameterBagResolver;
use Nelmio\Alice\Generator\Resolver\FixtureBagResolverInterface;
use Nelmio\Alice\Generator\Resolver\ParameterBagResolverInterface;
use Nelmio\Alice\ObjectBag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Nelmio\Alice\Generator\Resolver\Parameter;

use Nelmio\Alice\Generator\Resolver\FakeParameterBagResolver;
use Nelmio\Alice\Generator\Resolver\ParameterBagResolverInterface;
use Nelmio\Alice\ParameterBag;
use PHPUnit\Framework\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Nelmio\Alice\Generator\Resolver\Parameter;

use Nelmio\Alice\Generator\Resolver\FakeParameterResolver;
use Nelmio\Alice\Generator\Resolver\ParameterBagResolverInterface;
use Nelmio\Alice\Generator\Resolver\ParameterResolverInterface;
use Nelmio\Alice\Generator\Resolver\ResolvingContext;
Expand Down
1 change: 0 additions & 1 deletion tests/IsAServiceTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Nelmio\Alice;

use Nelmio\Alice\Throwable\Exception\UnclonableException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Throwable;
Expand Down
Loading