Skip to content

Commit

Permalink
Cas is not anymore static, add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
trecouvr committed Oct 4, 2013
1 parent f397c7e commit d97f291
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 16 deletions.
3 changes: 2 additions & 1 deletion class/ServiceBase.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use \Payutc\Exception\UserError;
use \Payutc\Bom\User;
use \Payutc\Log;
use \Payutc\Config;

/**
* ServiceBase.class
Expand Down Expand Up @@ -125,7 +126,7 @@ public function logout() {
* @return String $url
*/
public function getCasUrl() {
return Cas::getUrl();
return Config::get('cas_url');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Payutc/Bom/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,9 @@ public function decCredit($val) {
public static function getUserFromCas($ticket, $service) {
Log::debug("User: getUserFromCas($ticket, $service)");

$cas = new Cas(Config::get('cas_url'));
try {
$login = Cas::authenticate($ticket, $service);
$login = $cas->authenticate($ticket, $service);
}
catch (AuthenticationFailure $e) {
throw new LoginError("Impossible de valider le ticket CAS fourni");
Expand Down
34 changes: 23 additions & 11 deletions src/Payutc/Cas.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,29 @@

class Cas
{
public static function authenticate($ticket,$service)
protected $url;
protected $timeout;

public function __construct($url, $timeout=10)
{
$this->url = $url;
$this->timeout = $timeout;
}

public function authenticate($ticket, $service)
{
$r = Request::get(self::getValidateUrl($ticket, $service))
$r = Request::get($this->getValidateUrl($ticket, $service))
->sendsXml()
->timeoutIn($this->timeout)
->send();
$r->body = str_replace("\n", "", $r->body);
$xml = new SimpleXMLElement($r->body);
try {
$xml = new SimpleXMLElement($r->body);
}
catch (\Exception $e) {
throw new \UnexpectedValueException("Return cannot be parsed : '{$r->body}'");
}

$namespaces = $xml->getNamespaces();

$serviceResponse = $xml->children($namespaces['cas']);
Expand All @@ -29,23 +45,19 @@ public static function authenticate($ticket,$service)
if ($authFailed) {
$attributes = $authFailed->attributes();
Log::warning("AuthenticationFailure : ".$attributes['code']." ($ticket, $service)");
throw new AuthenticationFailure("AuthenticationFailure: ".$attributes['code']);
throw new AuthenticationFailure("".$attributes['code']);
}
else {
Log::error("Cas return is weird : '{$r->body}'");
throw new UnexpectedValueException($r->body);
throw new \UnexpectedValueException($r->body);
}
}
// never reach there
}

public static function getURl() {
return Config::get('cas_url');
}

public static function getValidateUrl($ticket, $service)
public function getValidateUrl($ticket, $service)
{
return self::getURl()."serviceValidate?ticket=".$ticket."&service=".$service;
return $this->url."serviceValidate?ticket=".$ticket."&service=".$service;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Payutc/Exception/AuthenticationFailure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php namespace Payutc\Exception;

class AuthenticationFailure extends PayutcException {};
3 changes: 2 additions & 1 deletion src/Payutc/Service/AADMIN.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use \ComplexData;
use \Payutc\Db\DbBuckutt;
use \PlageHoraire;
use \Payutc\Config;

/**
* AAdmin.class
Expand Down Expand Up @@ -65,7 +66,7 @@ protected function getRemoteIp() {
* @return String $url
*/
public function getCasUrl() {
return Cas::getUrl();
return Config::get('cas_url');
}


Expand Down
4 changes: 2 additions & 2 deletions src/Payutc/Service/POSS2.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function getRemoteIp() {
* @return array $url
*/
public function getCasUrl() {
return array("success"=>Cas::getUrl());
return array("success"=>Config::get('cas_url'));
}

/**
Expand Down Expand Up @@ -119,7 +119,7 @@ public function logout() {
unset($this->Seller);
session_destroy();
Log::info("logout() : OK");
return array("success"=>"ok", "url"=>Cas::getUrl()."/logout");
return array("success"=>"ok", "url"=>Config::get('cas_url')."/logout");
} else {
Log::warn("logout() : No seller loaded");
return array("error"=>"1401", "error_msg"=>"Aucun seller n'est logué.");
Expand Down
66 changes: 66 additions & 0 deletions tests/Payutc/CasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php


include 'bootstrap.php';


use \Payutc\Cas;
use \Payutc\Config;

class CasTest extends PHPUnit_Framework_TestCase
{
public function __construct()
{
global $_CONFIG;
Config::initFromArray($_CONFIG);
}

public function testConstruct()
{
$cas = new Cas("http://cas.coucou.fr/");
}

public function testAuthenticateSuccess()
{
$cas = new Cas(Config::get('cas_url'));
$user = $cas->authenticate('trecouvr@coucou', 'coucou');
$this->assertEquals('trecouvr', $user);
}

/**
* @expectedException \Payutc\Exception\AuthenticationFailure
*/
public function testAuthenticationFailure()
{
$cas = new Cas(Config::get('cas_url'));
$cas->authenticate('trecouvr@coou', 'coucou');
}

/**
* @expectedException \Httpful\Exception\ConnectionErrorException
*/
public function testNetworkFailure()
{
$cas = new Cas('http://12345cas.coucou.fr/', 1);
$cas->authenticate('trecouvr@coucou', 'coucou');
}

/**
* @expectedException \Httpful\Exception\ConnectionErrorException
*/
public function testNetworkFailure2()
{
$cas = new Cas('http://localhost:60904/', 1);
$cas->authenticate('trecouvr@coucou', 'coucou');
}

/**
* @expectedException \UnexpectedValueException
*/
public function testNoXmlResponse()
{
$cas = new Cas('http://google.fr/');
$cas->authenticate('trecouvr@coucou', 'coucou');
}
}

0 comments on commit d97f291

Please sign in to comment.