Last active
May 25, 2023 17:11
-
-
Save angelxmoreno/c95a8f21fdadda5c9cd350cf2b216f63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* Test suite bootstrap for SeoBakery. | |
* | |
* This function is used to find the location of CakePHP whether CakePHP | |
* has been installed as a dependency of the plugin, or the plugin is itself | |
* installed as a dependency of an application. | |
*/ | |
use Migrations\TestSuite\Migrator; | |
$findRoot = function ($root) { | |
do { | |
$lastRoot = $root; | |
$root = dirname($root); | |
if (is_dir($root . '/vendor/cakephp/cakephp')) { | |
return $root; | |
} | |
} while ($root !== $lastRoot); | |
throw new Exception('Cannot find the root of the application, unable to run tests'); | |
}; | |
$root = $findRoot(__FILE__); | |
unset($findRoot); | |
chdir($root); | |
require_once $root . '/vendor/autoload.php'; | |
/** | |
* Define fallback values for required constants and configuration. | |
* To customize constants and configuration remove this require | |
* and define the data required by your plugin here. | |
*/ | |
require_once $root . '/vendor/cakephp/cakephp/tests/bootstrap.php'; | |
require $root . '/config/bootstrap.php'; | |
$migrator = new Migrator(); | |
$migrator->run([ | |
'connection' => 'test', | |
'plugin' => 'SeoBakery', | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace SeoBakery\Test\TestCase\Core; | |
use Cake\ORM\Table; | |
use Cake\ORM\TableRegistry; | |
use Cake\TestSuite\TestCase; | |
use SeoBakery\Test\SeoObjects\Product; | |
class SeoAwareEntityTest extends TestCase | |
{ | |
/** | |
* Test subject | |
*/ | |
protected Table $Products; | |
public function setUp(): void | |
{ | |
parent::setUp(); | |
$this->Products = $this->getTableInstance(); | |
TableRegistry::getTableLocator()->set('Products', $this->Products); | |
} | |
protected function getTableInstance(): Table | |
{ | |
$schema = [ | |
'id' => ['type' => 'integer'], | |
'name' => ['type' => 'string'], | |
'description' => ['type' => 'string'], | |
'_constraints' => [ | |
'primary' => ['type' => 'primary', 'columns' => ['id']], | |
], | |
]; | |
$table = new Table([ | |
'alias' => 'Products', | |
'schema' => $schema, | |
'entityClass' => Product::class, | |
]); | |
$table->setPrimaryKey('id'); | |
$table->setDisplayField('name'); | |
return $table; | |
} | |
public function tearDown(): void | |
{ | |
parent::tearDown(); | |
unset($this->Products); | |
TableRegistry::getTableLocator()->remove('Products'); | |
} | |
public function testSomething(): void | |
{ | |
$data = [ | |
'name' => 'Product 1', | |
'description' => 'description 1', | |
]; | |
$entity = new Product($data, [ | |
'source' => 'Products' | |
]); | |
$this->assertSame($data['name'], $entity->buildMetaTitleFallback('view')); | |
} | |
} |
The issue was that inside the entity I am doing TableRegistry::getTableLocator()->get($entity->getSource());
which looks for an alias that does no exist 🤷
The solution was to manually add the alias https://gist.github.com/angelxmoreno/c95a8f21fdadda5c9cd350cf2b216f63#file-seoawareentitytest-php-L22
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when I run
./vendor/bin/phpunit --filter SeoAwareEntityTest
I get: