<commit_before><?php

namespace Doctrine\ODM\MongoDB\Tests\Functional;

use Doctrine\ODM\MongoDB\MongoDBException;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Tests\BaseTest;

class RequireIndexesTest extends BaseTest
{
	public function setUp()
	{
		parent::setUp();
		$this->dm->getSchemaManager()->ensureDocumentIndexes('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument');
	}

	public function testIsIndexedTrue()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
			->field('indexed')->equals('test');
		$query = $qb->getQuery();
		$this->assertTrue($query->isIndexed());
	}

	public function testIsIndexedFalse()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
			->field('notIndexed')->equals('test');
		$query = $qb->getQuery();
		$this->assertFalse($query->isIndexed());

		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
			->field('indexed')->equals('test')
			->field('notIndexed')->equals('test');
		$query = $qb->getQuery();
		$this->assertFalse($query->isIndexed());
	}

	/**
	 * @expectedException Doctrine\ODM\MongoDB\MongoDBException
	 */
	public function testRequireIndexesThrowsExceptionOnExecute()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
			->field('notIndexed')->equals('test');
		$query = $qb->getQuery();
		$query->execute();
	}

	public function testRequireIndexesExceptionMessage()
	{
		try {
			$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
				->field('notIndexed')->equals('test');
			$query = $qb->getQuery();
			$query->execute();
		} catch (MongoDBException $e) {
			$this->assertEquals('Cannot execute unindexed queries on Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument. Unindexed fields: notIndexed', $e->getMessage());
		}
	}

	/**
	 * @expectedException Doctrine\ODM\MongoDB\MongoDBException
	 */
	public function testForceEnableRequireIndexes()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\DoesNotRequireIndexesDocument')
			->field('notIndexed')->equals('test')
			->requireIndexes();
		$query = $qb->getQuery();
		$query->execute();
	}

	public function testForceDisableRequireIndexes()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
			->field('notIndexed')->equals('test')
			->requireIndexes(false);
		$query = $qb->getQuery();
		$query->execute();
	}


	public function testRequireIndexesFalse()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\DoesNotRequireIndexesDocument')
			->field('notIndexed')->equals('test');
		$query = $qb->getQuery();
		$query->execute();
	}

	public function testRequireIndexesOnEmbeddedDocument()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
			->field('embedOne.indexed')->equals('test');
		$query = $qb->getQuery();
		$query->execute();
	}

	/**
	 * @expectedException Doctrine\ODM\MongoDB\MongoDBException
	 */
	public function testRequireIndexesOnEmbeddedDocumentThrowsException()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
			->field('embedOne.notIndexed')->equals('test');
		$query = $qb->getQuery();
		$query->execute();
	}

	/**
	 * @expectedException Doctrine\ODM\MongoDB\MongoDBException
	 */
	public function testRequireIndexesOnSortThrowException()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
			->sort('embedOne.notIndexed', 'asc');
		$query = $qb->getQuery();
		$query->execute();
	}

	public function testGetUnindexedFields()
	{
		$qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
			->field('embedOne.notIndexed')->equals('test')
			->field('notIndexed')->equals('test')
			->field('indexed')->equals('test');
		$query = $qb->getQuery();
		$this->assertEquals(array('embedOne.notIndexed', 'notIndexed'), $query->getUnindexedFields());
	}
}

/**
 * @ODM\Document(requireIndexes=true)
 */
class RequireIndexesDocument
{
	/** @ODM\Id */
	public $id;

	/** @ODM\String @ODM\Index */
	public $indexed;

	/** @ODM\String */
	public $notIndexed;

	/** @ODM\EmbedOne(targetDocument="Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesEmbeddedDocument") */
	public $embedOne;

	/** @ODM\EmbedMany(targetDocument="Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesEmbeddedDocument") */
	public $embedMany;
}

/**
 * @ODM\Document(requireIndexes=false)
 */
class DoesNotRequireIndexesDocument
{
	/** @ODM\Id */
	public $id;

	/** @ODM\String @ODM\Index */
	public $indexed;

	/** @ODM\String */
	public $notIndexed;
}

/** @ODM\EmbeddedDocument */
class RequireIndexesEmbeddedDocument
{
	/** @ODM\String @ODM\Index */
	public $indexed;

	/** @ODM\String */
	public $notIndexed;
}<commit_msg>Convert tabs to spaces. Whoops.<commit_after><?php

namespace Doctrine\ODM\MongoDB\Tests\Functional;

use Doctrine\ODM\MongoDB\MongoDBException;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Tests\BaseTest;

