Ver Fonte

[ZF-8953] Zend_Log:

- added fluent interface

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22567 44c647ce-9c0f-0410-b52a-842ac1e357ba
ramon há 15 anos atrás
pai
commit
e926dc93ff

+ 6 - 1
library/Zend/Log.php

@@ -402,6 +402,7 @@ class Zend_Log
         }
 
         $this->_priorities[$priority] = $name;
+        return $this;
     }
 
     /**
@@ -429,6 +430,7 @@ class Zend_Log
         }
 
         $this->_filters[] = $filter;
+        return $this;
     }
 
     /**
@@ -454,6 +456,7 @@ class Zend_Log
         }
 
         $this->_writers[] = $writer;
+        return $this;
     }
 
     /**
@@ -463,8 +466,10 @@ class Zend_Log
      * @param  $value   Value of the field
      * @return void
      */
-    public function setEventItem($name, $value) {
+    public function setEventItem($name, $value)
+    {
         $this->_extras = array_merge($this->_extras, array($name => $value));
+        return $this;
     }
 
     /**

+ 2 - 0
library/Zend/Log/Writer/Abstract.php

@@ -63,6 +63,7 @@ abstract class Zend_Log_Writer_Abstract implements Zend_Log_FactoryInterface
         }
 
         $this->_filters[] = $filter;
+        return $this;
     }
 
     /**
@@ -92,6 +93,7 @@ abstract class Zend_Log_Writer_Abstract implements Zend_Log_FactoryInterface
     public function setFormatter(Zend_Log_Formatter_Interface $formatter)
     {
         $this->_formatter = $formatter;
+        return $this;
     }
 
     /**

+ 4 - 2
library/Zend/Log/Writer/Syslog.php

@@ -179,7 +179,7 @@ class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract
     public function setFacility($facility)
     {
         if ($this->_facility === $facility) {
-            return;
+            return $this;
         }
 
         if (!count($this->_validFacilities)) {
@@ -200,6 +200,7 @@ class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract
 
         $this->_facility = $facility;
         $this->_initializeSyslog();
+        return $this;
     }
 
     /**
@@ -211,10 +212,11 @@ class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract
     public function setApplicationName($application)
     {
         if ($this->_application === $application) {
-            return;
+            return $this;
         }
         $this->_application = $application;
         $this->_initializeSyslog();
+        return $this;
     }
 
     /**

+ 14 - 0
tests/Zend/Log/LogTest.php

@@ -400,6 +400,20 @@ class Zend_Log_LogTest extends PHPUnit_Framework_TestCase
             $this->assertRegExp('#^(Zend_Log_Filter_NotImplementsFilterInterface|The\sspecified\sfilter)#', $e->getMessage());
         }
     }
+
+    /**
+     * @group ZF-8953
+     */
+    public function testFluentInterface()
+    {
+        $logger   = new Zend_Log();
+        $instance = $logger->addPriority('all', 8)
+                           ->addFilter(1)
+                           ->addWriter(array('writerName' => 'Null'))
+                           ->setEventItem('os', PHP_OS);
+
+        $this->assertTrue($instance instanceof Zend_Log);
+    }
 }
 
 class Zend_Log_Writer_NotExtendedWriterAbstract implements Zend_Log_FactoryInterface

+ 12 - 0
tests/Zend/Log/Writer/AbstractTest.php

@@ -62,6 +62,18 @@ class Zend_Log_Writer_AbstractTest extends PHPUnit_Framework_TestCase
         $this->setExpectedException('Zend_Log_Exception');
         $this->_writer->addFilter(new StdClass());
     }
+
+    /**
+     * @group ZF-8953
+     */
+    public function testFluentInterface()
+    {
+        require_once 'Zend/Log/Formatter/Simple.php';
+        $instance = $this->_writer->addFilter(1)
+                                  ->setFormatter(new Zend_Log_Formatter_Simple());
+
+        $this->assertTrue($instance instanceof Zend_Log_Writer_AbstractTest_Concrete);
+    }
 }
 
 class Zend_Log_Writer_AbstractTest_Concrete extends Zend_Log_Writer_Abstract

+ 13 - 0
tests/Zend/Log/Writer/MailTest.php

@@ -274,6 +274,19 @@ class Zend_Log_Writer_MailTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-8953
+     */
+    public function testFluentInterface()
+    {
+        require_once 'Zend/Log/Formatter/Simple.php';
+        list(, $writer) = $this->_getSimpleLogger(true);
+        $instance = $writer->setLayoutFormatter(new Zend_Log_Formatter_Simple())
+                           ->setSubjectPrependText('subject');
+
+        $this->assertTrue($instance instanceof Zend_Log_Writer_Mail);
+    }
+
+    /**
      * Returns an array of the Zend_Mail mock object, Zend_Log_Writer_Mail
      * object, and Zend_Log objects.
      *

+ 12 - 0
tests/Zend/Log/Writer/SyslogTest.php

@@ -84,4 +84,16 @@ class Zend_Log_Writer_SyslogTest extends PHPUnit_Framework_TestCase
             $this->assertContains('Only LOG_USER is a valid', $e->getMessage());
         }
     }
+
+    /**
+     * @group ZF-8953
+     */
+    public function testFluentInterface()
+    {
+        $writer   = new Zend_Log_Writer_Syslog();
+        $instance = $writer->setFacility(LOG_USER)
+                           ->setApplicationName('my_app');
+
+        $this->assertTrue($instance instanceof Zend_Log_Writer_Syslog);
+    }
 }