<commit_before><?php

namespace Doctrine\ODM\MongoDB\Tests;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

use Doctrine\Common\Collections\ArrayCollection;

class GH665Test extends BaseTest
{

    public function testUseAddToSetStrategyOnEmbeddedDocument()
    {
        $embedded = new GH665Embedded();
        $embedded->name = 'test';

        $document = new GH665Document();
        $document->embedded->add($embedded);
        $document->embeddedset->add($embedded);
        $this->dm->persist($document);
        $this->dm->flush();

        $check = $this->dm->getDocumentCollection(__NAMESPACE__ . '\GH665Document')
            ->findOne(array('embedded.name' => 'test'));
        $this->assertNotNull($check);

        $persisted = $this->dm->createQueryBuilder(__NAMESPACE__ . '\GH665Document')
            ->hydrate(false)
            ->field('id')->equals($document->id)
            ->getQuery()
            ->getSingleResult();

        $expected = array(
            '_id' => $document->id,
            'embedded' => array(
                array('name' => 'test')
                ),
            'embeddedset' => array(
                array('name' => 'test')
                )
            );
        $this->assertEquals($expected, $persisted);

        // the dot.notation should work.
        $check = $this->dm->getDocumentCollection(__NAMESPACE__ . '\GH665Document')
            ->findOne(array('embeddedset.name' => 'test'));
        $this->assertNotNull($check);

    }
}

/** @ODM\Document */
class GH665Document
{
    /** @ODM\Id */
    public $id;

    /**
      * @ODM\EmbedMany(targetDocument="GH665Embedded")
      */
    public $embedded;

    /**
      * @ODM\EmbedMany(targetDocument="GH665Embedded",strategy="addToSet")
      */
    public $embeddedset;

    public function __construct() {
        $this->embedded = new ArrayCollection();
        $this->embeddedset = new ArrayCollection();
    }
}

/** @ODM\EmbeddedDocument */
class GH665Embedded
{
    /** @ODM\String */
    public $name;
}



?><commit_msg>Clean up test case for #665<commit_after><?php

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

class GH665Test extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
    public function testUseAddToSetStrategyOnEmbeddedDocument()
    {
        $document = new GH665Document();
        $document->embeddedPushAll->add(new GH665Embedded('foo'));
        $document->embeddedAddToSet->add(new GH665Embedded('bar'));

        $this->dm->persist($document);
        $this->dm->flush();
        $this->dm->clear();

        $check = $this->dm->getDocumentCollection(__NAMESPACE__ . '\GH665Document')
            ->findOne(array('embeddedPushAll.name' => 'foo'));
        $this->assertNotNull($check);
        $this->assertSame($document->id, (string) $check['_id']);

        $check = $this->dm->getDocumentCollection(__NAMESPACE__ . '\GH665Document')
            ->findOne(array('embeddedAddToSet.name' => 'bar'));
        $this->assertNotNull($check);
        $this->assertSame($document->id, (string) $check['_id']);

        $persisted = $this->dm->createQueryBuilder(__NAMESPACE__ . '\GH665Document')
            ->hydrate(false)
            ->field('id')->equals($document->id)
            ->getQuery()
            ->getSingleResult();

        $expected = array(
            '_id' => $document->id,
            'embeddedPushAll' => array(array('name' => 'foo')),
            'embeddedAddToSet' => array(array('name' => 'bar'))
        );

        $this->assertEquals($expected, $persisted);
    }
}

/** @ODM\Document */
class GH665Document
{
    /** @ODM\Id */
    public $id;

    /** @ODM\EmbedMany(targetDocument="GH665Embedded", strategy="pushAll") */
    public $embeddedPushAll;

    /** @ODM\EmbedMany(targetDocument="GH665Embedded", strategy="addToSet") */
    public $embeddedAddToSet;

    public function __construct()
    {
        $this->embeddedPushAll = new ArrayCollection();
        $this->embeddedAddToSet = new ArrayCollection();
    }
}

/** @ODM\EmbeddedDocument */
class GH665Embedded
{
    /** @ODM\String */
    public $name;

    public function __construct($name)
    {
        $this->name = $name;
    }
}
{'fraction_non_alphanumeric': 0.16487873134328357, 'fraction_numerical': 0.015391791044776119, 'mean_word_length': 2.607232968881413, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 82, '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': '6614292', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1263, 'n_words': 194}