Manoratra test PHP ho an’ny WordPress

Hitantsika atsy amin'ilay article iray atsy ny environnement de test an'ny mpanoratra. Ato isika dia hijery indray hoe toy ny ahoana no fanoratana test ho an'ireo fonctions rehetra ampiasaintsika.

Fichier de test

Araky ny configuration natao moa dia ao anatin'ilay repertoire test/ no apetratsika ireo fichier de tests, ary asiana prefixe test- mba ahafahan'i PHPUnit mahafantatra azy.

test-ato-daholo.php no ataontsika anarany, ho asesiko ao daholo ireo cas de test ho asehoko eto fa azonao sarahina fichier izy ireo, tsy misy olana.

tests/test-ato-daholo.php
/**
 * Mila manamboatra classe isika, manao extends WP_UnitTestCase. Azonao atao tsara ny mampiasa TestCase fotsiny, fa raha mampiasa code WordPress ianao vao tena mila ilay WP_UnitTestCase.
 */
class TestTambabe extends WP_UnitTestCase {

  /** Afaka manao declaration ana attibut ianao raha ilaina
   * private attribut_1;
   */
   
  public function test_fanaovana_test() {
    $this->assertTrue(true);
  }
} 
PHP

Io izany no modele ana fichier de test. Tsy hampianatra anao assertion aho fa mandehana mijerijery doc kely hoe inona avy no assertion azo ampiasaina. Mila fantatra fotsiny hoe ireny assertion ireny no test aminy.

Eto izao assertTrue(true), mijery izy hoe true tokoa ve ilay parametre omena azy. Eto moa true foana io dia PASS izany ilay test. Raha tsy izay dia FAIL ilay test.

Ohatra maromaro fonction atao test

functions.php
/**
 * Afenina ny Admin Bar raha tsy administrator
 */
function restrict_admin_bar() {
	return current_user_can( 'manage_options' ) ? true : false;
}
add_filter( 'show_admin_bar', 'restrict_admin_bar' );

/**
* Callback mampiseho page html
*/
function my_account_callback() {
  ob_start();
	if ( is_user_logged_in() ) {
		include plugin_dir_path( __FILE__ ) . 'templates/my-account.php';
	} else {
		include get_stylesheet_directory() . '/template/login.php';
	}
	return ob_get_clean();
}
add_shortcode( 'my_account', 'my_account_callback' );
PHP

test-functions.php
class TestTambabe extends WP_UnitTestCase {
  	public function test_restrict_admin_bar_aseho_administrators() {
  	// Ilay factory() manamboatra utilisateur iray ho an'ilay test.
		$admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
		wp_set_current_user( $admin_id );

		$this->assertTrue( restrict_admin_bar() );
	}

	public function test_restrict_admin_bar_afenina_ho_an_ny_hafa() {
		$subscriber_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
		wp_set_current_user( $subscriber_id );

		$this->assertFalse( restrict_admin_bar() );
	}
	
	public function test_ao_ve_ilay_shortcode() {
  	$this->assertTrue( shortcode_exists( 'my_account' ) );
	}
	
	public function test_my_account_callback_connecte() {
		wp_set_current_user( self::factory()->user->create() );

		$output = my_account_callback();

    //html moa no avoakan'ilay callback, dia manao test ianao izany hoe misy an'izao html izao ao
		$this->assertStringContainsString( '<span>Mon Compte</span>', $output );
	}

  
	public function test_my_account_callback_deconnecte() {

		$output = my_account_callback();

    //html an'ny login
		$this->assertStringContainsString( '<input type="email" placeholder="Email"/>', $output );
	}

}
PHP

Fehiny

Ita amin'izao moa fa betsaka kokoa ny test mila soratana ary noho ilay code mampandeha ilay app-nao. Fa aza kivy, mety ho lany fotoana be eo manoratra ilay test (na asaivo ataon'i claude) fa tena misaotra tena ianao raha sendra plugin de 5000 lignes mila atao verification hoe mbola mandeha ve après update.

Navigation

0 commentaire

Envoyer un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

<a href="https://mety-aby.com/author/nampoina/" target="_self">Nampoina Razafindralaisa</a>

Nampoina Razafindralaisa

Developpeur Web ny mpanoratra. Mba mizarazara ny hevitro fotsiny aho eto. Mamakia finaritra!