Commits

Thomas Weinert  committed 2551b67

Tested: Carica\Io\Stream\File

  • Participants
  • Parent commits 49a2228

Comments (0)

Files changed (3)

File src/Carica/Io/Stream/File.php

     private $_resource = NULL;
     private $_listener = NULL;
 
+    /**
+     * Store filename and mode options
+     *
+     * @param string $filename
+     * @param string $mode
+     */
     public function __construct($filename, $mode = 'r') {
       $this->_filename = $filename;
       $this->_mode = $mode;
     }
 
+    /**
+     * On destruct close file resource
+     */
     public function __destruct() {
       $this->close();
     }
 
+    /**
+     * Read/Write the file resource, if it is an valid resource attach
+     * the an event listener to the loop, that calls read on new data
+     *
+     * @param string $resource
+     * @return NULL
+     */
     public function resource($resource = NULL) {
       if ($resource === FALSE) {
         $this->_resource = NULL;
       return NULL;
     }
 
+    /**
+     * Open file (nonblocking) and store resource
+     *
+     * @return boolean
+     */
     public function open() {
       if ($resource = @fopen($this->_filename, $this->_mode)) {
         stream_set_blocking($resource, 0);
       }
     }
 
+    /**
+     * Close file resource if it is opened
+     */
     public function close() {
-      if ($resource = $this->Resource()) {
+      if ($resource = $this->resource()) {
         fclose($resource);
       }
       $this->resource(FALSE);
     }
 
+    /**
+     * Read some bytes from the file resource
+     *
+     * @param number $bytes
+     * @return string|NULL
+     */
     public function read($bytes = 1024) {
-      if ($resource = $this->Resource()) {
+      if ($resource = $this->resource()) {
         $data = fread($resource, $bytes);
         if (is_string($data) && $data !== '') {
           $this->events()->emit('read-data', $data);
       return NULL;
     }
 
+    /**
+     * Write some bytes to the file resource
+     *
+     * @param unknown $data
+     */
     public function write($data) {
       if ($resource = $this->resource()) {
         fwrite(
           $writtenData = is_array($data) ? \Carica\Io\encodeBinaryFromArray($data) : $data
         );
         $this->events()->emit('write-data', $writtenData);
+        return TRUE;
       }
+      return FALSE;
     }
   }
 }

File tests/Carica/Io/Stream/FileTest.php

+<?php
+
+namespace Carica\Io\Stream {
+
+  include_once(__DIR__.'/../Bootstrap.php');
+
+  class FileTest extends \PHPUnit_Framework_TestCase {
+
+    /**
+     * @covers Carica\Io\Stream\File
+     */
+    public function testConstructor() {
+      $file = new File('sample.txt');
+      $this->assertAttributeEquals(
+        'sample.txt', '_filename', $file
+      );
+      $this->assertAttributeEquals(
+        'r', '_mode', $file
+      );
+    }
+
+    /**
+     * @covers Carica\Io\Stream\File
+     */
+    public function testOpen() {
+      $loop = $this->getMock('Carica\Io\Event\Loop');
+      $loop
+        ->expects($this->once())
+        ->method('setStreamReader')
+        ->with($this->isType('callable'), $this->isType('resource'));
+
+      $file = new File(__DIR__.'/TestData/sample.txt');
+      $file->loop($loop);
+      $this->assertTrue($file->open());
+      $this->assertInternalType('resource', $file->resource());
+    }
+
+    /**
+     * @covers Carica\Io\Stream\File
+     */
+    public function testOpenExpectingError() {
+      $events = $this
+        ->getMockBuilder('Carica\Io\Event\Emitter')
+        ->disableOriginalConstructor()
+        ->getMock();
+      $events
+        ->expects($this->once())
+        ->method('emit')
+        ->with('error', $this->stringStartsWith('Can not open file: '));
+
+      $file = new File(__DIR__.'/TestData/NON_EXISTING_FILE.txt');
+      $file->events($events);
+      $this->assertFalse($file->open());
+      $this->assertNull($file->resource());
+    }
+
+    /**
+     * @covers Carica\Io\Stream\File
+     */
+    public function testRead() {
+      $loop = $this->getMock('Carica\Io\Event\Loop');
+      $loop
+        ->expects($this->once())
+        ->method('setStreamReader')
+        ->with($this->isType('callable'), $this->isType('resource'));
+
+      $events = $this
+        ->getMockBuilder('Carica\Io\Event\Emitter')
+        ->disableOriginalConstructor()
+        ->getMock();
+      $events
+        ->expects($this->once())
+        ->method('emit')
+        ->with('read-data', 'Hello World!');
+
+      $file = new File(__DIR__.'/TestData/sample.txt');
+      $file->loop($loop);
+      $file->events($events);
+      $file->open();
+      $this->assertEquals('Hello World!', $file->read());
+    }
+
+    /**
+     * @covers Carica\Io\Stream\File
+     */
+    public function testReadWithoutResource() {
+      $file = new File(__DIR__.'/TestData/sample.txt');
+      $this->assertEquals('', $file->read());
+    }
+
+    /**
+     * @covers Carica\Io\Stream\File
+     */
+    public function testWriteWithoutResource() {
+      $file = new File(__DIR__.'/TestData/sample.txt');
+      $this->assertFalse($file->write('foobar'));
+    }
+  }
+}

File tests/Carica/Io/Stream/TestData/sample.txt

+Hello World!