john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

phpunit json file kohana example

<?php
require_once 'kohana_bootstrap.php';
require_once "../modules/twilio/libraries/Twilio.php";

/*
 * Verify reading a json config file when creating Twilio objects
 */
class TwilioTest extends PHPUnit_Framework_TestCase
{
  protected $createdByTest = array();
  protected $expectedAccountSid = "EXAMPLE6c3d6c74ad9df18298fexample";
  protected $expectedApiVersion = "2008-08-01";
  protected $expectedAuthToken = "EXAMPLE7463f77f2dda6a1b67example";
  protected $expectedEndpoint = "https://api.twilio.com";


  function setUp() {
    // sudo or run as root in order to create these directories and files
    if( ! is_dir( '/project') ){
      mkdir( '/project');
      array_push( $this->createdByTest , '/project' );
    }
    if( ! is_dir( '/project/config') ){
      mkdir( '/project/config');
      array_push( $this->createdByTest , '/project/config' );
    }

    if( ! file_exists( '/project/config/twilio-credentials.json' ) ){
      $filename = "/project/config/twilio-credentials.json";
      $fp = fopen($filename, 'w');
      $arr = array();
      $arr[ 'account_sid' ] = $this->expectedAccountSid;
      $arr['api_version'] = $this->expectedApiVersion;
      $arr['auth_token'] = $this->expectedAuthToken;
      $arr[ 'endpoint' ] = $this->expectedEndpoint;


      fwrite($fp, json_encode( $arr , JSON_UNESCAPED_SLASHES));
      fclose($fp);

      array_push( $this->createdByTest , '/project/config/twilio-credentials.json' );
    }
  }

  function tearDown() {
    print "\n";
//    var_dump( $this->createdByTest);
    foreach( array_reverse( $this->createdByTest ) as $value)
    {
      if( is_file( $value )){
        print 'removing: ' . $value . "\n";
        unlink( $value );
      }
      if( is_dir( $value )){
        print 'removing: ' . $value . "\n";
        rmdir( $value );
      }

    }
  }


  /*
   * Twilio objects are configured via kohana
   */
  public function testTwilioConfig() {

    $t = new Twilio();      // $config = Kohana::config_load('twilio');  // config/twilio.php
    $this->assertEquals( $t->account_sid, $this->expectedAccountSid);
    $this->assertEquals( $t->api_version, $this->expectedApiVersion);
    $this->assertEquals( $t->auth_token, $this->expectedAuthToken);
    $this->assertEquals( $t->endpoint, $this->expectedEndpoint);
  }


}




<?php

/**
 * This is a bootstrap script for Kohana unit tests that need to autoload
 * models/libraries/etc.
 *
 * It's kind of like www/index.php and system/core/Bootstrap.php combined
 */

define('IN_PRODUCTION', FALSE);
define('EXT', '.php');
define('DOCROOT', getcwd().DIRECTORY_SEPARATOR);
define('KOHANA',  basename(__FILE__));
define('APPPATH', str_replace('\\', '/', realpath('../application')).'/');
define('MODPATH', str_replace('\\', '/', realpath('../modules')).'/');
define('SYSPATH', str_replace('\\', '/', realpath('../system')).'/');

require SYSPATH.'core/Benchmark'.EXT;
require SYSPATH.'core/utf8'.EXT;
require SYSPATH.'core/Event'.EXT;
require SYSPATH.'core/Kohana'.EXT;

Kohana::setup();

  • « s3 example curl
  • subprocess supress output errors devnull vmrun »

Published

Sep 25, 2013

Category

php

~198 words

Tags

  • example 36
  • file 92
  • json 3
  • kohana 2
  • php 82
  • phpunit 3