Показаны сообщения с ярлыком php. Показать все сообщения
Показаны сообщения с ярлыком php. Показать все сообщения

вторник, 15 мая 2012 г.

Very simple SimpleTest drupal example

It was done to check basics of drupal SimpleTest work.
Module part just created a menu page with some "Hello World" text.
Test should check:

  1. If drupal slice generated by SimpleText exists
  2. If menu page exists
  3. If menu page has a string "Lets test it!"
Module code:
1:  <?php  
2:    
3:  function alternative_example_menu() {  
4:   $items['alt_example'] = array(   
5:    'title' => 'Hello World test example',  
6:    'description' => 'Its easy and fun',  
7:        'page callback' => '_alt_example_basic_instructions',  
8:    'page arguments' => array(t('Hello World')),   
9:    'access callback' => TRUE,  
10:    'type' => MENU_NORMAL_ITEM,  
11:   );  
12:   return $items;  
13:   }  
14:     
15:   function _alt_example_basic_instructions($content = NULL) {  
16:   $base_content = t('Lets test it!');  
17:   return '<div>' . $base_content . '</div><br />';  
18:  }  

Test code:
1:  <?php  
2:    
3:  /**  
4:   * @file  
5:   * Tests for alternative example module.  
6:   */  
7:  class AlternativeExampleTestCase extends DrupalWebTestCase {  
8:   protected $custom_user;  
9:    
10:   public static function getInfo() {  
11:    return array(  
12:     'name' => 'Hello world example functionality',  
13:     'description' => 'Checks "Hello world" string and page',  
14:     'group' => 'Alternative Examples',  
15:    );  
16:   }  
17:    
18:   /**  
19:    * Enable modules and create user with specific permissions.  
20:    */  
21:   public function setUp() {  
22:    parent::setUp('alternative_example');  
23:   }  
24:   function testAlternativeHelloExample() {  
25:        // Load site  
26:    $this->drupalGet('');  
27:    // Load page  
28:    $this->clickLink(t('Hello World test example'));  
29:    // Look for string  
30:    $this->assertText(t('Lets test it!'));  
31:   }  
32:   }  

Web output:


Also there is possible to run SimpleTest through drush:

Nothing enormous. Tests should run faster if they will done like unit tests way but i should check that option next time.



воскресенье, 25 марта 2012 г.

array_rand function alternative


Strange thing happened with array_rand function after PHP 5.2.10 Release... I should paste it from official site:

"The resulting array of keys is no longer shuffled." 
C'mon i dont even understand now how it works. Anyway some good man make good and also elegant alternative, when you need to shuffle an array and take some entries from it.

 <?php  
 function array_random($arr, $num = 1) {  
   shuffle($arr);  
     
   $r = array();  
   for ($i = 0; $i < $num; $i++) {  
     $r[] = $arr[$i];  
   }  
   return $num == 1 ? $r[0] : $r;  
 }  
   
 $a = array("apple", "banana", "cherry");  
 print_r(array_random($a));  
 print_r(array_random($a, 2));  
 ?>