Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mollux committed Jun 13, 2023
1 parent 31e5da1 commit d050597
Showing 1 changed file with 75 additions and 25 deletions.
100 changes: 75 additions & 25 deletions app/bundles/LeadBundle/Tests/Controller/CompanyControllerTest.php
Expand Up @@ -4,47 +4,63 @@

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\Company;
use Mautic\LeadBundle\Entity\Lead;
use Symfony\Component\HttpFoundation\Response;

class CompanyControllerTest extends MauticMysqlTestCase
{
private $id;
private int $company1Id;

private int $company2Id;

protected function setUp(): void
{
parent::setUp();

$companyData = [
$companiesData = [
1 => [
'name' => 'Amazon',
'state' => 'Washington',
'city' => 'Seattle',
'country' => 'United States',
'industry' => 'Goods',
],
2 => [
'name' => 'Google',
'state' => 'Washington',
'city' => 'Seattle',
'country' => 'United States',
'industry' => 'Services',
],
];

/** @var CompanyModel $model */
$model = self::$container->get('mautic.lead.model.company');
$company = new Company();
$company->setIsPublished(true)
->setName($companyData['name'])
->setState($companyData['state'])
->setCity($companyData['city'])
->setCountry($companyData['country'])
->setIndustry($companyData['industry']);
$model->saveEntity($company);
$this->id = $company->getId();
/** @var \Mautic\LeadBundle\Model\CompanyModel $model */
$model = self::getContainer()->get('mautic.lead.model.company');

foreach ($companiesData as $i => $companyData) {
$company = new Company();
$company->setIsPublished(true)
->setName($companyData['name'])
->setState($companyData['state'])
->setCity($companyData['city'])
->setCountry($companyData['country'])
->setIndustry($companyData['industry']);
$model->saveEntity($company);

$this->{'company'.$i.'Id'} = $company->getId();
}
}

/**
* Get company's view page.
*/
public function testViewActionCompany(): void
{
$this->client->request('GET', '/s/companies/view/'.$this->id);
$this->client->request('GET', '/s/companies/view/'.$this->company1Id);
$clientResponse = $this->client->getResponse();
$clientResponseContent = $clientResponse->getContent();
$model = self::$container->get('mautic.lead.model.company');
$company = $model->getEntity($this->id);
$model = self::getContainer()->get('mautic.lead.model.company');
$company = $model->getEntity($this->company1Id);
$this->assertEquals(Response::HTTP_OK, $clientResponse->getStatusCode());
$this->assertStringContainsString($company->getName(), $clientResponseContent, 'The return must contain the name of company');
}
Expand All @@ -54,24 +70,58 @@ public function testViewActionCompany(): void
*/
public function testEditActionCompany(): void
{
$this->client->request('GET', '/s/companies/edit/'.$this->id);
$this->client->request('GET', '/s/companies/edit/'.$this->company1Id);
$clientResponse = $this->client->getResponse();
$clientResponseContent = $clientResponse->getContent();
$model = self::$container->get('mautic.lead.model.company');
$company = $model->getEntity($this->id);
$model = self::getContainer()->get('mautic.lead.model.company');
$company = $model->getEntity($this->company1Id);
$this->assertEquals(Response::HTTP_OK, $clientResponse->getStatusCode());
$this->assertStringContainsString('Edit Company '.$company->getName(), $clientResponseContent, 'The return must contain \'Edit Company\' text');
}

/* Get company contacts list */
public function testListCompanyContacts(): void
{
$this->client->request('GET', 's/company/'.$this->id.'/contacts/');
$clientResponse = $this->client->getResponse();
$clientResponseContent = $clientResponse->getContent();
$model = self::$container->get('mautic.lead.model.company');
$company = $model->getEntity($this->id);
$this->assertEquals(Response::HTTP_OK, $clientResponse->getStatusCode());
/** @var \Mautic\LeadBundle\Model\CompanyModel $companyModel */
$companyModel = self::getContainer()->get('mautic.lead.model.company');
$leadModel = self::getContainer()->get('mautic.lead.model.lead');
$company1 = $companyModel->getEntity($this->company1Id);

// Create a lead linked to the first company
$lead1 = new Lead();
$lead1
->setFirstname('lead')
->setLastname('for '.$company1->getName());
$leadModel->saveEntity($lead1);

$companyModel->addLeadToCompany($company1, $lead1);

// Create a lead not linked to a company
$lead2 = new Lead();
$lead2
->setFirstname('lead')
->setLastname('without company');
$leadModel->saveEntity($lead2);

// Create a lead not linked to a company, but with `ids` in it's name (see https://github.com/mautic/mautic/issues/12415)
$lead3 = new Lead();
$lead3
->setFirstname('lead')
->setLastname('without company')
->setEmail('example@idstart.com');
$leadModel->saveEntity($lead3);

$crawler = $this->client->request('GET', '/s/company/'.$this->company1Id.'/contacts/');
$leadsTableRows = $crawler->filterXPath("//table[@id='leadTable']//tbody//tr");

$this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$this->assertEquals(1, $leadsTableRows->count(), $crawler->html());

$crawler = $this->client->request('GET', '/s/company/'.$this->company2Id.'/contacts/');
$leadsTableRows = $crawler->filterXPath("//table[@id='leadTable']//tbody//tr");

$this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$this->assertEquals(0, $leadsTableRows->count(), $crawler->html());
}

/**
Expand Down

0 comments on commit d050597

Please sign in to comment.