PHP Classes

File: tests/PromiseTest.php

Recommend this page to a friend!
  Classes of Daniel   ohmy-auth   tests/PromiseTest.php   Download  
File: tests/PromiseTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: ohmy-auth
Obtain authorization to access APIs using OAuth
Author: By
Last change: Update of tests/PromiseTest.php
Date: 6 months ago
Size: 5,200 bytes
 

Contents

Class file image Download
<?php

/*
 * Copyright (c) 2014, Yahoo! Inc. All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */

use ohmy\Auth\Promise;

class
PromiseTest extends PHPUnit_Framework_TestCase {

    public function
setUp(){}
    public function
tearDown(){}

    public function
testStaticResolve() {
       
$phpunit = $this;
       
$promise = Promise::resolve(0);
       
$promise->then(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
        }, function(
$data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        });
    }

    public function
testStaticReject() {
       
$phpunit = $this;
       
$promise = Promise::reject(0);
       
$promise->then(function($data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        }, function(
$data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
        });
    }

    public function
testResolve() {

       
$phpunit = $this;

       
$promise = new Promise(function($resolve) {
           
$resolve(0);
        });

       
$promise->then(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
        }, function(
$data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        });

    }

    public function
testReject() {

       
$phpunit = $this;

       
$promise = new Promise(function($resolve, $reject) {
           
$reject(0);
        });

       
$promise->then(function($data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        }, function(
$data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
        });
    }

    public function
testCatchWithResolve() {

       
$phpunit = $this;

       
$promise = new Promise(function($resolve, $reject) {
           
$resolve(0);
        });

       
$promise->then(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
        }, function(
$data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        })
        ->
catch(function($data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        });

    }

    public function
testCatchWithReject() {

       
$phpunit = $this;

       
$promise = new Promise(function($resolve, $reject) {
           
$reject(0);
        });

       
$promise->then(function($data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        }, function(
$data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
            return
$data + 1;
        })
        ->
catch(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 1);
        });

    }

    public function
testValueAfterCatchWithResolve() {

       
$phpunit = $this;

       
$promise = new Promise(function($resolve, $reject) {
           
$resolve(0);
        });

       
$promise->then(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
            return
$data + 1;
        }, function(
$data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        })
        ->
catch(function($data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        })
        ->
then(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 1);
        });

    }

    public function
testValueAfterCatchWithReject() {

       
$phpunit = $this;

       
$promise = new Promise(function($resolve, $reject) {
           
$reject(0);
        });

       
$promise->then(function($data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        }, function(
$data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
            return
$data + 1;
        })
        ->
catch(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 1);
            return
$data + 1;
        })
        ->
then(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 2);
        });

    }

    public function
testFinallyAfterResolve() {

       
$phpunit = $this;

       
$promise = new Promise(function($resolve) {
           
$resolve(0);
        });

       
$promise->then(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
            return
$data + 1;
        }, function(
$data) {
           
$phpunit->fail('This should not run!');
        })
        ->
finally(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 1);
        });
    }

    public function
testFinallyAfterReject() {

       
$phpunit = $this;

       
$promise = new Promise(function($resolve, $reject) {
           
$reject(0);
        });

       
$promise->then(function($data) use($phpunit) {
           
$phpunit->fail('This should not run!');
        }, function(
$data) use($phpunit) {
           
$phpunit->assertEquals($data, 0);
            return
$data + 1;
        })
        ->
catch(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 1);
            return
$data + 1;
        })
        ->
finally(function($data) use($phpunit) {
           
$phpunit->assertEquals($data, 2);
        });
    }
}