Skip to content

Commit

Permalink
Merge branch 'feature/factory'
Browse files Browse the repository at this point in the history
  • Loading branch information
cambell-prince committed Jul 28, 2020
2 parents 4cb65e8 + 4d25524 commit 4368795
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 73 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"autoload": {
"psr-4": {
"Anorm\\Test\\": "test/anorm/",
"Anorm\\Tools\\": "tools/src/",
"Anorm\\": "src/"
}
Expand Down
18 changes: 17 additions & 1 deletion src/Anorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class Anorm
{

const DEFAULT = 'default';

private static $connections = array();

/**
Expand Down Expand Up @@ -37,14 +39,28 @@ public static function connect($name, $dsn, $user, $password)
* @return Anorm
* @see connect
*/
public static function use($name)
public static function use($name = self::DEFAULT)
{
if (!\array_key_exists($name, self::$connections)) {
throw new \Exception("Anorm: Connection '$name' doesn't exist. Call Anorm::connection first.");
}
return self::$connections[$name];
}

/**
* Returns the \PDO instance of the Anorm connection of the given $name
* @param string $name Name of the connection to use.
* @return \PDO
* @see connect
*/
public static function pdo($name = self::DEFAULT)
{
if (!\array_key_exists($name, self::$connections)) {
throw new \Exception("Anorm: Connection '$name' doesn't exist. Call Anorm::connection first.");
}
return self::$connections[$name]->pdo;
}

/** @var \PDO The connection */
public $pdo;

Expand Down
9 changes: 9 additions & 0 deletions test/anorm/Anorm_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public function testUse_NotConnected_Fails()
$result = Anorm::use('bogusname');
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Anorm: Connection 'bogusname' doesn't exist. Call Anorm::connection first.
*/
public function testPdo_NotConnected_Fails()
{
$result = Anorm::pdo('bogusname');
}

/**
* @expectedException \PDOException
* @expectedExceptionMessage SQLSTATE[HY000] [1045] Access denied for user 'bogus'@'localhost' (using password: NO)
Expand Down
19 changes: 6 additions & 13 deletions test/anorm/DataMapper_CrudEscape_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@

require_once(__DIR__ . '/../../vendor/autoload.php');

require_once(__DIR__ . '/SomeTableModel.php');
require_once(__DIR__ . '/TestEnvironment.php');

use PHPUnit\Framework\TestCase;

use Anorm\DataMapper;
use Anorm\Model;
use Anorm\Test\SomeTableModel;
use Anorm\Test\TestEnvironment;

class DataMapperCrudEscapeTest extends TestCase
{
/** @var \PDO */
private $pdo;

public function __construct()
{
parent::__construct();
$this->pdo = TestEnvironment::pdo();
TestEnvironment::connect();
}

public static function setUpBeforeClass()
Expand All @@ -31,8 +25,7 @@ public static function setUpBeforeClass()

public function testCrud_OK()
{
$model0 = new SomeTableModel($this->pdo);
$this->assertEquals($this->pdo, $model0->_mapper->pdo);
$model0 = new SomeTableModel();
// Count current rows
$n0 = $model0->countRows();
$this->assertEquals(0, $n0);
Expand All @@ -49,7 +42,7 @@ public function testCrud_OK()
$this->assertEquals($n0 + 1, $n1);

// Read (data present)
$model1 = new SomeTableModel($this->pdo);
$model1 = new SomeTableModel();
$model1->read($model0->someId);
$this->assertEquals($model0->name, $model1->name);
$this->assertEquals($model0->dtc, $model1->dtc);
Expand All @@ -59,7 +52,7 @@ public function testCrud_OK()
$model1->write();

// Read (data changed)
$model2 = new SomeTableModel($this->pdo);
$model2 = new SomeTableModel();
$model2->read($model1->someId);
$this->assertEquals($model1->name, $model2->name);

Expand Down
31 changes: 15 additions & 16 deletions test/anorm/DataMapper_Crud_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

require_once(__DIR__ . '/../../vendor/autoload.php');

require_once(__DIR__ . '/SomeTableModel.php');

use PHPUnit\Framework\TestCase;

use Anorm\Anorm;
use Anorm\DataMapper;
use Anorm\Model;
use Anorm\Test\SomeTableModel;
use Anorm\Test\TestEnvironment;

class BogusModel extends Model {
public function __construct(\PDO $pdo)
public function __construct()
{
$pdo = Anorm::pdo();
parent::__construct($pdo, DataMapper::createByClass($pdo, $this));
$this->_mapper->modelPrimaryKey = 'someId';
}
Expand All @@ -23,27 +25,24 @@ public function __construct(\PDO $pdo)

class DataMapperCrudTest extends TestCase
{
/** @var \PDO */
private $pdo;

public function __construct()
{
parent::__construct();
$this->pdo = new \PDO('mysql:host=localhost;dbname=anorm_test', 'travis', '');
TestEnvironment::connect();
}

public static function setUpBeforeClass()
{
$pdo = new \PDO('mysql:host=localhost;dbname=anorm_test', 'travis', '');
$pdo = TestEnvironment::pdo();
$pdo->query('DROP TABLE IF EXISTS `some_table`');
$sql = file_get_contents(__DIR__ . '/TestSchema.sql');
$pdo->query($sql);
}

public function testCrud_OK()
{
$model0 = new SomeTableModel($this->pdo);
$this->assertEquals($this->pdo, $model0->_mapper->pdo);
$model0 = new SomeTableModel();
// Count current rows
$n0 = $model0->countRows();
$this->assertEquals(0, $n0);
Expand All @@ -60,7 +59,7 @@ public function testCrud_OK()
$this->assertEquals($n0 + 1, $n1);

// Read (data present)
$model1 = new SomeTableModel($this->pdo);
$model1 = new SomeTableModel();
$model1->read($model0->someId);
$this->assertEquals($model0->name, $model1->name);
$this->assertEquals($model0->dtc, $model1->dtc);
Expand All @@ -70,7 +69,7 @@ public function testCrud_OK()
$model1->write();

// Read (data changed)
$model2 = new SomeTableModel($this->pdo);
$model2 = new SomeTableModel();
$model2->read($model1->someId);
$this->assertEquals($model1->name, $model2->name);

Expand All @@ -85,18 +84,18 @@ public function testCrud_OK()

function testDateWrite_Null_Ok()
{
$model1 = new SomeTableModel($this->pdo);
$model1 = new SomeTableModel();
$model1->dtc = null;
$model1->name = 'bob';
$id = $model1->write();
$model2 = new SomeTableModel($this->pdo);
$model2 = new SomeTableModel();
$model2->read($id);
$this->assertNull($model2->dtc);
}

function testBogusRead_Fails()
{
$model = new SomeTableModel($this->pdo);
$model = new SomeTableModel();
$result = $model->read('1');
$this->assertFalse($result);
}
Expand All @@ -107,7 +106,7 @@ function testBogusRead_Fails()
*/
function testBogusWrite_Fails()
{
$model = new BogusModel($this->pdo);
$model = new BogusModel();
$result = $model->write();
$this->assertFalse($result);
}
Expand All @@ -118,7 +117,7 @@ function testBogusWrite_Fails()
*/
function testBogusDelete_Fails()
{
$model = new BogusModel($this->pdo);
$model = new BogusModel();
$result = $model->_mapper->delete('bogus');
$this->assertFalse($result);
}
Expand Down
26 changes: 10 additions & 16 deletions test/anorm/DataMapper_Dynamic_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use PHPUnit\Framework\TestCase;

use Anorm\Anorm;
use Anorm\DataMapper;
use Anorm\Model;
use Anorm\Test\TestEnvironment;

class NotYetModel extends Model {
public function __construct(\PDO $pdo)
public function __construct()
{
$pdo = Anorm::pdo();
parent::__construct($pdo, DataMapper::createByClass($pdo, $this));
$this->_mapper->mode = DataMapper::MODE_DYNAMIC;
}
Expand All @@ -27,33 +30,24 @@ public function countRows()

class DataMapperDynamicTest extends TestCase
{
/** @var \PDO */
private $pdo;

public function __construct()
{
parent::__construct();
$this->pdo = new \PDO('mysql:host=localhost;dbname=anorm_test', 'travis', '');
}

public function setUp()
{
$this->pdo->query('DROP TABLE IF EXISTS `not_yet`');
$pdo = TestEnvironment::pdo();
$pdo->query('DROP TABLE IF EXISTS `not_yet`');
}

public function testFindOne_OK()
{
/** @var NotYetModel */
$model = DataMapper::find('NotYetModel', $this->pdo)
$model = DataMapper::find('NotYetModel', Anorm::pdo())
->where("`name`=:name", [':name' => 'Name 1'])
->one();
$this->assertTrue(true); // Just testing that we haven't yet thrown.
}

public function testCrud_OK()
{
$model0 = new NotYetModel($this->pdo);
$this->assertEquals($this->pdo, $model0->_mapper->pdo);
$model0 = new NotYetModel();
// Count current rows
$n0 = $model0->countRows();
$this->assertEquals(0, $n0);
Expand All @@ -70,7 +64,7 @@ public function testCrud_OK()
$this->assertEquals($n0 + 1, $n1);

// Read (data present)
$model1 = new NotYetModel($this->pdo);
$model1 = new NotYetModel();
$model1->read($model0->id);
$this->assertEquals($model0->name, $model1->name);
$this->assertEquals($model0->dtc, $model1->dtc);
Expand All @@ -80,7 +74,7 @@ public function testCrud_OK()
$model1->write();

// Read (data changed)
$model2 = new NotYetModel($this->pdo);
$model2 = new NotYetModel();
$model2->read($model1->id);
$this->assertEquals($model1->name, $model2->name);

Expand Down
18 changes: 8 additions & 10 deletions test/anorm/DataMapper_Find_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

require_once(__DIR__ . '/../../vendor/autoload.php');

require_once(__DIR__ . '/SomeTableModel.php');
require_once(__DIR__ . '/TestEnvironment.php');

use PHPUnit\Framework\TestCase;

use Anorm\DataMapper;
use Anorm\Model;
use Anorm\Test\SomeTableModel;
use Anorm\Test\TestEnvironment;

class DataMapperFindTest extends TestCase
{
Expand Down Expand Up @@ -40,7 +38,7 @@ public static function setUpBeforeClass()
// public function testFindOne_OK()
// {
// /** @var SomeTableModel */
// $model = DataMapper::find('SomeTableModel', $this->pdo)
// $model = DataMapper::find(SomeTableModel::class, $this->pdo)
// ->where("`name`='Name 1'")
// ->one();
// $this->assertEquals('Name 1', $model->name);
Expand All @@ -49,7 +47,7 @@ public static function setUpBeforeClass()
public function testFindOne_OK()
{
/** @var SomeTableModel */
$model = DataMapper::find('SomeTableModel', $this->pdo)
$model = DataMapper::find(SomeTableModel::class, $this->pdo)
->where("`name`=:name", [':name' => 'Name 1'])
->one();
$this->assertEquals('Name 1', $model->name);
Expand All @@ -58,15 +56,15 @@ public function testFindOne_OK()
public function testFindOneOrThrow_OK()
{
/** @var SomeTableModel */
$model = DataMapper::find('SomeTableModel', $this->pdo)
$model = DataMapper::find(SomeTableModel::class, $this->pdo)
->where("`name`=:name", [':name' => 'Name 1'])
->oneOrThrow();
$this->assertEquals('Name 1', $model->name);
}

public function testFindSome_OK()
{
$generator = DataMapper::find('SomeTableModel', $this->pdo)
$generator = DataMapper::find(SomeTableModel::class, $this->pdo)
->orderBy("name")
->limit(3)
->some();
Expand All @@ -81,7 +79,7 @@ public function testFindSome_OK()
public function testFindOne_NotPresent_False()
{
/** @var SomeTableModel */
$model = DataMapper::find('SomeTableModel', $this->pdo)
$model = DataMapper::find(SomeTableModel::class, $this->pdo)
->where("`name`=:name", [':name' => 'Bogus Name'])
->one();
$this->assertEquals(false, $model);
Expand All @@ -94,7 +92,7 @@ public function testFindOne_NotPresent_False()
public function testFindOneOrThrow_NotPresent_Throws()
{
/** @var SomeTableModel */
$model = DataMapper::find('SomeTableModel', $this->pdo)
$model = DataMapper::find(SomeTableModel::class, $this->pdo)
->where("`name`=:name", [':name' => 'Bogus Name'])
->oneOrThrow();
}
Expand Down
Loading

0 comments on commit 4368795

Please sign in to comment.