Skip to content

Commit

Permalink
phpcs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nag0n committed Mar 12, 2024
1 parent 8a2c97c commit ea0d8d4
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 102 deletions.
72 changes: 36 additions & 36 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ abstract class ActiveRecord extends Base implements JsonSerializable
*/
protected array $sqlExpressions = [];

/**
* @var string SQL that is built to be used by execute()
*/
protected string $built_sql = '';
/**
* @var string SQL that is built to be used by execute()
*/
protected string $built_sql = '';

/**
* Captures all the joins that are made
*
* @var Expressions|null
*/
protected ?Expressions $join = null;
/**
* Captures all the joins that are made
*
* @var Expressions|null
*/
protected ?Expressions $join = null;

/**
* Database connection
Expand Down Expand Up @@ -373,19 +373,19 @@ public function getDatabaseConnection()
return $this->databaseConnection;
}

/**
* set the database connection.
* @param DatabaseInterface|mysqli|PDO $databaseConnection
* @return void
*/
public function setDatabaseConnection($databaseConnection): void
{
if (($databaseConnection instanceof DatabaseInterface) === true) {
/**
* set the database connection.
* @param DatabaseInterface|mysqli|PDO $databaseConnection
* @return void
*/
public function setDatabaseConnection($databaseConnection): void
{
if (($databaseConnection instanceof DatabaseInterface) === true) {
$this->databaseConnection = $databaseConnection;
} else {
$this->transformAndPersistConnection($databaseConnection);
}
}
$this->transformAndPersistConnection($databaseConnection);
}
}

