2012-02-21 22 views
7

私はphpunitでZend Frameworkのショッピングカート、チェックアウト、支払い処理をテストしています。私はShoppingCartControllerを商品に追加してテストしています。ShoppingCartモデルは、商品IDをZend Session Namespaceに保存して製品追加を処理し、別のテストで商品が追加されたことをテストしたいと思います。同じShoppingCartモデルは、同じZend Session名前空間変数から追加された製品のリストを取得します。phpunitのすべてのテストを通してセッションを保存するには?

アドオン製品のテストは次のようになりますし、うまく機能、およびvar_dump($_SESSION)がデバッグに加え、正しく製品を示しています

public function testCanAddProductsToShoppingCart() { 

    $testProducts = array(
     array(
      "product_id" => "1", 
      "product_quantity" => "5" 
     ), 
     array(
      "product_id" => "1", 
      "product_quantity" => "3" 
     ), 
     array(
      "product_id" => "2", 
      "product_quantity" => "1" 
     ) 
    ); 

    Ecommerce_Model_Shoppingcart::clean(); 

    foreach ($testProducts as $product) { 
     $this->request->setMethod('POST') 
       ->setPost(array(
        'product_id' => $product["product_id"], 
        'quantity' => $product["product_quantity"] 
       )); 

     $this->dispatch($this->getRouteUrl("add_to_shopping_cart")); 
     $this->assertResponseCode('200'); 
    } 

    $products = Ecommerce_Model_Shoppingcart::getData(); 
    $this->assertTrue($products[2][0]["product"] instanceof Ecommerce_Model_Product); 
    $this->assertEquals($products[2][0]["quantity"], 
      "8"); 

    $this->assertTrue($products[2][1]["product"] instanceof Ecommerce_Model_Product); 
    $this->assertEquals($products[2][1]["quantity"], 
      "1"); 

    var_dump($_SESSION); 
} 

第二の試験がそうするモデルを尋ねることによって製品を取得しようとしテスト開始時には既にvar_dump($_SESSION)がヌルです。 セッション変数がリセットされました。私はそれらを保持する方法を見つけたいと思います。間違った方向にあなたを指しているため

public function testCanDisplayShoppingCartWidget() { 
    var_dump($_SESSION); 
    $this->dispatch($this->getRouteUrl("view_shopping_mini_cart")); 
    $this->assertResponseCode('200'); 
} 

答えて

6

申し訳ありません。

<?php 

# running from the cli doesn't set $_SESSION here on phpunit trunk 
if (!isset($_SESSION)) $_SESSION = array(); 

class FooTest extends PHPUnit_Framework_TestCase { 
    protected $backupGlobalsBlacklist = array('_SESSION'); 

    public function testOne() { 
     $_SESSION['foo'] = 'bar'; 
    } 

    public function testTwo() { 
     $this->assertEquals('bar', $_SESSION['foo']); 
    } 

} 

?> 

== END UPDATE機能のティアダウンで

  1. ():$ _SESSIONをコピーするためにここにこれを達成するa way better wayで、irc.freenode.netの#phpunitチャネルからashawleyによって提案されました機能のセットアップではクラス属性と
  2. ()は:$ _SESSION
にクラス属性をコピーします

<?php 
# Usage: save this to test.php and run phpunit test.php  

# running from the cli doesn't set $_SESSION here on phpunit trunk                         
if (!isset($_SESSION)) $_SESSION = array(); 

class FooTest extends PHPUnit_Framework_TestCase { 
    public static $shared_session = array(); 

    public function setUp() { 
     $_SESSION = FooTest::$shared_session; 
    } 

    public function tearDown() { 

     FooTest::$shared_session = $_SESSION; 
    } 

    public function testOne() { 
     $_SESSION['foo'] = 'bar'; 
    } 

    public function testTwo() { 
     $this->assertEquals('bar', $_SESSION['foo']); 
    } 
} 

はまたbackupGlobals featureありますが、それは私のために動作しません:たとえば

は、このテストでは、関数のsetUp()およびtearDown()メソッドを削除すると失敗します。あなたはそれを考えてみるべきです、多分それは安定したPHPUnitで動作します。

+0

大丈夫、私はそれを試してみます – Daniel

+0

ありがとう、それは期待どおりに働いた! – Daniel

+0

セッションbeetweenテストケースを共有するにはどうすればよいですか? – Daniel

1

これは非常に醜いことです。正しい方法は依存性注入を使うべきです。

class Session 
{ 
    private $adapter; 
    public static function init(SessionAdapter $adapter) 
    { 
    self::$adapter = $adapter; 
    } 
    public static function get($var) 
    { 
     return self::$adapter->get($var); 
    } 
    public static function set($var, $value) 
    { 
    return self::$adapter->set($var, $value); 
    } 
} 

interface SessionAdapter 
{ 
    public function get($var); 
    public function set($var, $value); 
} 

追加情報:

0

のことができます。また、単に直接の代わりにセッションのこのクラスを使用するようにソースコードを変更することを意味し

ランダムなセッションIDを作成するあなたのPHPUnitテストのために、このセッションIDを、それ以降のすべての呼び出しでクッキーに渡すようにしてください。カールを使用すると、CURLOPT_COOKIEオプションを使用することになり、そのよう'PHPSESSID=thesessionidofyourunittest'に設定します。

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=thesessionidofyourunittest'); 

は私が詳細にかつthis stackoverflow answerの例でより多くを説明しました。

関連する問題