Skip to content

Commit

Permalink
tests for mocked node
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelog committed Mar 24, 2012
1 parent 2cc6b88 commit a44bd33
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/examples/node/test_example.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
->onAnswer(true)
->onCreateNode('mainMenu')
->runWithInput('888')
->expectSaySound('pp/50', 2)
->assertSaySound('pp/50', 2)
->assertMaxInputAttemptsReached()
;
$pagiApp->init();
Expand Down
27 changes: 8 additions & 19 deletions src/mg/PAGI/Node/MockedNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MockedNode extends Node
*
* @return MockedNode
*/
public function expectSaySound($filename, $totalTimes)
public function assertSaySound($filename, $totalTimes)
{
$this->expectedSaySound[$filename] = $totalTimes;
$this->doneSaySound[$filename] = 0;
Expand Down Expand Up @@ -101,18 +101,6 @@ public function assertComplete()
return $this;
}

/**
* Assert that this node is in state of max input attempts reached
* after run().
*
* @return MockedNode
*/
public function assertTimeout()
{
$this->expectedState = Node::STATE_TIMEOUT;
return $this;
}

/**
* Assert that this node is in state of max input attempts reached
* after run().
Expand All @@ -134,7 +122,7 @@ public function assertMaxInputAttemptsReached()
*/
public function runWithInput($digits)
{
preg_match_all('/([0-9*# ])/', $digits, $matches);
preg_match_all('/([0-9*# X])/', $digits, $matches);
$this->mockedInput = $matches[1];
return $this;
}
Expand All @@ -147,13 +135,14 @@ public function run()
{
$result = parent::run();
foreach ($this->expectedSaySound as $filename => $times) {
if (!isset($this->doneSaySound[$filename])) {
throw new MockedException("$filename was never played");
}
if ($times != $this->doneSaySound[$filename]) {
$playedTimes = isset($this->doneSaySound[$filename])
? $this->doneSaySound[$filename]
: 0
;
if ($times != $playedTimes) {
throw new MockedException(
"$filename expected to be played $times times, was "
. "played {$this->doneSaySound[$filename]} times"
. "played $playedTimes times"
);
}
}
Expand Down
246 changes: 246 additions & 0 deletions test/mock/Test_Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
<?php
/**
* This class will test the node mock
*
* PHP Version 5
*
* @category Pagi
* @package Test
* @subpackage Mock
* @author Marcelo Gornstein <[email protected]>
* @license http://marcelog.github.com/ Apache License 2.0
* @version SVN: $Id$
* @link http://marcelog.github.com/
*
* Copyright 2011 Marcelo Gornstein <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use PAGI\Client\Impl\MockedClientImpl;
use PAGI\Node\MockedNode;
use PAGI\Node\Node;

/**
* This class will test the node mock
*
* PHP Version 5
*
* @category Pagi
* @package Test
* @subpackage Mock
* @author Marcelo Gornstein <[email protected]>
* @license http://marcelog.github.com/ Apache License 2.0
* @link http://marcelog.github.com/
*/
class Test_MockedNode extends PHPUnit_Framework_TestCase
{
private $properties = array();
private $client;

public function setUp()
{
$this->properties = array(
'log4php.properties' => RESOURCES_DIR . DIRECTORY_SEPARATOR . 'log4php.properties',
);
$this->client = new MockedClientImpl($this->properties);
}

/**
* @test
*/
public function can_skip_input_on_uninterruptable_message()
{
$this->client
->onCreateNode(__METHOD__)
->runWithInput('1')
;
$node = $this->client
->createNode(__METHOD__)
->unInterruptablePrompts()
->expectExactly(1)
->saySound('prompt')
->run()
;
}

/**
* @test
*/
public function can_consume_whitespace_as_no_input()
{
$this->client
->onCreateNode(__METHOD__)
->runWithInput(' ')
;
$node = $this->client
->createNode(__METHOD__)
->expectExactly(3)
->saySound('prompt')
->playOnNoInput('no-input')
->run()
;
}

/**
* @test
* @expectedException PAGI\Exception\MockedException
*/
public function can_assert_wrong_state()
{
$this->client
->onCreateNode(__METHOD__)
->assertCancelled()
->runWithInput('*')
;
$node = $this->client
->createNode(__METHOD__)
->expectExactly(1)
->run()
;
}

/**
* @test
* @expectedException PAGI\Exception\MockedException
*/
public function can_assert_sound_not_played()
{
$this->client
->onCreateNode(__METHOD__)
->assertSaySound('max-attempts', 1)
->assertSaySound('no-input', 1)
->assertSaySound('prompt', 99)
->assertMaxInputAttemptsReached()
->runWithInput('1')
;
$node = $this->client
->createNode(__METHOD__)
->expectExactly(1)
->validateInputWith(
'fails',
function (Node $node) {
return false;
},
'invalid'
)
->maxAttemptsForInput(3)
->saySound('prompt')
->playOnNoInput('no-input')
->playOnMaxValidInputAttempts('max-attempts')
->run()
;
}
/**
* @test
*/
public function can_assert_play_sound()
{
$this->client
->onCreateNode(__METHOD__)
->assertSaySound('max-attempts', 1)
->assertSaySound('no-input', 1)
->assertSaySound('prompt', 3)
->assertMaxInputAttemptsReached()
->runWithInput('1')
;
$node = $this->client
->createNode(__METHOD__)
->expectExactly(1)
->validateInputWith(
'fails',
function (Node $node) {
return false;
},
'invalid'
)
->maxAttemptsForInput(3)
->saySound('prompt')
->playOnNoInput('no-input')
->playOnMaxValidInputAttempts('max-attempts')
->run()
;
}
/**
* @test
*/
public function can_assert_max_attempts_reached()
{
$this->client
->onCreateNode(__METHOD__)
->assertMaxInputAttemptsReached()
->runWithInput('111')
;
$node = $this->client
->createNode(__METHOD__)
->expectExactly(1)
->validateInputWith('fails', function (Node $node) { return false; })
->maxAttemptsForInput(3)
->run()
;
}

/**
* @test
*/
public function can_assert_complete_node()
{
$this->client
->onCreateNode(__METHOD__)
->assertComplete()
->runWithInput('12345678901#')
;
$node = $this->client
->createNode(__METHOD__)
->expectAtLeast(10)
->endInputWith(Node::DTMF_HASH)
->expectAtMost(12)
->run()
;
}

/**
* @test
*/
public function can_assert_cancelled_node()
{
$this->client
->onCreateNode(__METHOD__)
->assertCancelled()
->runWithInput('*')
;
$node = $this->client
->createNode(__METHOD__)
->cancelWith(Node::DTMF_STAR)
->expectExactly(1)
->run()
;
}

/**
* @test
*/
public function can_skip_input_on_non_interrupt_digit_input()
{
$this->client
->onCreateNode(__METHOD__)
->runWithInput('X X')
;
$node = $this->client
->createNode(__METHOD__)
->expectExactly(1)
->saySound('prompt')
->run()
;
}
}

0 comments on commit a44bd33

Please sign in to comment.