Source for file SwfCompilerTest.php

Documentation is available at SwfCompilerTest.php

  1. <?php
  2. require_once('../SwfCompiler.php');
  3.  
  4. class SwfCompilerTest extends UnitTestCase
  5. {
  6.     var $swfCompiler;
  7.     
  8.     function setUp()
  9.     {
  10.         $this->swfCompiler = new SwfCompiler();
  11.     }
  12.  
  13. /*
  14.     function testPush ()
  15.     {
  16.         //
  17.         // Strings
  18.         //
  19.         
  20.         // The string 'hello'
  21.         $this->assertPushIsCorrect('hello', '960D000064617461000068656C6C6F00');
  22.         
  23.         // Empty string
  24.         $this->assertPushIsCorrect('', '9608000064617461000000');
  25.         
  26.         // String longer than 255 characters (multi-byte length)
  27.         // Note: Max length of a string is 65535 bytes.
  28.         $data = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345';
  29.         $this->assertPushIsCorrect($data, '960801006461746100003031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343500');
  30.         
  31.         // Array [1,2,3]
  32.         $data = array(1, 2, 3);
  33.         $this->assertPushIsCorrect($data, '961A00006461746100070300000007020000000701000000070300000042');
  34.         
  35.         // Boolean
  36.         $data = true;
  37.         $this->assertPushIsCorrect($data, '9608000064617461000501');
  38.     }
  39. */    
  40.     
  41.     function testCreateSwf()
  42.     {
  43.         // Correct SWF bytecode that contains a single frame with the action data="hello" in it
  44.         /*
  45.         $correctSwfBytecode = '4657530631000000300A00A0000101004302FFFFFF3F0312000000960D000064617461000068656C6C6F001D0040000000';
  46.         
  47.         $testSwfBytecode = $this->swfCompiler->createSwf('hello');
  48.         $this->assertEqual($correctSwfBytecode, $testSwfBytecode);
  49.         */
  50.         
  51.         // TODO: Add an actual test for this that passes an array. 
  52.         
  53.     }
  54.     
  55.     
  56.     // Test generic data to bytecode 
  57.         function testDataToBytecode()
  58.     {
  59.         // 'hello'
  60.         $correctBytecode '0068656C6C6F00';
  61.         $str 'hello';
  62.         $this->assertEqual($correctBytecode$this->swfCompiler->dataToBytecode($str));
  63.         
  64.         // [1, 2, 3]
  65.         $correctBytecode '961400070300000007020000000701000000070300000042';
  66.         $testBytecode $this->swfCompiler->dataToBytecode(array(123));
  67.         $this->assertEqual($correctBytecode$testBytecode);
  68.         
  69.         // null
  70.         $this->assertEqual('02'$this->swfCompiler->dataToBytecode(null));
  71.         
  72.         // Double float (3.14)
  73.         // $correctBytecode = '06 B8 1E 09 40 20 85 EB 51'
  74.         
  75.         // TODO: Add tests for object, etc. 
  76.         
  77.     }
  78.     
  79.     // Test string to bytecode conversion
  80.         function testStringToBytecode()
  81.     {
  82.         // 'hello'
  83.         $correctBytecode '0068656C6C6F00';
  84.         $str 'hello';
  85.         $this->assertEqual($correctBytecode$this->swfCompiler->stringToBytecode($str));
  86.         
  87.         // Empty string
  88.         $testBytecode $this->swfCompiler->stringToBytecode('');
  89.         $this->assertEqual('0000'$testBytecode);
  90.         $this->assertEqual(4strlen($testBytecode));
  91.     }
  92.     
  93.     // Test boolean to bytecode conversion
  94.         function testBooleanToBytecode()
  95.     {
  96.         // True test
  97.         $this->assertEqual('0501'$this->swfCompiler->booleanToBytecode(true));
  98.         
  99.         // False test
  100.         $this->assertEqual('0500'$this->swfCompiler->booleanToBytecode(false));        
  101.     }
  102.     
  103.     // Test array to bytecode conversion
  104.         function testArrayToBytecode()
  105.     {
  106.         // [1, 2, 3]
  107.         $correctBytecode '961400070300000007020000000701000000070300000042';
  108.         
  109.         $testBytecode $this->swfCompiler->arrayToBytecode(array(123));
  110.         $this->assertEqual($correctBytecode$testBytecode);
  111.         
  112.         // error_log('SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS');
  113.         
  114.         /*
  115.         // data = [{x: 1, y: 2, z: 3}, {a: 4, b: 5, c: [true, false]}, [6, 7, {d: 8, e: 9}]];
  116.         // $correctBytecode = '961B0000646174610000640007080000000065000709000000070200000043960F0007070000000706000000070300000042961C000061000704000000006200070500000000630005000501070200000042960500070300000043961D0000780007010000000079000702000000007A000703000000070300000043960500070300000042';
  117.         $correctBytecode = '00640007080000000065000709000000070200000043960F0007070000000706000000070300000042961C000061000704000000006200070500000000630005000501070200000042960500070300000043961D0000780007010000000079000702000000007A000703000000070300000043960500070300000042';
  118.         $testData = array(array('x' => 1, 'y' => 2, 'z' => 3), array('a' => 4, 'b' => 5, 'c' => array(true, false)), array(6, 7, array('d' => 8, 'e' => 9)));
  119.         
  120.         $this->assertEqual($correctBytecode, $this->swfCompiler->arrayToBytecode($testData));
  121. */
  122.         //error_log('EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE');
  123.  
  124.         
  125.         // [{x: 1, y: 2, z: 3}]
  126.         /*
  127.         $correctBytecode = '962500006461746100050100780007010000000079000702000000007A000703000000070300000043960500070200000042';
  128.         
  129.         $testBytecode = $this->swfCompiler->arrayToBytecode(array(array('x'=>1, 'y'=>2, 'z'=>3)));
  130.         $this->assertEqual($correctBytecode, $testBytecode);
  131.         */
  132.     }
  133.     
  134.     
  135.     // Test integer to bytecode conversion
  136.         function testIntegerToBytecode()
  137.     {
  138.         $intToTest = 16711680;
  139.         $correctBytecode '070000FF00';
  140.         
  141.         $this->assertEqual($correctBytecode$this->swfCompiler->integerToBytecode($intToTest));
  142.     }
  143.     
  144.     // Test double to bytecode conversion
  145.     // Note: Not all float values generated by PHP and Flash will be 
  146.     // entirely identical but, in testing, the slight difference in the 
  147.     // hex representation isn't large enough to change the actual value. 
  148.     // (This may be proven wrong with additional testing but I'm assuming
  149.     // that it's a fact of life with floats.)
  150.         function testDoubleToBytecode()
  151.     {
  152.         $correctBytecode '06DD9ABFBF5F633937';
  153.         $floatToTest = -0.123456789;
  154.         
  155.         $this->assertEqual($correctBytecode$this->swfCompiler->doubleToBytecode($floatToTest));
  156.         
  157.     }
  158.     
  159.     
  160.     function testGetIntAsHex()
  161.     {
  162.         $intToTest = 16711680;
  163.         $numBytes = 8;
  164.         $correctString '0000FF0000000000';
  165.         
  166.         $this->assertEqual($correctString$this->swfCompiler->getIntAsHex($intToTest$numBytes));
  167.     }
  168.     
  169.     
  170.  
  171.     function testGetStringLengthInBytesHex()
  172.     {
  173.         // Single-byte length
  174.         $lengthInBytesHex $this->swfCompiler->getStringLengthInBytesHex('68656c6c6f'2);
  175.         $this->assertEqual('0500'$lengthInBytesHex);
  176.  
  177.         // Double-byte length
  178.         $lengthInBytesHex $this->swfCompiler->getStringLengthInBytesHex('006461746100003031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343500'2);    
  179.         $this->assertEqual('0801'$lengthInBytesHex);
  180.     }
  181.     
  182.     //
  183.     // Helper method tests
  184.     //
  185.  
  186.     
  187.     function testMakeLittleEndian()
  188.     {
  189.         // Single byte
  190.         $this->assertEqual('AB'$this->swfCompiler->makeLittleEndian('AB'));
  191.         
  192.         // Two bytes
  193.         $this->assertEqual('BBAA'$this->swfCompiler->makeLittleEndian('AABB'));    
  194.     }
  195.     
  196.     function testStrhex()
  197.     {
  198.         $this->assertEqual('68656c6c6f'$this->swfCompiler->strhex('hello'));
  199.     }
  200.     
  201.     function testHexStr()
  202.     {
  203.         $this->assertEqual('hello'$this->swfCompiler->hexstr('68656c6c6f'));
  204.     }
  205.     
  206.     //
  207.     // Helper methods
  208.     //
  209.     
  210.         
  211.     function assertPushIsCorrect($data$correctBytecode)
  212.     {
  213.         // Create the bytecode for the push statement 
  214.         // based on the passed $data.
  215.         $result $this->swfCompiler->push('data'$data);
  216.         
  217.         $this->assertEqual(strlen($result)strlen($correctBytecode));
  218.         $this->assertEqual($result$correctBytecode);        
  219.     }
  220. }
  221.  
  222. ?>

Documentation generated on Fri, 06 Jul 2007 19:54:53 +0100 by phpDocumentor 1.3.1