/**
* function to find one record and assign in to current object.
Expand Down Expand Up @@ -500,7 +500,7 @@ public function save(): ActiveRecord
}
}

return $record;
return $record;
}

/**
Expand Down Expand Up @@ -562,10 +562,10 @@ public function query(string $sql, array $param = [], ActiveRecord $obj = null,
protected function &getRelation(string $name)
{

// can't set the name of a relation to a protected keyword
if(in_array($name, ['select', 'from', 'join', 'where', 'group', 'having', 'order', 'limit', 'offset'], true) === true) {
throw new Exception($name. ' is a protected keyword and cannot be used as a relation name');
}
// can't set the name of a relation to a protected keyword
if (in_array($name, ['select', 'from', 'join', 'where', 'group', 'having', 'order', 'limit', 'offset'], true) === true) {
throw new Exception($name . ' is a protected keyword and cannot be used as a relation name');
}

$relation = $this->relations[$name];
if (is_array($relation) === true) {
Expand Down Expand Up @@ -636,19 +636,19 @@ protected function buildSql(array $sql_statements = []): string
}
//this code to debug info.
//echo 'SQL: ', implode(' ', $sql_statements), "\n", "PARAMS: ", implode(', ', $this->params), "\n";
$this->built_sql = implode(' ', $sql_statements);
$this->built_sql = implode(' ', $sql_statements);
return $this->built_sql;
}

/**
* Gets the built SQL after buildSql has been called
*
* @return string
*/
public function getBuiltSql(): string
{
return $this->built_sql;
}
/**
* Gets the built SQL after buildSql has been called
*
* @return string
*/
public function getBuiltSql(): string
{
return $this->built_sql;
}
/**
* make wrap when build the SQL expressions of WHERE.
* @param string $op If give this param will build one WrapExpressions include the stored expressions add into WHERE. Otherwise will stored the expressions into array.
Expand Down
103 changes: 52 additions & 51 deletions tests/ActiveRecordPdoIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,61 +492,62 @@ public function testIsHydratedGoodFindAll()
$this->assertTrue($users[0]->isHydrated());
}

public function testRelationsCascadingSave()
public function testRelationsCascadingSave()
{
$user = new User(new PDO('sqlite:test.db'));
$user->name = 'demo';
$user->password = md5('demo');
$user->insert();

$user->name = 'bobby';
$user->contact->user_id = $user->id;
$user->contact->email = '[email protected]';
$user->contact->address = 'test address';
$user->save();

$this->assertEquals($user->id, $user->contact->user_id);
$this->assertFalse($user->contact->isDirty());
$this->assertGreaterThan(0, $user->contact->id);
$this->assertFalse($user->isDirty());
}

public function testSetDatabaseConnection()
{
$user = new User();
$user->setDatabaseConnection(new PDO('sqlite:test.db'));
$user->name = 'bob';
$user->password = 'pass';
$user->save();

$this->assertGreaterThan(0, $user->id);
}

public function testSetDatabaseConnectionWithAdapter()
{
$user = new User();
$user->setDatabaseConnection(new PdoAdapter(new PDO('sqlite:test.db')));
$user->name = 'bob';
$user->password = 'pass';
$user->save();

$this->assertGreaterThan(0, $user->id);
}

public function testRelationWithProtectedKeyword() {
$user = new User(new PDO('sqlite:test.db'));
$user->name = 'demo';
$user->password = md5('demo');
$user->insert();

$contact = new class(new PDO('sqlite:test.db')) extends ActiveRecord {
protected array $relations = [
'group' => [self::HAS_ONE, User::class, 'user_id']
];
};

$this->expectException(\Exception::class);
$this->expectExceptionMessage('group is a protected keyword and cannot be used as a relation name');
$contact->group->id;
}
$user->name = 'bobby';
$user->contact->user_id = $user->id;
$user->contact->email = '[email protected]';
$user->contact->address = 'test address';
$user->save();

$this->assertEquals($user->id, $user->contact->user_id);
$this->assertFalse($user->contact->isDirty());
$this->assertGreaterThan(0, $user->contact->id);
$this->assertFalse($user->isDirty());
}

public function testSetDatabaseConnection()
{
$user = new User();
$user->setDatabaseConnection(new PDO('sqlite:test.db'));
$user->name = 'bob';
$user->password = 'pass';
$user->save();

$this->assertGreaterThan(0, $user->id);
}

public function testSetDatabaseConnectionWithAdapter()
{
$user = new User();
$user->setDatabaseConnection(new PdoAdapter(new PDO('sqlite:test.db')));
$user->name = 'bob';
$user->password = 'pass';
$user->save();

$this->assertGreaterThan(0, $user->id);
}

public function testRelationWithProtectedKeyword()
{
$user = new User(new PDO('sqlite:test.db'));
$user->name = 'demo';
$user->password = md5('demo');
$user->insert();

$contact = new class (new PDO('sqlite:test.db')) extends ActiveRecord {
protected array $relations = [
'group' => [self::HAS_ONE, User::class, 'user_id']
];
};

$this->expectException(\Exception::class);
$this->expectExceptionMessage('group is a protected keyword and cannot be used as a relation name');
$contact->group->id;
}
}
30 changes: 15 additions & 15 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,26 @@ public function testCopyFrom()
$this->assertEquals(['name' => 'John'], $record->getData());
}

public function testIsset()
{
$record = new class (null, 'test_table') extends ActiveRecord {
};
$record->name = 'John';
$this->assertTrue(isset($record->name));
$this->assertFalse(isset($record->email));
}
public function testIsset()
{
$record = new class (null, 'test_table') extends ActiveRecord {
};
$record->name = 'John';
$this->assertTrue(isset($record->name));
$this->assertFalse(isset($record->email));
}

public function testMultipleJoins()
public function testMultipleJoins()
{
$record = new class (null, 'test_table') extends ActiveRecord {
public function query(string $sql, array $param = [], ?ActiveRecord $obj = null, bool $single = false)
{
return $this;
}
public function query(string $sql, array $param = [], ?ActiveRecord $obj = null, bool $single = false)
{
return $this;
}
};
$record->join('table1', 'table1.some_id = test_table.id');
$record->join('table2', 'table2.some_id = table1.id');
$record->join('table2', 'table2.some_id = table1.id');
$result = $record->find()->getBuiltSql();
$this->assertEquals('SELECT test_table.* FROM test_table LEFT JOIN table1 ON table1.some_id = test_table.id LEFT JOIN table2 ON table2.some_id = table1.id LIMIT 1 ', $result);
$this->assertEquals('SELECT test_table.* FROM test_table LEFT JOIN table1 ON table1.some_id = test_table.id LEFT JOIN table2 ON table2.some_id = table1.id LIMIT 1 ', $result);
}
}

0 comments on commit ea0d8d4

Please sign in to comment.