Location: PHPKode > projects > Xyster Framework > xyster-02build01/xyster-02build01/tests/Xyster/Controller/Action/Helper/FileTest.php
<?php
/**
 * Xyster Framework
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * @category  Xyster
 * @package   Xyster_Controller
 * @subpackage   UnitTests
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   $Id: FileTest.php 418 2010-10-18 21:40:08Z jonathanhawk $
 */
namespace XysterTest\Controller\Action\Helper;
use Xyster\Controller\Action\Helper\File;
use Xyster\Controller\Action\Helper\Cache;
require_once dirname(dirname(dirname(__FILE__))) . '/_files/HelperCacheController.php';

/**
 * Test class for Xyster_Controller_Action_Helper_File.
 * Generated by PHPUnit on 2007-09-11 at 19:28:58.
 */
class FileTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \Zend_Controller_Action
     */
    public $controller;
    
    /**
     * @var \Zend_Controller_Front
     */
    public $front;

    /**
     * @var File
     */
    public $helper;

    /**
     * @var \Zend_Controller_Request_Http
     */
    public $request;

    /**
     * @var \Zend_Controller_Response_Cli
     */
    public $response;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     *
     */
    protected function setUp()
    {
        $object = new File;        
        $this->front = \Zend_Controller_Front::getInstance();
        $this->front->resetInstance();
        $this->front->returnResponse(true);
        $this->front->setControllerDirectory(dirname(dirname(dirname(__FILE__))) . '/_files');
        
        $this->request    = new \Zend_Controller_Request_Http();
        $this->response   = new \Zend_Controller_Response_Cli();
        
        \Zend_Controller_Action_HelperBroker::resetHelpers();
        \Zend_Controller_Action_HelperBroker::addHelper($object);
        \Zend_Controller_Action_HelperBroker::addHelper(new Cache());
        $this->controller = new \XysterTest\Controller\HelperCacheController($this->request, $this->response, array());
        $this->controller->setFrontController($this->front);
        $this->helper     = $this->controller->getHelper('File');
    }
    
    /**
     * Tests the setFileHeaders/direct method
     */
    public function testDirect()
    {
        $oldTime = strtotime('-2 hours');
        $oldDate = gmdate('D, d M Y H:i:s', $oldTime) . ' GMT';
        $_SERVER['HTTP_IF_MODIFIED_SINCE'] = $oldDate;
        
        $newTime = time();
        $newDate = gmdate('D, d M Y H:i:s', $newTime) . ' GMT';
        
        $this->helper->direct('myFileName.txt', 'text/plain', $newTime);
        
        $headers = $this->response->getHeaders();
        $rawHeaders = $this->response->getRawHeaders();
        
        $this->assertContains(Cache::CACHE_CONTROL_HEADER, $rawHeaders);
        $this->assertContains('Last-Modified: ' . $newDate, $rawHeaders);
        $this->assertContains(array('name'=>'Etag','value'=>md5($newDate), 'replace'=>false), $headers);
        $this->assertContains(array('name'=>'Accept-Ranges','value'=>'bytes', 'replace'=>false), $headers);
        $this->assertContains(array('name'=>'Content-Type','value'=>'text/plain', 'replace'=>false), $headers);
        $this->assertContains(array('name'=>'Content-Disposition','value'=>'attachment; filename="myFileName.txt"', 'replace'=>false), $headers);
        $this->assertNotEquals(304, $this->response->getHttpResponseCode());
    }
    
    /**
     * Tests the setFileHeaders/direct method if modified
     *
     */
    public function testDirectNotModified()
    {
        $time = strtotime('-2 hours');
        $date = gmdate('D, d M Y H:i:s', $time) . ' GMT';
        $_SERVER['HTTP_IF_MODIFIED_SINCE'] = $date;
        
        $this->helper->direct('myFileName.txt', 'text/plain', $time);
        
        $headers = $this->response->getHeaders();
        $rawHeaders = $this->response->getRawHeaders();
        
        $this->assertContains('Status: 304 Not Modified', $rawHeaders);
        $this->assertContains(array('name'=>'Content-Length', 'value'=>'0', 'replace'=>true), $headers);
        $this->assertEquals(304, $this->response->getHttpResponseCode());
        $this->assertTrue($this->front->getParam('noViewRenderer'));
    }
}
Return current item: Xyster Framework