<commit_before><?php
	declare(strict_types = 1);

	namespace Edde\Common\Storage;

	use Edde\Api\Container\IContainer;
	use Edde\Api\Crate\ICrateFactory;
	use Edde\Api\Schema\ISchemaManager;
	use Edde\Api\Storage\IStorage;
	use Edde\Common\Cache\CacheFactory;
	use Edde\Common\Container\Factory\FactoryFactory;
	use Edde\Common\Crate\Crate;
	use Edde\Common\Crate\CrateFactory;
	use Edde\Common\Crate\DummyCrateGenerator;
	use Edde\Common\Database\DatabaseStorage;
	use Edde\Common\File\TempDirectory;
	use Edde\Common\Query\Schema\CreateSchemaQuery;
	use Edde\Common\Query\Select\SelectQuery;
	use Edde\Common\Resource\ResourceManager;
	use Edde\Common\Schema\SchemaFactory;
	use Edde\Common\Schema\SchemaManager;
	use Edde\Ext\Cache\DevNullCacheStorage;
	use Edde\Ext\Container\ContainerFactory;
	use Edde\Ext\Database\Sqlite\SqliteDriver;
	use Edde\Ext\Resource\JsonResourceHandler;
	use phpunit\framework\TestCase;

	class StorageTest extends TestCase {
		/**
		 * @var IContainer
		 */
		protected $container;
		/**
		 * @var ISchemaManager
		 */
		protected $schemaManager;
		/**
		 * @var ICrateFactory
		 */
		protected $crateFactory;
		/**
		 * @var IStorage
		 */
		protected $storage;
		/**
		 * @var SqliteDriver
		 */
		protected $sqliteDriver;

		public function testSimpleStorable() {
			$crate = new Crate($this->crateFactory);
			$crate->setSchema($schema = $this->schemaManager->getSchema('Foo\\Bar\\SimpleStorable'));
			$this->storage->start();
			$this->storage->execute(new CreateSchemaQuery($schema));

			$crate->set('guid', $guid = sha1(random_bytes(64)));
			$crate->set('value', 'foobar');
			$this->storage->store($crate);

			$crate->set('guid', sha1(random_bytes(64)));
			$crate->set('value', 'barfoo');
			$this->storage->store($crate);

			$this->storage->commit();

			$query = new SelectQuery();
			$query->select()
				->all()
				->from()
				->source($schemaName = $schema->getSchemaName())
				->where()
				->eq()
				->property('guid')
				->parameter($guid);

			$crate = $this->storage->load(Crate::class, $query, $schemaName);
			self::assertEquals($guid, $crate->get('guid'));
			self::assertEquals('foobar', $crate->get('value'));
			$count = 0;
			foreach ($this->storage->collection(Crate::class, null, $schemaName) as $crate) {
				$count++;
			}
			self::assertEquals(2, $count);
		}

		public function testComplexStorable() {
			$groupSchema = $this->schemaManager->getSchema('Group');
			$identitySchema = $this->schemaManager->getSchema('Identity');
			$identityGroupSchema = $this->schemaManager->getSchema('IdentityGroup');

			$this->storage->start();
			$this->storage->execute(new CreateSchemaQuery($groupSchema));
			$this->storage->execute(new CreateSchemaQuery($identitySchema));
			$this->storage->execute(new CreateSchemaQuery($identityGroupSchema));

			$rootGroup = new Crate($this->crateFactory);
			$rootGroup->setSchema($groupSchema);
			$rootGroup->put([
				'guid' => sha1(random_bytes(64)),
				'name' => 'root',
			]);
			$this->storage->store($rootGroup);

			$guestGroup = new Crate($this->crateFactory);
			$guestGroup->setSchema($groupSchema);
			$guestGroup->put([
				'guid' => sha1(random_bytes(64)),
				'name' => 'guest',
			]);
			$this->storage->store($guestGroup);

			$godIdentity = new Crate($this->crateFactory);
			$godIdentity->setSchema($identitySchema);
			$godIdentity->put([
				'guid' => sha1(random_bytes(64)),
				'name' => 'The God',
			]);
			$this->storage->store($godIdentity);

			$guestIdentity = new Crate($this->crateFactory);
			$guestIdentity->setSchema($identitySchema);
			$guestIdentity->put([
				'guid' => sha1(random_bytes(64)),
				'name' => "The God's Guest",
			]);
			$this->storage->store($guestIdentity);

			$identityGroup = new Crate($this->crateFactory);
			$identityGroup->setSchema($identityGroupSchema);
			$identityGroup->put([
				'guid' => sha1(random_bytes(64)),
				'identity' => $godIdentity->get('guid'),
				'group' => $rootGroup->get('guid'),
			]);
			$this->storage->store($identityGroup);

			$identityGroup = new Crate($this->crateFactory);
			$identityGroup->setSchema($identityGroupSchema);
			$identityGroup->put([
				'guid' => sha1(random_bytes(64)),
				'identity' => $godIdentity->get('guid'),
				'group' => $guestGroup->get('guid'),
			]);
			$this->storage->store($identityGroup);

			$identityGroup = new Crate($this->crateFactory);
			$identityGroup->setSchema($identityGroupSchema);
			$identityGroup->put([
				'guid' => sha1(random_bytes(64)),
				'identity' => $guestIdentity->get('guid'),
				'group' => $guestGroup->get('guid'),
			]);
			$this->storage->store($identityGroup);

			$groupList = [];
			foreach ($this->storage->collectionTo($godIdentity, $identityGroupSchema, 'identity', 'group', Crate::class) as $storable) {<commit_msg>- updated test to use CrateFactory<commit_after><?php
	declare(strict_types = 1);

	namespace Edde\Common\Storage;

	use Edde\Api\Container\IContainer;
	use Edde\Api\Crate\ICrateFactory;
	use Edde\Api\Schema\ISchemaManager;
	use Edde\Api\Storage\IStorage;
	use Edde\Common\Cache\CacheFactory;
	use Edde\Common\Container\Factory\FactoryFactory;
	use Edde\Common\Crate\Crate;
	use Edde\Common\Crate\CrateFactory;
	use Edde\Common\Crate\DummyCrateGenerator;
	use Edde\Common\Database\DatabaseStorage;
	use Edde\Common\File\TempDirectory;
	use Edde\Common\Query\Schema\CreateSchemaQuery;
	use Edde\Common\Query\Select\SelectQuery;
	use Edde\Common\Resource\ResourceManager;
	use Edde\Common\Schema\SchemaFactory;
	use Edde\Common\Schema\SchemaManager;
	use Edde\Ext\Cache\DevNullCacheStorage;
	use Edde\Ext\Container\ContainerFactory;
	use Edde\Ext\Database\Sqlite\SqliteDriver;
	use Edde\Ext\Resource\JsonResourceHandler;
	use phpunit\framework\TestCase;

	class StorageTest extends TestCase {
		/**
		 * @var IContainer
		 */
		protected $container;
		/**
		 * @var ISchemaManager
		 */
		protected $schemaManager;
		/**
		 * @var ICrateFactory
		 */
		protected $crateFactory;
		/**
		 * @var IStorage
		 */
		protected $storage;
		/**
		 * @var SqliteDriver
		 */
		protected $sqliteDriver;

		public function testSimpleStorable() {
			$crate = new Crate($this->crateFactory);
			$crate->setSchema($schema = $this->schemaManager->getSchema('Foo\\Bar\\SimpleStorable'));
			$this->storage->start();
			$this->storage->execute(new CreateSchemaQuery($schema));

			$crate->set('guid', $guid = sha1(random_bytes(64)));
			$crate->set('value', 'foobar');
			$this->storage->store($crate);

			$crate->set('guid', sha1(random_bytes(64)));
			$crate->set('value', 'barfoo');
			$this->storage->store($crate);

			$this->storage->commit();

			$query = new SelectQuery();
			$query->select()
				->all()
				->from()
				->source($schemaName = $schema->getSchemaName())
				->where()
				->eq()
				->property('guid')
				->parameter($guid);

			$crate = $this->storage->load(Crate::class, $query, $schemaName);
			self::assertEquals($guid, $crate->get('guid'));
			self::assertEquals('foobar', $crate->get('value'));
			$count = 0;
			foreach ($this->storage->collection(Crate::class, null, $schemaName) as $crate) {
				$count++;
			}
			self::assertEquals(2, $count);
		}

		public function testComplexStorable() {
			$groupSchema = $this->schemaManager->getSchema('Group');
			$identitySchema = $this->schemaManager->getSchema('Identity');
			$identityGroupSchema = $this->schemaManager->getSchema('IdentityGroup');

			$this->storage->start();
			$this->storage->execute(new CreateSchemaQuery($groupSchema));
			$this->storage->execute(new CreateSchemaQuery($identitySchema));
			$this->storage->execute(new CreateSchemaQuery($identityGroupSchema));

			$rootGroup = $this->crateFactory->crate(Crate::class, null, 'Group');
			$rootGroup->put([
				'guid' => sha1(random_bytes(64)),
				'name' => 'root',
			]);
			$this->storage->store($rootGroup);

			$guestGroup = $this->crateFactory->crate(Crate::class, null, 'Group');
			$guestGroup->put([
				'guid' => sha1(random_bytes(64)),
				'name' => 'guest',
			]);
			$this->storage->store($guestGroup);

			$godIdentity = $this->crateFactory->crate(Crate::class, null, 'Identity');
			$godIdentity->put([
				'guid' => sha1(random_bytes(64)),
				'name' => 'The God',
			]);
			$this->storage->store($godIdentity);

			$guestIdentity = $this->crateFactory->crate(Crate::class, null, 'Identity');
			$guestIdentity->setSchema($identitySchema);
			$guestIdentity->put([
				'guid' => sha1(random_bytes(64)),
				'name' => "The God's Guest",
			]);
			$this->storage->store($guestIdentity);

			$identityGroup = $this->crateFactory->crate(Crate::class, null, 'IdentityGroup');
			$identityGroup->put([
				'guid' => sha1(random_bytes(64)),
				'identity' => $godIdentity->get('guid'),
				'group' => $rootGroup->get('guid'),
			]);
			$this->storage->store($identityGroup);

			$identityGroup = $this->crateFactory->crate(Crate::class, null, 'IdentityGroup');
			$identityGroup->setSchema($identityGroupSchema);
			$identityGroup->put([
				'guid' => sha1(random_bytes(64)),
				'identity' => $godIdentity->get('guid'),
				'group' => $guestGroup->get('guid'),
			]);
			$this->storage->store($identityGroup);

			$identityGroup = $this->crateFactory->crate(Crate::class, null, 'IdentityGroup');
			$identityGroup->setSchema($identityGroupSchema);
			$identityGroup->put([
				'guid' => sha1(random_bytes(64)),
				'identity' => $guestIdentity->get('guid'),
				'group' => $guestGroup->get('guid'),
			]);
			$this->storage->store($identityGroup);

			$groupList = [];
			foreach ($this->storage->collectionTo($godIdentity, $identityGroupSchema, 'identity', 'group', Crate::class) as $storable) {
{'fraction_non_alphanumeric': 0.18860286485364336, 'fraction_numerical': 0.006227942702927133, 'mean_word_length': 4.618075801749272, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 220, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6978150', 'n_tokens_mistral': 3626, 'n_tokens_neox': 3143, 'n_words': 466}