<commit_before><?php

namespace Nextras\Orm\Tests\Entity\Fragments;

use Mockery;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Tests\TestCase;
use Tester\Assert;

$dic = require_once __DIR__ . '/../../../bootstrap.php';


class RepositoryEntityFragmentTestCase extends TestCase
{

	public function testAttach()
	{
		$bookRepository = Mockery::mock('Nextras\Orm\Repository\IRepository');

		/** @var IEntity $entity */
		$entity = Mockery::mock('Nextras\Orm\Entity\Fragments\RepositoryEntityFragment')->makePartial();
		$entity->fireEvent('onAttach', array($bookRepository));

		Assert::same($bookRepository, $entity->getRepository());
	}


	public function testDoubleAttach()
	{
		$bookRepository = Mockery::mock('Nextras\Orm\Repository\IRepository');

		/** @var IEntity $entity */
		$entity = Mockery::mock('Nextras\Orm\Entity\Fragments\RepositoryEntityFragment')->makePartial();
		$entity->fireEvent('onAttach', array($bookRepository));

		Assert::throws(function() use ($entity) {
			$entity->fireEvent('onAttach', [Mockery::mock('Nextras\Orm\Repository\IRepository')]);
		}, 'Nextras\Orm\InvalidStateException', 'Entity is already attached.');

		Assert::same($bookRepository, $entity->getRepository());
	}


	public function testAfterRemove()
	{
		$bookRepository = Mockery::mock('Nextras\Orm\Repository\IRepository');

		/** @var IEntity $entity */
		$entity = Mockery::mock('Nextras\Orm\Entity\Fragments\RepositoryEntityFragment')->makePartial();
		$entity->fireEvent('onAttach', array($bookRepository));
		Assert::same($bookRepository, $entity->getRepository());

		$entity->fireEvent('onAfterRemove');
		Assert::null($entity->getRepository(FALSE));
		Assert::throws(function() use ($entity) {
			$entity->getRepository();
		}, 'Nextras\Orm\InvalidStateException', 'Entity is not attached to repository.');
	}

}


$test = new RepositoryEntityFragmentTestCase($dic);
$test->run();<commit_msg>[tests] added cloning test for repository entity fragment<commit_after><?php

namespace Nextras\Orm\Tests\Entity\Fragments;

use Mockery;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Tests\TestCase;
use Tester\Assert;

$dic = require_once __DIR__ . '/../../../bootstrap.php';


class RepositoryEntityFragmentTestCase extends TestCase
{

	public function testAttach()
	{
		$repository = Mockery::mock('Nextras\Orm\Repository\IRepository');

		/** @var IEntity $entity */
		$entity = Mockery::mock('Nextras\Orm\Entity\Fragments\RepositoryEntityFragment')->makePartial();
		$entity->fireEvent('onAttach', [$repository]);

		Assert::same($repository, $entity->getRepository());
	}


	public function testDoubleAttach()
	{
		$repository = Mockery::mock('Nextras\Orm\Repository\IRepository');

		/** @var IEntity $entity */
		$entity = Mockery::mock('Nextras\Orm\Entity\Fragments\RepositoryEntityFragment')->makePartial();
		$entity->fireEvent('onAttach', [$repository]);

		Assert::throws(function() use ($entity) {
			$entity->fireEvent('onAttach', [Mockery::mock('Nextras\Orm\Repository\IRepository')]);
		}, 'Nextras\Orm\InvalidStateException', 'Entity is already attached.');

		Assert::same($repository, $entity->getRepository());
	}


	public function testAfterRemove()
	{
		$repository = Mockery::mock('Nextras\Orm\Repository\IRepository');

		/** @var IEntity $entity */
		$entity = Mockery::mock('Nextras\Orm\Entity\Fragments\RepositoryEntityFragment')->makePartial();
		$entity->fireEvent('onAttach', [$repository]);
		Assert::same($repository, $entity->getRepository());

		$entity->fireEvent('onAfterRemove');
		Assert::null($entity->getRepository(FALSE));
		Assert::throws(function() use ($entity) {
			$entity->getRepository();
		}, 'Nextras\Orm\InvalidStateException', 'Entity is not attached to repository.');
	}


	public function testCloning()
	{
		$cloned = NULL;

		/** @var IEntity $entity */
		$repository = Mockery::mock('Nextras\Orm\Repository\IRepository');
		$entity = Mockery::mock('Nextras\Orm\Entity\Fragments\RepositoryEntityFragment')->makePartial();
		$repository->shouldReceive('attach')->andReturnUsing(function(IEntity $entity) use (& $cloned) { $cloned = $entity; } );

		$entity->fireEvent('onAttach', [$repository]);
		$entity = clone $entity;

		Assert::same($entity, $cloned);
	}

}


$test = new RepositoryEntityFragmentTestCase($dic);
$test->run();
{'fraction_non_alphanumeric': 0.17567567567567569, 'fraction_numerical': 0.0, 'mean_word_length': 6.026186579378069, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '12511', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1314, 'n_words': 207}