mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-29 11:10:19 +02:00
@@ -4,14 +4,8 @@ namespace DesignPatterns\Creational\Pool\Tests;
|
|||||||
|
|
||||||
use DesignPatterns\Creational\Pool\Pool;
|
use DesignPatterns\Creational\Pool\Pool;
|
||||||
|
|
||||||
class TestWorker
|
|
||||||
{
|
|
||||||
public $id = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
class PoolTest extends \PHPUnit_Framework_TestCase
|
class PoolTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function testPool()
|
public function testPool()
|
||||||
{
|
{
|
||||||
$pool = new Pool('DesignPatterns\Creational\Pool\Tests\TestWorker');
|
$pool = new Pool('DesignPatterns\Creational\Pool\Tests\TestWorker');
|
||||||
|
8
Creational/Pool/Tests/TestWorker.php
Normal file
8
Creational/Pool/Tests/TestWorker.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Creational\Pool\Tests;
|
||||||
|
|
||||||
|
class TestWorker
|
||||||
|
{
|
||||||
|
public $id = 1;
|
||||||
|
}
|
@@ -40,7 +40,7 @@ class MemoryStorage implements Storage
|
|||||||
*/
|
*/
|
||||||
public function delete($id)
|
public function delete($id)
|
||||||
{
|
{
|
||||||
if(!isset($this->data[$id])){
|
if (!isset($this->data[$id])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,6 +49,4 @@ class MemoryStorage implements Storage
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace DesignPatterns\Repository;
|
namespace DesignPatterns\Repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Post represents entity for some post that user left on the site
|
* Post represents entity for some post that user left on the site
|
||||||
*
|
*
|
||||||
@@ -34,9 +35,6 @@ class Post
|
|||||||
*/
|
*/
|
||||||
private $created;
|
private $created;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $id
|
* @param int $id
|
||||||
*/
|
*/
|
||||||
@@ -116,7 +114,4 @@ class Post
|
|||||||
{
|
{
|
||||||
return $this->title;
|
return $this->title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@@ -5,8 +5,12 @@ namespace DesignPatterns\Repository;
|
|||||||
/**
|
/**
|
||||||
* Repository for class Post
|
* Repository for class Post
|
||||||
* This class is between Entity layer(class Post) and access object layer(interface Storage)
|
* This class is between Entity layer(class Post) and access object layer(interface Storage)
|
||||||
* Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer
|
*
|
||||||
* Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers
|
* Repository encapsulates the set of objects persisted in a data store and the operations performed over them
|
||||||
|
* providing a more object-oriented view of the persistence layer
|
||||||
|
*
|
||||||
|
* Repository also supports the objective of achieving a clean separation and one-way dependency
|
||||||
|
* between the domain and data mapping layers
|
||||||
*
|
*
|
||||||
* Class PostRepository
|
* Class PostRepository
|
||||||
* @package DesignPatterns\Repository
|
* @package DesignPatterns\Repository
|
||||||
@@ -29,7 +33,7 @@ class PostRepository
|
|||||||
public function getById($id)
|
public function getById($id)
|
||||||
{
|
{
|
||||||
$arrayData = $this->persistence->retrieve($id);
|
$arrayData = $this->persistence->retrieve($id);
|
||||||
if(is_null($arrayData)){
|
if (is_null($arrayData)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -33,8 +33,15 @@ class ServiceLocatorTest extends TestCase
|
|||||||
|
|
||||||
public function testHasServices()
|
public function testHasServices()
|
||||||
{
|
{
|
||||||
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService);
|
$this->serviceLocator->add(
|
||||||
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService);
|
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
|
||||||
|
$this->logService
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->serviceLocator->add(
|
||||||
|
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
|
||||||
|
$this->databaseService
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
|
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
|
||||||
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
|
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
|
||||||
@@ -44,35 +51,92 @@ class ServiceLocatorTest extends TestCase
|
|||||||
|
|
||||||
public function testServicesWithObject()
|
public function testServicesWithObject()
|
||||||
{
|
{
|
||||||
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService);
|
$this->serviceLocator->add(
|
||||||
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService);
|
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
|
||||||
|
$this->logService
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
|
$this->serviceLocator->add(
|
||||||
$this->assertSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
|
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
|
||||||
|
$this->databaseService
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
$this->logService,
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
$this->databaseService,
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testServicesWithClass()
|
public function testServicesWithClass()
|
||||||
{
|
{
|
||||||
$this->serviceLocator
|
$this->serviceLocator->add(
|
||||||
->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', get_class($this->logService));
|
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
|
||||||
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', get_class($this->databaseService));
|
get_class($this->logService)
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
|
$this->serviceLocator->add(
|
||||||
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
|
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
|
||||||
|
get_class($this->databaseService)
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
|
$this->assertNotSame(
|
||||||
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
|
$this->logService,
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotSame(
|
||||||
|
$this->databaseService,
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testServicesNotShared()
|
public function testServicesNotShared()
|
||||||
{
|
{
|
||||||
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService, false);
|
$this->serviceLocator->add(
|
||||||
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService, false);
|
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
|
||||||
|
$this->logService,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
|
$this->serviceLocator->add(
|
||||||
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
|
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
|
||||||
|
$this->databaseService,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
|
$this->assertNotSame(
|
||||||
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
|
$this->logService,
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertNotSame(
|
||||||
|
$this->databaseService,
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
|
||||||
|
$this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface')
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user