Bravo3\Orm\Tests\RefTest::testDeleteIndicesAndRelationships PHP Method

testDeleteIndicesAndRelationships() public method

Use ref's to delete non-inversed relationships
public testDeleteIndicesAndRelationships ( EntityManager $em )
$em Bravo3\Orm\Services\EntityManager
    public function testDeleteIndicesAndRelationships(EntityManager $em)
    {
        $article1 = new RefArticle();
        $article1->setId(501)->setTitle('Ref Article 501');
        $category1 = new RefCategory();
        $category1->setId(532)->setName('Ref Category 532');
        $article1->setCanonicalCategory($category1);
        $em->persist($category1)->persist($article1)->flush();
        $this->assertTrue($this->exists($em, 'article', '501'));
        $key = $this->getRelKey($em, 'article', 'category', '501', 'canonical_category', RelationshipType::MANYTOONE());
        $this->assertEquals('532', $em->getDriver()->getSingleValueIndex($key));
        // Not inversed:
        $ikey = $this->getRelKey($em, 'category', 'article', '532', 'articles', RelationshipType::ONETOMANY());
        $this->assertFalse(in_array('501', $em->getDriver()->getMultiValueIndex($ikey)));
        // Ref exists:
        $refs = $em->getDriver()->getRefs($this->getEntityRefKey($em, 'category', '532'));
        $ref = (string) new Ref(RefArticle::class, '501', 'canonical_category');
        $this->assertCount(1, $refs);
        $this->assertEquals($ref, (string) $refs[0]);
        /** @var RefArticle $article */
        $article = $em->retrieve(RefArticle::class, 501);
        $em->delete($article)->flush();
        $this->assertFalse($this->exists($em, 'article', '501'));
        $mto = $this->getRelKey($em, 'article', 'category', '501', 'canonical_category', RelationshipType::MANYTOONE());
        $otm = $this->getRelKey($em, 'category', 'article', '532', 'articles', RelationshipType::ONETOMANY());
        $this->assertNull($em->getDriver()->getSingleValueIndex($mto));
        $this->assertNotContains('501', $em->getDriver()->getMultiValueIndex($otm));
        // Ref no longer needed:
        $refs = $em->getDriver()->getRefs($this->getEntityRefKey($em, 'category', '532'));
        $this->assertNotContains($ref, $refs);
    }