/** * @test * @depends it_handles_basic_retrieval_operations */ function it_takes_criteria_and_handles_basic_criteria_manipulation() { // clear all criteria, see if none are applied $this->repository->clearCriteria(); $this->assertTrue($this->repository->getCriteria()->isEmpty(), "getCriteria() not empty after clearCriteria()"); $this->assertRegExp("#^select \\* from [`\"]" . static::TABLE_NAME . "[`\"]\$#i", $this->repository->query()->toSql(), "Query SQL should be totally basic after clearCriteria()"); // add new criteria, see if it is applied $criteria = $this->makeMockCriteria($this->exactly(2), 'MockCriteria', function ($query) { return $query->where(self::UNIQUE_FIELD, '1337'); }); $this->repository->pushCriteria($criteria, 'TemporaryCriteria'); $this->assertCount(1, $this->repository->getCriteria(), "getCriteria() count incorrect after pushing new Criteria"); $this->assertRegExp("#where [`\"]" . self::UNIQUE_FIELD . "[`\"] =#i", $this->repository->query()->toSql(), "Query SQL should be altered by pushing Criteria"); // set repository to ignore criteria, see if they do not get applied $this->repository->ignoreCriteria(); $this->assertNotRegExp("#where [`\"]" . self::UNIQUE_FIELD . "[`\"] =#i", $this->repository->query()->toSql(), "Query SQL should be altered by pushing Criteria"); $this->repository->ignoreCriteria(false); // remove criteria once, see if it is not applied $this->repository->removeCriteriaOnce('TemporaryCriteria'); $this->assertCount(1, $this->repository->getCriteria(), "getCriteria() should still have a count of one if only removing temporarily"); $this->assertRegExp("#^select \\* from [`\"]" . static::TABLE_NAME . "[`\"]\$#i", $this->repository->query()->toSql(), "Query SQL should be totally basic while removing Criteria once"); $this->assertRegExp("#where [`\"]" . self::UNIQUE_FIELD . "[`\"] =#i", $this->repository->query()->toSql(), "Query SQL should be altered again on next call after removing Criteria once"); // override criteria once, see if it is overridden succesfully and not called $secondCriteria = $this->makeMockCriteria($this->exactly(1), 'SecondCriteria', function ($query) { return $query->where(self::SECOND_FIELD, '12345'); }); $this->repository->pushCriteriaOnce($secondCriteria, 'TemporaryCriteria'); $sql = $this->repository->query()->toSql(); $this->assertNotRegExp("#where [`\"]" . self::UNIQUE_FIELD . "[`\"] =#i", $sql, "Query SQL should not be built using first TemporaryCriteria"); $this->assertRegExp("#where [`\"]" . self::SECOND_FIELD . "[`\"] =#i", $sql, "Query SQL should be built using the overriding Criteria"); // remove specific criteria, see if it is not applied $this->repository->removeCriteria('TemporaryCriteria'); $this->assertTrue($this->repository->getCriteria()->isEmpty(), "getCriteria() not empty after removing Criteria"); $this->assertRegExp("#^select \\* from [`\"]" . static::TABLE_NAME . "[`\"]\$#i", $this->repository->query()->toSql(), "Query SQL should be totally basic after removing Criteria"); // override criteria once, see if it is changed $criteria = $this->makeMockCriteria($this->exactly(1), 'MockCriteria', function ($query) { return $query->where(self::UNIQUE_FIELD, '1337'); }); $this->repository->pushCriteriaOnce($criteria); $this->assertTrue($this->repository->getCriteria()->isEmpty(), "getCriteria() not empty with only once Criteria pushed"); $this->assertRegExp("#where [`\"]" . self::UNIQUE_FIELD . "[`\"] =#i", $this->repository->query()->toSql(), "Query SQL should be altered by pushing Criteria once"); }
/** * @test */ function with_relations_criteria_works() { $this->assertEmpty($this->repository->findBy(self::UNIQUE_FIELD, '1337')->getRelations(), "Model already includes translations relation without WithRelations Criteria"); $this->repository->pushCriteria(new WithRelations(['translations'])); $this->assertNotEmpty($this->repository->findBy(self::UNIQUE_FIELD, '1337')->getRelations(), "Model does not include translations relation with WithRelations Criteria"); }