/** * @covers \SweetORM\Database\Query * @covers \SweetORM\Database\QueryGenerator * @covers \SweetORM\Entity * @covers \SweetORM\EntityManager */ public function testFindQueryBuilder() { Utilities::resetDatabase(); // Find All $all = Category::find()->all(); $this->assertCount(4, $all); foreach ($all as $single) { $this->assertInstanceOf(Category::class, $single); } // Try to find with wrong column try { Category::find()->where('nonexisting', 1)->all(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } // This MUST throw exceptions: try { // Invalid limit type Category::find()->limit("test")->one(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid offset type Category::find()->offset("test")->one(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid sort column Category::find()->where('id', '<>', null)->where('id', '<>', false)->sort(null)->all(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid order by type Category::find()->where('id', '<>', null)->where('id', '<>', false)->sort('id', 'falsfasdfasdf')->all(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid Operator Category::find()->where('id', 'SADSDASD', 1)->all(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid Mode Category::find()->insert()->into()->where('id', 'SADSDASD', 1)->all(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid Mode Category::find()->insert()->into()->sort('id')->apply(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid Mode Category::find()->insert()->into()->limit(1)->apply(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid Mode Category::find()->insert()->into()->offset(1)->apply(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid Mode Category::find()->insert()->into()->one(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } try { // Invalid joining types Category::find()->join(null, null, null)->one(); $this->assertTrue(false); } catch (QueryException $qe) { $this->assertTrue(true); } // END FAIL TESTS // Weird but good! $all = Category::find()->where('id', 'IS NOT', null)->where('id', '!=', false)->all(); $this->assertCount(4, $all); // Sorting $all = Category::find()->sort('description')->all(); $this->assertCount(4, $all); // Where IN $all = Category::find()->where('id', 'IN', array(1, 2))->all(); $this->assertCount(2, $all); // Joining $all = Category::find()->join('post', 'post.categoryid = category.id')->select('post.*, category.*')->where('post.authorid', '2')->asArray()->all(); $this->assertCount(3, $all); $all = Category::find()->join('post', 'pst.categoryid = category.id', 'JOIN', 'pst')->select('pst.*, category.*')->where('pst.authorid', '2')->asArray()->all(); $this->assertCount(3, $all); // Test with valid where, limit and offset in one $posts = Post::find()->where(array('categoryid' => '1'))->sort("title", "ASC")->limit(2)->offset(2)->all(); $this->assertCount(2, $posts); $this->assertEquals("Sample News #3", $posts[0]->title); $this->assertEquals("Sample News #4", $posts[1]->title); }