class RequireIndexesTest extends BaseTest
{
    public function setUp()
    {
        parent::setUp();
        $this->dm->getSchemaManager()->ensureDocumentIndexes('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument');
    }

    public function testIsIndexedTrue()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
            ->field('indexed')->equals('test');
        $query = $qb->getQuery();
        $this->assertTrue($query->isIndexed());
    }

    public function testIsIndexedFalse()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
            ->field('notIndexed')->equals('test');
        $query = $qb->getQuery();
        $this->assertFalse($query->isIndexed());

        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
            ->field('indexed')->equals('test')
            ->field('notIndexed')->equals('test');
        $query = $qb->getQuery();
        $this->assertFalse($query->isIndexed());
    }

    /**
     * @expectedException Doctrine\ODM\MongoDB\MongoDBException
     */
    public function testRequireIndexesThrowsExceptionOnExecute()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
            ->field('notIndexed')->equals('test');
        $query = $qb->getQuery();
        $query->execute();
    }

    public function testRequireIndexesExceptionMessage()
    {
        try {
            $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
                ->field('notIndexed')->equals('test');
            $query = $qb->getQuery();
            $query->execute();
        } catch (MongoDBException $e) {
            $this->assertEquals('Cannot execute unindexed queries on Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument. Unindexed fields: notIndexed', $e->getMessage());
        }
    }

    /**
     * @expectedException Doctrine\ODM\MongoDB\MongoDBException
     */
    public function testForceEnableRequireIndexes()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\DoesNotRequireIndexesDocument')
            ->field('notIndexed')->equals('test')
            ->requireIndexes();
        $query = $qb->getQuery();
        $query->execute();
    }

    public function testForceDisableRequireIndexes()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
            ->field('notIndexed')->equals('test')
            ->requireIndexes(false);
        $query = $qb->getQuery();
        $query->execute();
    }


    public function testRequireIndexesFalse()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\DoesNotRequireIndexesDocument')
            ->field('notIndexed')->equals('test');
        $query = $qb->getQuery();
        $query->execute();
    }

    public function testRequireIndexesOnEmbeddedDocument()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
            ->field('embedOne.indexed')->equals('test');
        $query = $qb->getQuery();
        $query->execute();
    }

    /**
     * @expectedException Doctrine\ODM\MongoDB\MongoDBException
     */
    public function testRequireIndexesOnEmbeddedDocumentThrowsException()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
            ->field('embedOne.notIndexed')->equals('test');
        $query = $qb->getQuery();
        $query->execute();
    }

    /**
     * @expectedException Doctrine\ODM\MongoDB\MongoDBException
     */
    public function testRequireIndexesOnSortThrowException()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
            ->sort('embedOne.notIndexed', 'asc');
        $query = $qb->getQuery();
        $query->execute();
    }

    public function testGetUnindexedFields()
    {
        $qb = $this->dm->createQueryBuilder('Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesDocument')
            ->field('embedOne.notIndexed')->equals('test')
            ->field('notIndexed')->equals('test')
            ->field('indexed')->equals('test');
        $query = $qb->getQuery();
        $this->assertEquals(array('embedOne.notIndexed', 'notIndexed'), $query->getUnindexedFields());
    }
}

/**
 * @ODM\Document(requireIndexes=true)
 */
class RequireIndexesDocument
{
    /** @ODM\Id */
    public $id;

    /** @ODM\String @ODM\Index */
    public $indexed;

    /** @ODM\String */
    public $notIndexed;

    /** @ODM\EmbedOne(targetDocument="Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesEmbeddedDocument") */
    public $embedOne;

    /** @ODM\EmbedMany(targetDocument="Doctrine\ODM\MongoDB\Tests\Functional\RequireIndexesEmbeddedDocument") */
    public $embedMany;
}

/**
 * @ODM\Document(requireIndexes=false)
 */
class DoesNotRequireIndexesDocument
{
    /** @ODM\Id */
    public $id;

    /** @ODM\String @ODM\Index */
    public $indexed;

    /** @ODM\String */
    public $notIndexed;
}

/** @ODM\EmbeddedDocument */
class RequireIndexesEmbeddedDocument
{
    /** @ODM\String @ODM\Index */
    public $indexed;

    /** @ODM\String */
    public $notIndexed;
}
{'fraction_non_alphanumeric': 0.16839762611275966, 'fraction_numerical': 0.0, 'mean_word_length': 3.8646820027063598, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 179, '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': '5107555', 'n_tokens_mistral': 3644, 'n_tokens_neox': 3495, 'n_words': 398}