Commit 53b05c1d authored by meus's avatar meus

added tests to get complete coverage of Transaction, RawSQL, AuditLog and...

added tests to get complete coverage of Transaction, RawSQL, AuditLog and Query/Having. Related refactorings. Added some getters to be able to test. Also added coverage report for these changes
parent 520f4a10
......@@ -18,7 +18,9 @@
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
Doctrine::autoload("Doctrine_Connection_Common");
/**
* Doctrine_Connection_Sqlite
*
......@@ -46,9 +48,7 @@ class Doctrine_Connection_Sqlite extends Doctrine_Connection_Common
*/
public function __construct(Doctrine_Manager $manager, $adapter)
{
$this->supported = array(
'sequences' => 'emulated',
$this->supported = array('sequences' => 'emulated',
'indexes' => true,
'affected_rows' => true,
'summary_functions' => true,
......@@ -67,13 +67,6 @@ class Doctrine_Connection_Sqlite extends Doctrine_Connection_Common
'identifier_quoting' => true,
'pattern_escaping' => false,
);
/**
$this->options['base_transaction_name'] = '___php_Doctrine_sqlite_auto_commit_off';
$this->options['fixed_float'] = 0;
$this->options['database_path'] = '';
$this->options['database_extension'] = '';
$this->options['server_version'] = '';
*/
parent::__construct($manager, $adapter);
if ($this->isConnected) {
......
......@@ -1267,4 +1267,14 @@ class Doctrine_Hydrate extends Doctrine_Locator_Injectable implements Serializab
{
return Doctrine_Lib::formatSql($this->getQuery());
}
/**
* Return the parts
*
* @return array The parts
*/
public function getParts()
{
return $this->parts;
}
}
......@@ -78,6 +78,7 @@ class Doctrine_Query_Having extends Doctrine_Query_Condition
}
}
/**
* load
* returns the parsed query part
......@@ -87,17 +88,11 @@ class Doctrine_Query_Having extends Doctrine_Query_Condition
*/
final public function load($having)
{
$e = Doctrine_Tokenizer::bracketExplode($having, ' ', '(', ')');
$r = array_shift($e);
$t = explode('(', $r);
$count = count($t);
$r = $this->parseAggregateFunction($r);
$operator = array_shift($e);
$value = implode(' ', $e);
$r .= ' ' . $operator . ' ' . $value;
return $r;
$tokens = Doctrine_Tokenizer::bracketExplode($having, ' ', '(', ')');
$part = $this->parseAggregateFunction(array_shift($tokens));
$operator = array_shift($tokens);
$value = implode(' ', $tokens);
$part .= ' ' . $operator . ' ' . $value;
return $part;
}
}
......@@ -51,20 +51,31 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
public function parseQueryPart($queryPartName, $queryPart, $append = false)
{
if ($queryPartName == 'select') {
preg_match_all('/{([^}{]*)}/U', $queryPart, $m);
$this->parseSelectFields($queryPart);
return $this;
}
if( ! isset($this->parts[$queryPartName])) {
$this->parts[$queryPartName] = array();
}
$this->fields = $m[1];
$this->parts['select'] = array();
} else {
if ( ! $append) {
if (! $append) {
$this->parts[$queryPartName] = array($queryPart);
} else {
}else {
$this->parts[$queryPartName][] = $queryPart;
}
}
return $this;
}
/**
* Add select parts to fields
*
* @param $queryPart sting The name of the querypart
*/
private function parseSelectFields($queryPart){
preg_match_all('/{([^}{]*)}/U', $queryPart, $m);
$this->fields = $m[1];
$this->parts['select'] = array();
}
/**
* parseQuery
* parses an sql query and adds the parts to internal array
......@@ -74,46 +85,48 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
*/
public function parseQuery($query)
{
preg_match_all('/{([^}{]*)}/U', $query, $m);
$this->fields = $m[1];
$this->parseSelectFields($query);
$this->clear();
$e = Doctrine_Tokenizer::sqlExplode($query,' ');
$tokens = Doctrine_Tokenizer::sqlExplode($query,' ');
foreach ($e as $k => $part) {
$low = strtolower($part);
switch (strtolower($part)) {
$parts = array();
foreach ($tokens as $key => $part) {
$partLowerCase = strtolower($part);
switch ($partLowerCase) {
case 'select':
case 'from':
case 'where':
case 'limit':
case 'offset':
case 'having':
$p = $low;
if ( ! isset($parts[$low])) {
$parts[$low] = array();
$type = $partLowerCase;
if ( ! isset($parts[$partLowerCase])) {
$parts[$partLowerCase] = array();
}
break;
case 'order':
case 'group':
$i = ($k + 1);
if (isset($e[$i]) && strtolower($e[$i]) === 'by') {
$p = $low;
$p .= 'by';
$parts[$low . 'by'] = array();
$i = $key + 1;
if (isset($tokens[$i]) && strtolower($tokens[$i]) === 'by') {
$type = $partLowerCase . 'by';
$parts[$type] = array();
} else {
$parts[$p][] = $part;
//not a keyword so we add it to the previous type
$parts[$type][] = $part;
}
break;
case 'by':
continue;
default:
if ( ! isset($parts[$p][0])) {
$parts[$p][0] = $part;
//not a keyword so we add it to the previous type.
if ( ! isset($parts[$type][0])) {
$parts[$type][0] = $part;
} else {
$parts[$p][0] .= ' '.$part;
// why does this add to index 0 and not append to the
// array. If it had done that one could have used
// parseQueryPart.
$parts[$type][0] .= ' '.$part;
}
}
}
......
......@@ -125,6 +125,16 @@ class Doctrine_Transaction extends Doctrine_Connection_Module
return true;
}
/**
* Return the invalid records
*
* @return array An array of invalid records
*/
public function getInvalid(){
return $this->invalid;
}
/**
* getTransactionLevel
* get the current transaction nesting level
......
......@@ -90,4 +90,12 @@ class Doctrine_AuditLog_TestCase extends Doctrine_UnitTestCase
$this->pass();
}
}
public function testReturnFalseIfVersionTableExists()
{
$entity = new VersioningTest();
$entity_table = $entity->getTable();
$auditLog = new Doctrine_AuditLog(array("table" => $entity_table));
$this->assertFalse($auditLog->buildDefinition($entity_table));
}
}
......@@ -56,4 +56,12 @@ class Doctrine_Query_Having_TestCase extends Doctrine_UnitTestCase
$q->parseQuery("SELECT CONCAT(u.name, u.loginname) name FROM User u LEFT JOIN u.Phonenumber p HAVING name = 'xx'");
}
public function testReturnFuncIfNumeric()
{
$having = new Doctrine_Query_Having("test");
$part = $having->load("1");
$this->assertEqual("1",trim($part));
}
}
......@@ -33,7 +33,6 @@
*/
class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
{
public function testQueryParser()
{
$sql = 'SELECT {p.*} FROM photos p';
......@@ -248,4 +247,47 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$this->assertEqual(count($coll), 3);
}
public function testParseQueryPartShouldAddPartIfNotSelectAndAppend()
{
$query = new Doctrine_Rawsql();
$query->parseQueryPart("test", "test", true);
$parts = $query->getParts();
$this->assertTrue(isset($parts["test"]));
$this->assertTrue(is_array($parts["test"]));
$this->assertTrue(isset($parts["test"][0]));
$this->assertEqual("test", $parts["test"][0]);
}
public function testParseQueryShouldExtractGroupBy()
{
$query = new Doctrine_RawSql();
$query->parseQuery("having group");
$parts = $query->getParts();
$this->assertEqual($parts["having"][0], "group");
}
public function testThrowExceptionIfFieldNameIsOnWrongForm()
{
$query = new Doctrine_RawSql();
$query->parseQueryPart("select", "{test}");
try{
$query->getQuery();
$this->fail("Should throw exception");
}catch(Doctrine_RawSql_Exception $exception){
$this->assertEqual($exception->getMessage(), "All selected fields in Sql query must be in format tableAlias.fieldName");
}
}
public function testThrowExceptionIfAliasDoesNotExist()
{
$query = new Doctrine_RawSql();
$query->parseQueryPart("select", "{test.test}");
try{
$query->getQuery();
$this->fail("Should throw exception");
}catch(Doctrine_RawSql_Exception $exception){
$this->assertEqual($exception->getMessage(), "The associated component for table alias test couldn't be found.");
}
}
}
......@@ -224,6 +224,15 @@ class Doctrine_Transaction_TestCase extends Doctrine_UnitTestCase
$this->assertTrue($phonenumber->id > 0);
}
public function testAddDuplicateRecordToTransactionShouldSkipSecond(){
$transaction = new Doctrine_Transaction();
$user = new User();
$transaction->addInvalid($user);
$this->assertEqual(1, count($transaction->getInvalid()));
$transaction->addInvalid($user);
$this->assertEqual(1, count($transaction->getInvalid()));
}
}
class TransactionListener extends Doctrine_EventListener
{
......
......@@ -247,7 +247,7 @@
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(class_exists($className))&nbsp;{
</td></tr>
<tr><td>80</td>
<td class="red">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false;
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false;
</td></tr>
<tr><td>81</td>
<td class="orange">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
......
......@@ -3817,6 +3817,36 @@
<td class="orange">&nbsp;&nbsp;&nbsp;&nbsp;}
</td></tr>
<tr><td>1270</td>
<td class="normal">
</td></tr>
<tr><td>1271</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;/**
</td></tr>
<tr><td>1272</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Return&nbsp;the&nbsp;parts
</td></tr>
<tr><td>1273</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
</td></tr>
<tr><td>1274</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;array&nbsp;The&nbsp;parts
</td></tr>
<tr><td>1275</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
</td></tr>
<tr><td>1276</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;getParts()
</td></tr>
<tr><td>1277</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;{
</td></tr>
<tr><td>1278</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this-&gt;parts;
</td></tr>
<tr><td>1279</td>
<td class="orange">&nbsp;&nbsp;&nbsp;&nbsp;}
</td></tr>
<tr><td>1280</td>
<td class="normal">}
</td></tr>
</table></body></html>
\ No newline at end of file
This diff is collapsed.
......@@ -343,5 +343,6 @@
<td class="orange">&nbsp;&nbsp;&nbsp;&nbsp;}
</td></tr>
<tr><td>112</td>
<td class="covered">}</td></tr>
<td class="normal">}
</td></tr>
</table></body></html>
\ No newline at end of file
......@@ -235,7 +235,7 @@
<td class="orange">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
</td></tr>
<tr><td>76</td>
<td class="red">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$func;
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$func;
</td></tr>
<tr><td>77</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
......@@ -250,71 +250,57 @@
<td class="normal">
</td></tr>
<tr><td>81</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;/**
<td class="normal">
</td></tr>
<tr><td>82</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;load
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;/**
</td></tr>
<tr><td>83</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;returns&nbsp;the&nbsp;parsed&nbsp;query&nbsp;part
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;load
</td></tr>
<tr><td>84</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;returns&nbsp;the&nbsp;parsed&nbsp;query&nbsp;part
</td></tr>
<tr><td>85</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;string&nbsp;$having
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
</td></tr>
<tr><td>86</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;string
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;string&nbsp;$having
</td></tr>
<tr><td>87</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;string
</td></tr>
<tr><td>88</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;final&nbsp;public&nbsp;function&nbsp;load($having)
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
</td></tr>
<tr><td>89</td>
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;{
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;final&nbsp;public&nbsp;function&nbsp;load($having)
</td></tr>
<tr><td>90</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$e&nbsp;=&nbsp;Doctrine_Tokenizer::bracketExplode($having,&nbsp;'&nbsp;',&nbsp;'(',&nbsp;')');
<td class="normal">&nbsp;&nbsp;&nbsp;&nbsp;{
</td></tr>
<tr><td>91</td>
<td class="normal">
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$tokens&nbsp;=&nbsp;Doctrine_Tokenizer::bracketExplode($having,&nbsp;'&nbsp;',&nbsp;'(',&nbsp;')');
</td></tr>
<tr><td>92</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r&nbsp;=&nbsp;array_shift($e);
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$part&nbsp;=&nbsp;$this-&gt;parseAggregateFunction(array_shift($tokens));
</td></tr>
<tr><td>93</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$t&nbsp;=&nbsp;explode('(',&nbsp;$r);
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$operator&nbsp;&nbsp;=&nbsp;array_shift($tokens);
</td></tr>
<tr><td>94</td>
<td class="normal">
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;implode('&nbsp;',&nbsp;$tokens);
</td></tr>
<tr><td>95</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$count&nbsp;=&nbsp;count($t);
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$part&nbsp;.=&nbsp;'&nbsp;'&nbsp;.&nbsp;$operator&nbsp;.&nbsp;'&nbsp;'&nbsp;.&nbsp;$value;
</td></tr>
<tr><td>96</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r&nbsp;=&nbsp;$this-&gt;parseAggregateFunction($r);
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$part;
</td></tr>
<tr><td>97</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$operator&nbsp;&nbsp;=&nbsp;array_shift($e);
<td class="orange">&nbsp;&nbsp;&nbsp;&nbsp;}
</td></tr>
<tr><td>98</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;implode('&nbsp;',&nbsp;$e);
</td></tr>
<tr><td>99</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r&nbsp;.=&nbsp;'&nbsp;'&nbsp;.&nbsp;$operator&nbsp;.&nbsp;'&nbsp;'&nbsp;.&nbsp;$value;
</td></tr>
<tr><td>100</td>
<td class="normal">
</td></tr>
<tr><td>101</td>
<td class="covered">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$r;
</td></tr>
<tr><td>102</td>
<td class="orange">&nbsp;&nbsp;&nbsp;&nbsp;}
<td class="normal">}
</td></tr>
<tr><td>103</td>
<td class="covered">}</td></tr>
</table></body></html>
\ No newline at end of file
......@@ -172,5 +172,6 @@
<td class="orange">&nbsp;&nbsp;&nbsp;&nbsp;}
</td></tr>
<tr><td>55</td>
<td class="covered">}</td></tr>
<td class="normal">}
</td></tr>
</table></body></html>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
......@@ -973,5 +973,6 @@
<td class="orange">&nbsp;&nbsp;&nbsp;&nbsp;}
</td></tr>
<tr><td>322</td>
<td class="covered">}</td></tr>
<td class="normal">}
</td></tr>
</table></body></html>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment