<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009 Xavier Perseguers <typo3@perseguers.ch>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/


/**
 * Testcase for class t3lib_sqlparser
 *
 * @author Xavier Perseguers <typo3@perseguers.ch>
 *
 * @package TYPO3
 * @subpackage t3lib
 */
class t3lib_sqlparser_testcase extends tx_phpunit_testcase {

        /**
         * @var t3lib_sqlparser (extended to make protected methods public)
         */
        protected $fixture;

        public function setUp() {
                $className = uniqid('t3lib_sqlparser');
                        // Class that makes protected methods public for testing purpose
                eval(
                        'class ' . $className . ' extends t3lib_sqlparser {
                                public function parseSELECT($parseString) {
                                        return parent::parseSELECT($parseString);
                                }
                                public function parseUPDATE($parseString) {
                                        return parent::parseUPDATE;
                                }
                                public function parseINSERT($parseString) {
                                        return parent::parseINSERT;
                                }
                                public function parseDELETE($parseString) {
                                        return parent::parseDELETE($parseString);
                                }
                                public function parseEXPLAIN($parseString) {
                                        return parent::parseEXPLAIN($parseString);
                                }
                                public function parseCREATETABLE($parseString) {
                                        return parent::parseCREATETABLE($parseString);
                                }
                                public function parseALTERTABLE($parseString) {
                                        return parent::parseALTERTABLE($parseString);
                                }
                                public function parseDROPTABLE($parseString) {
                                        return parent::parseDROPTABLE($parseString);
                                }
                                public function parseCREATEDATABASE($parseString) {
                                        return parent::parseCREATEDATABASE($parseString);
                                }
                                public function parseFieldList(&$parseString, $stopRegex = \'\') {
                                        return parent::parseFieldList($parseString, $stopRegex);
                                }

                                public function parseFromTables(&$parseString, $stopRegex = \'\') {
                                        return parent::parseFromTables($parseString, $stopRegex);
                                }
                                public function parseWhereClause(&$parseString, $stopRegex = \'\') {
                                        return parent::parseWhereClause($parseString, $stopRegex);
                                }
                                public function parseFieldDef(&$parseString, $stopRegex = \'\') {
                                        return parent::parseFieldDef($parseString, $stopRegex);
                                }
                                public function nextPart(&$parseString, $regex, $trimAll = FALSE) {
                                        return parent::nextPart($parseString, $regex, $trimAll);
                                }
                                public function getValue(&$parseString, $comparator = \'\') {
                                        return parent::getValue($parseString, $comparator);
                                }
                                public function getValueInQuotes(&$parseString, $quote) {
                                        return parent::getValueInQuotes($parseString, $quote);
                                }
                                public function parseStripslashes($str) {
                                        return parent::parseStripslashes($str);
                                }
                                public function compileAddslashes($str) {
                                        return parent::compileAddslashes($str);
                                }
                                public function parseError($msg, $restQuery) {
                                        return parent::parseError($msg, $restQuery);
                                }
                                public function trimSQL($str) {
                                        return parent::trimSQL($str);
                                }
                                public function compileSELECT($components) {
                                        return parent::compileSELECT($components);
                                }
                                public function compileUPDATE($components) {
                                        return parent::compileUPDATE($components);
                                }
                                public function compileINSERT($components) {
                                        return parent::compileINSERT($components);
                                }
                                public function compileDELETE($components) {
                                        return parent::compileDELETE($components);
                                }
                                public function compileCREATETABLE($components) {
                                        return parent::compileCREATETABLE($components);
                                }
                                public function compileALTERTABLE($components) {
                                        return parent::compileALTERTABLE($components);
                                }
                                public function compileFieldCfg($fieldCfg) {
                                        return parent::compileFieldCfg($fieldCfg);
                                }
                        }'
                );

                $this->fixture = new $className();
        }

        /**
         * Cleans a SQL query.
         *  
         * @param string $sql
         * @return string
         */
        private static function cleanSql($sql) {
                if (!is_string($sql)) {
                        return $sql;
                }

                $sql = str_replace("\n", ' ', $sql);
                $sql = preg_replace('/\s+/', ' ', $sql);
                return $sql;
        }

        /**
         * @test
         */
        public function testSelectAllFields() {
                $sql = 'SELECT * FROM pages';
                $expected = $sql;
                $actual = self::cleanSql($this->fixture->debug_testSQL($sql)); 

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

        /**
         * @test
         */
        public function testSelectInnerJoin() {
                $sql = 'SELECT pages.uid, be_users.username FROM be_users INNER JOIN pages ON pages.cruser_id = be_users.uid';
                $expected = 'SELECT pages.uid, be_users.username FROM be_users INNER JOIN pages ON pages.cruser_id=be_users.uid';
                $actual = self::cleanSql($this->fixture->debug_testSQL($sql)); 

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

        /**
         * @test
         */
        public function testSelectDoubleInnerJoin() {
                $sql = 'SELECT * FROM tt_news_cat INNER JOIN tt_news_cat_mm ON tt_news_cat.uid = tt_news_cat_mm.uid_foreign INNER JOIN tt_news ON tt_news.uid = tt_news_cat_mm.uid_local';
                $expected = 'SELECT * FROM tt_news_cat INNER JOIN tt_news_cat_mm ON tt_news_cat.uid=tt_news_cat_mm.uid_foreign INNER JOIN tt_news ON tt_news.uid=tt_news_cat_mm.uid_local';
                $actual = self::cleanSql($this->fixture->debug_testSQL($sql)); 

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

}
?>